blob: 4447186e228d3d74e1d3ea702b37608d28104dbe [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;
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000143 if (sourcing_a_script(eap))
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200144 {
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 Moolenaarfe8e9f62022-03-16 13:09:15 +0000698 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100700 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200701 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000702 funcexe.fe_firstline = curwin->w_cursor.lnum;
703 funcexe.fe_lastline = curwin->w_cursor.lnum;
704 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000705
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000706 // The name might be "import.Func" or "Funcref". We don't know, we need to
707 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000708 // autoload script has errors. Guess that when there is a dot in the name
709 // showing errors is the right choice.
710 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000711 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000712 if (ignore_errors)
713 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000714 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000715 if (ignore_errors)
716 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000717 if (name == NULL)
718 name = func;
719
720 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
721
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722 if (ret == FAIL)
723 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000724 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000725
726 return ret;
727}
728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100729/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100730 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000731 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
732 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000733 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734 */
735 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100736call_func_retstr(
737 char_u *func,
738 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200739 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000740{
741 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000742 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743
Bram Moolenaarded27a12018-08-01 19:06:03 +0200744 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000745 return NULL;
746
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100747 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return retval;
750}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000752/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100753 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000754 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 */
757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758call_func_retlist(
759 char_u *func,
760 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200761 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000762{
763 typval_T rettv;
764
Bram Moolenaarded27a12018-08-01 19:06:03 +0200765 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 return NULL;
767
768 if (rettv.v_type != VAR_LIST)
769 {
770 clear_tv(&rettv);
771 return NULL;
772 }
773
774 return rettv.vval.v_list;
775}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaare70dd112022-01-21 16:31:11 +0000777#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100779 * Evaluate "arg", which is 'foldexpr'.
780 * Note: caller must set "curwin" to match "arg".
781 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
782 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000787 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000788 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200789 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000791 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000792 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
793 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 arg = wp->w_p_fde;
796 current_sctx = wp->w_p_script_ctx[WV_FDE];
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000799 if (use_sandbox)
800 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200801 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200803 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
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 number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 if (tv.v_type == VAR_NUMBER)
809 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000810 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 retval = 0;
812 else
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // If the result is a string, check if there is a non-digit before
815 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (!VIM_ISDIGIT(*s) && *s != '-')
818 *cp = *s++;
819 retval = atol((char *)s);
820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
823 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000824 if (use_sandbox)
825 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200826 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200827 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000828 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200830 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 * Get an lval: variable, Dict item or List item that can be assigned a value
836 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
837 * "name.key", "name.key[expr]" etc.
838 * Indexing only works if "name" is an existing List or Dictionary.
839 * "name" points to the start of the name.
840 * If "rettv" is not NULL it points to the value to be assigned.
841 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
842 * wrong; must end in space or cmd separator.
843 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100844 * flags:
845 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100846 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100847 * GLV_NO_AUTOLOAD: do not use script autoloading
848 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000850 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200853 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100854get_lval(
855 char_u *name,
856 typval_T *rettv,
857 lval_T *lp,
858 int unlet,
859 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 int flags, // GLV_ values
861 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 char_u *expr_start, *expr_end;
865 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 dictitem_T *v;
867 typval_T var1;
868 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100872 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100873 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100874 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000875 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200878 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100880 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100882 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200884 lp->ll_name_end = find_name_end(name, NULL, NULL,
885 FNE_INCL_BR | fne_flags);
886 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 }
888
Bram Moolenaara749a422022-02-12 19:52:25 +0000889 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000890 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000891 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
892 {
893 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
894 return NULL;
895 }
896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000898 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if (expr_start != NULL)
901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100903 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 && *p != '[' && *p != '.')
905 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000906 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 return NULL;
908 }
909
910 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
911 if (lp->ll_exp_name == NULL)
912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100913 // Report an invalid expression in braces, unless the
914 // expression evaluation has been cancelled due to an
915 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 if (!aborting() && !quiet)
917 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000918 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000919 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 return NULL;
921 }
922 }
923 lp->ll_name = lp->ll_exp_name;
924 }
925 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 lp->ll_name = name;
928
Bram Moolenaar4525a572022-02-13 11:57:33 +0000929 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200931 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000932 // However, "g:[key]" is indexing a dictionary.
933 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200934 {
935 --p;
936 lp->ll_name_end = p;
937 }
938 if (*p == ':')
939 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000940 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200941
942 if (tp == p + 1 && !quiet)
943 {
944 semsg(_(e_white_space_required_after_str_str), ":", p);
945 return NULL;
946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000948 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000949 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000950 semsg(_(e_using_type_not_in_script_context_str), p);
951 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000952 }
953
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200954 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000955 lp->ll_type = parse_type(&tp,
956 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
957 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100958 if (lp->ll_type == NULL && !quiet)
959 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200960 lp->ll_name_end = tp;
961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 }
963 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000964 if (lp->ll_name == NULL)
965 return p;
966
Bram Moolenaar62aec932022-01-29 21:45:34 +0000967 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000968 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000969 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000970
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000971 if (import != NULL)
972 {
973 ufunc_T *ufunc;
974 type_T *type;
975
976 lp->ll_sid = import->imp_sid;
977 lp->ll_name = skipwhite(p + 1);
978 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
979 lp->ll_name_end = p;
980
981 // check the item is exported
982 cc = *p;
983 *p = NUL;
984 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000985 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000986 {
987 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000988 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000989 }
990 *p = cc;
991 }
992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100994 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000995 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return p;
997
Bram Moolenaar4525a572022-02-13 11:57:33 +0000998 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200999 {
1000 // using local variable
1001 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001002 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001003 }
1004 else
1005 {
1006 cc = *p;
1007 *p = NUL;
1008 // When we would write to the variable pass &ht and prevent autoload.
1009 writing = !(flags & GLV_READ_ONLY);
1010 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001011 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001012 if (v == NULL && !quiet)
1013 semsg(_(e_undefined_variable_str), lp->ll_name);
1014 *p = cc;
1015 if (v == NULL)
1016 return NULL;
1017 lp->ll_tv = &v->di_tv;
1018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019
Bram Moolenaar4525a572022-02-13 11:57:33 +00001020 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001021 {
1022 if (!quiet)
1023 semsg(_(e_variable_already_declared), lp->ll_name);
1024 return NULL;
1025 }
1026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 /*
1028 * Loop until no more [idx] or .key is following.
1029 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001030 var1.v_type = VAR_UNKNOWN;
1031 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001032 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001034 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1035 {
1036 if (!quiet)
1037 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1038 return NULL;
1039 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001040 if (lp->ll_tv->v_type != VAR_LIST
1041 && lp->ll_tv->v_type != VAR_DICT
1042 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001045 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001048
1049 // a NULL list/blob works like an empty list/blob, allocate one now.
1050 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1051 rettv_list_alloc(lp->ll_tv);
1052 else if (lp->ll_tv->v_type == VAR_BLOB
1053 && lp->ll_tv->vval.v_blob == NULL)
1054 rettv_blob_alloc(lp->ll_tv);
1055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001057 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001059 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001062
Bram Moolenaar4525a572022-02-13 11:57:33 +00001063 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001064 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001065 && lp->ll_tv == &v->di_tv
1066 && ht != NULL && ht == get_script_local_ht())
1067 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001068 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001069
1070 // Vim9 script local variable: get the type
1071 if (sv != NULL)
1072 lp->ll_valtype = sv->sv_type;
1073 }
1074
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 len = -1;
1076 if (*p == '.')
1077 {
1078 key = p + 1;
1079 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1080 ;
1081 if (len == 0)
1082 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001084 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 }
1087 p = key + len;
1088 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001089 else
1090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001092 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001093 if (*p == ':')
1094 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001095 else
1096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001098 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001099 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001100 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001103 clear_tv(&var1);
1104 return NULL;
1105 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001106 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 }
1108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001109 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 if (*p == ':')
1111 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001113 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001115 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001116 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001118 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001119 if (rettv != NULL
1120 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001121 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001122 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001123 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001126 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001127 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 }
1130 p = skipwhite(p + 1);
1131 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 else
1134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001136 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001137 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001146 clear_tv(&var2);
1147 return NULL;
1148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001157 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001158 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
1160 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 }
1163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 ++p;
1166 }
1167
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 {
1170 if (len == -1)
1171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // "[key]": get key from "var1"
1173 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001174 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 }
1179 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001180 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001181
1182 // a NULL dict is equivalent with an empty dict
1183 if (lp->ll_tv->vval.v_dict == NULL)
1184 {
1185 lp->ll_tv->vval.v_dict = dict_alloc();
1186 if (lp->ll_tv->vval.v_dict == NULL)
1187 {
1188 clear_tv(&var1);
1189 return NULL;
1190 }
1191 ++lp->ll_tv->vval.v_dict->dv_refcount;
1192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001193 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001194
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001195 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001197 // When assigning to a scope dictionary check that a function and
1198 // variable name is valid (only variable name unless it is l: or
1199 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001200 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001201 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001202 int prevval;
1203 int wrong;
1204
1205 if (len != -1)
1206 {
1207 prevval = key[len];
1208 key[len] = NUL;
1209 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001210 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001212 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1213 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001214 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001215 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001216 if (len != -1)
1217 key[len] = prevval;
1218 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001219 {
1220 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001221 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001222 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001223 }
1224
Bram Moolenaar10c65862020-10-08 21:16:42 +02001225 if (lp->ll_valtype != NULL)
1226 // use the type of the member
1227 lp->ll_valtype = lp->ll_valtype->tt_member;
1228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001231 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001232 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001233 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001234 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001235 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001236 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001237 return NULL;
1238 }
1239
Bram Moolenaar31b81602019-02-10 22:14:27 +01001240 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001244 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001245 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 }
1248 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001252 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001254 p = NULL;
1255 break;
1256 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001258 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001259 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1260 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001261 {
1262 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001263 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001264 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001265
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001266 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001269 else if (lp->ll_tv->v_type == VAR_BLOB)
1270 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001271 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1272
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001273 /*
1274 * Get the number and item for the only or first index of the List.
1275 */
1276 if (empty1)
1277 lp->ll_n1 = 0;
1278 else
1279 // is number or string
1280 lp->ll_n1 = (long)tv_get_number(&var1);
1281 clear_tv(&var1);
1282
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001283 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001285 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 return NULL;
1287 }
1288 if (lp->ll_range && !lp->ll_empty2)
1289 {
1290 lp->ll_n2 = (long)tv_get_number(&var2);
1291 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001292 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1293 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 }
1296 lp->ll_blob = lp->ll_tv->vval.v_blob;
1297 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001298 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 else
1301 {
1302 /*
1303 * Get the number and item for the only or first index of the List.
1304 */
1305 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001307 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001308 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001309 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001310 clear_tv(&var1);
1311
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001312 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001314 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001315 if (lp->ll_li == NULL)
1316 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001317 clear_tv(&var2);
1318 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001319 }
1320
Bram Moolenaar10c65862020-10-08 21:16:42 +02001321 if (lp->ll_valtype != NULL)
1322 // use the type of the member
1323 lp->ll_valtype = lp->ll_valtype->tt_member;
1324
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 /*
1326 * May need to find the item or absolute index for the second
1327 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 * When no index given: "lp->ll_empty2" is TRUE.
1329 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001330 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001333 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001334 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001336 if (check_range_index_two(lp->ll_list,
1337 &lp->ll_n1, lp->ll_li,
1338 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001340 }
1341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001343 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 }
1345
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001346 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return p;
1349}
1350
1351/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356{
1357 vim_free(lp->ll_exp_name);
1358 vim_free(lp->ll_newkey);
1359}
1360
1361/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001362 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001364 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1365 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001368set_var_lval(
1369 lval_T *lp,
1370 char_u *endp,
1371 typval_T *rettv,
1372 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001373 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1374 char_u *op,
1375 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376{
1377 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379
1380 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001382 cc = *endp;
1383 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001384 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1385 return;
1386
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001387 if (lp->ll_blob != NULL)
1388 {
1389 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001390
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001391 if (op != NULL && *op != '=')
1392 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001393 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001394 return;
1395 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001397 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001398
1399 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1400 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001401 if (lp->ll_empty2)
1402 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1403
Bram Moolenaar68452172021-04-12 21:21:02 +02001404 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1405 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407 }
1408 else
1409 {
1410 val = (int)tv_get_number_chk(rettv, &error);
1411 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001412 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001413 }
1414 }
1415 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001417 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001419 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1420 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001421 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001422 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001423 *endp = cc;
1424 return;
1425 }
1426
Bram Moolenaarff697e62019-02-12 22:28:33 +01001427 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001428 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001429 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001430 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001431 {
1432 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001433 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1434 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001435 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001436 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001437 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001438 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001440 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001442 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001443 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1444 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001445 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001446 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001447 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001448 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001449 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001451 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001452 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001453 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001454 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 else if (lp->ll_range)
1456 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001457 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1458 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001459 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001460 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001461 return;
1462 }
1463
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001464 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1465 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 }
1467 else
1468 {
1469 /*
1470 * Assign to a List or Dictionary item.
1471 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001472 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1473 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001474 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001475 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001476 return;
1477 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001478
1479 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001480 && check_typval_arg_type(lp->ll_valtype, rettv,
1481 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001482 return;
1483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 if (lp->ll_newkey != NULL)
1485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486 if (op != NULL && *op != '=')
1487 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001488 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 return;
1490 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001491 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1492 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001493 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001495 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001496 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001497 if (di == NULL)
1498 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001499 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1500 {
1501 vim_free(di);
1502 return;
1503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 else if (op != NULL && *op != '=')
1507 {
1508 tv_op(lp->ll_tv, rettv, op);
1509 return;
1510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 /*
1515 * Assign the value to the variable or list item.
1516 */
1517 if (copy)
1518 copy_tv(rettv, lp->ll_tv);
1519 else
1520 {
1521 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001522 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 }
1525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526}
1527
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001529 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1530 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 * Returns OK or FAIL.
1532 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001536 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 char_u numbuf[NUMBUFLEN];
1538 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001539 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001541 // Can't do anything with a Funcref or Dict on the right.
1542 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001543 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001544 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1545 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 {
1547 switch (tv1->v_type)
1548 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001549 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001550 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 case VAR_DICT:
1553 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001554 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001555 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001556 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001557 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001558 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001559 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001560 break;
1561
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001562 case VAR_BLOB:
1563 if (*op != '+' || tv2->v_type != VAR_BLOB)
1564 break;
1565 // BLOB += BLOB
1566 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1567 {
1568 blob_T *b1 = tv1->vval.v_blob;
1569 blob_T *b2 = tv2->vval.v_blob;
1570 int i, len = blob_len(b2);
1571 for (i = 0; i < len; i++)
1572 ga_append(&b1->bv_ga, blob_get(b2, i));
1573 }
1574 return OK;
1575
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 case VAR_LIST:
1577 if (*op != '+' || tv2->v_type != VAR_LIST)
1578 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001580 if (tv2->vval.v_list != NULL)
1581 {
1582 if (tv1->vval.v_list == NULL)
1583 {
1584 tv1->vval.v_list = tv2->vval.v_list;
1585 ++tv1->vval.v_list->lv_refcount;
1586 }
1587 else
1588 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 return OK;
1591
1592 case VAR_NUMBER:
1593 case VAR_STRING:
1594 if (tv2->v_type == VAR_LIST)
1595 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001599 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600#ifdef FEAT_FLOAT
1601 if (tv2->v_type == VAR_FLOAT)
1602 {
1603 float_T f = n;
1604
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 if (*op == '%')
1606 break;
1607 switch (*op)
1608 {
1609 case '+': f += tv2->vval.v_float; break;
1610 case '-': f -= tv2->vval.v_float; break;
1611 case '*': f *= tv2->vval.v_float; break;
1612 case '/': f /= tv2->vval.v_float; break;
1613 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001614 clear_tv(tv1);
1615 tv1->v_type = VAR_FLOAT;
1616 tv1->vval.v_float = f;
1617 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619#endif
1620 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001621 switch (*op)
1622 {
1623 case '+': n += tv_get_number(tv2); break;
1624 case '-': n -= tv_get_number(tv2); break;
1625 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001626 case '/': n = num_divide(n, tv_get_number(tv2),
1627 &failed); break;
1628 case '%': n = num_modulus(n, tv_get_number(tv2),
1629 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 clear_tv(tv1);
1632 tv1->v_type = VAR_NUMBER;
1633 tv1->vval.v_number = n;
1634 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 }
1636 else
1637 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 if (tv2->v_type == VAR_FLOAT)
1639 break;
1640
Bram Moolenaarff697e62019-02-12 22:28:33 +01001641 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001642 s = tv_get_string(tv1);
1643 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 clear_tv(tv1);
1645 tv1->v_type = VAR_STRING;
1646 tv1->vval.v_string = s;
1647 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001648 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001650 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001651#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001652 {
1653 float_T f;
1654
Bram Moolenaarff697e62019-02-12 22:28:33 +01001655 if (*op == '%' || *op == '.'
1656 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001657 && tv2->v_type != VAR_NUMBER
1658 && tv2->v_type != VAR_STRING))
1659 break;
1660 if (tv2->v_type == VAR_FLOAT)
1661 f = tv2->vval.v_float;
1662 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001663 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 switch (*op)
1665 {
1666 case '+': tv1->vval.v_float += f; break;
1667 case '-': tv1->vval.v_float -= f; break;
1668 case '*': tv1->vval.v_float *= f; break;
1669 case '/': tv1->vval.v_float /= f; break;
1670 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001671 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001672#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001673 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 }
1675 }
1676
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001677 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678 return FAIL;
1679}
1680
1681/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 * Evaluate the expression used in a ":for var in expr" command.
1683 * "arg" points to "var".
1684 * Set "*errp" to TRUE for an error, FALSE otherwise;
1685 * Return a pointer that holds the info. Null when there is an error.
1686 */
1687 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001688eval_for_line(
1689 char_u *arg,
1690 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001691 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693{
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001695 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001697 typval_T tv;
1698 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001699 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001701 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001703 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 if (fi == NULL)
1705 return NULL;
1706
Bram Moolenaar404557e2021-07-05 21:41:48 +02001707 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1708 &fi->fi_semicolon, FALSE);
1709 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 return fi;
1711
Bram Moolenaar404557e2021-07-05 21:41:48 +02001712 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001713 if (expr[0] != 'i' || expr[1] != 'n'
1714 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001716 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1717 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1718 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001719 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return fi;
1721 }
1722
1723 if (skip)
1724 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001725 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1726 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 {
1728 *errp = FALSE;
1729 if (!skip)
1730 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001732 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 l = tv.vval.v_list;
1734 if (l == NULL)
1735 {
1736 // a null list is like an empty list: do nothing
1737 clear_tv(&tv);
1738 }
1739 else
1740 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001742 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 // No need to increment the refcount, it's already set for
1745 // the list being used in "tv".
1746 fi->fi_list = l;
1747 list_add_watch(l, &fi->fi_lw);
1748 fi->fi_lw.lw_item = l->lv_first;
1749 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001750 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001751 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001752 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001753 fi->fi_bi = 0;
1754 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001755 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001756 typval_T btv;
1757
1758 // Make a copy, so that the iteration still works when the
1759 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001761 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001762 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001764 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001765 else if (tv.v_type == VAR_STRING)
1766 {
1767 fi->fi_byte_idx = 0;
1768 fi->fi_string = tv.vval.v_string;
1769 tv.vval.v_string = NULL;
1770 if (fi->fi_string == NULL)
1771 fi->fi_string = vim_strsave((char_u *)"");
1772 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 else
1774 {
Sean Dewar80d73952021-08-04 19:25:54 +02001775 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001776 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 }
1778 }
1779 }
1780 if (skip)
1781 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001782 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783
1784 return fi;
1785}
1786
1787/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001788 * Used when looping over a :for line, skip the "in expr" part.
1789 */
1790 void
1791skip_for_lines(void *fi_void, evalarg_T *evalarg)
1792{
1793 forinfo_T *fi = (forinfo_T *)fi_void;
1794 int i;
1795
1796 for (i = 0; i < fi->fi_break_count; ++i)
1797 eval_next_line(evalarg);
1798}
1799
1800/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 * Use the first item in a ":for" list. Advance to the next.
1802 * Assign the values to the variable (list). "arg" points to the first one.
1803 * Return TRUE when a valid item was found, FALSE when at end of list or
1804 * something wrong.
1805 */
1806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001809 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001811 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001812 ? (ASSIGN_FINAL
1813 // first round: error if variable exists
1814 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1815 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001816 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001817 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001818 int skip_assign = in_vim9script() && arg[0] == '_'
1819 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001821 if (fi->fi_blob != NULL)
1822 {
1823 typval_T tv;
1824
1825 if (fi->fi_bi >= blob_len(fi->fi_blob))
1826 return FALSE;
1827 tv.v_type = VAR_NUMBER;
1828 tv.v_lock = VAR_FIXED;
1829 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1830 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001831 if (skip_assign)
1832 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001833 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001834 fi->fi_varcount, flag, NULL) == OK;
1835 }
1836
1837 if (fi->fi_string != NULL)
1838 {
1839 typval_T tv;
1840 int len;
1841
1842 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1843 if (len == 0)
1844 return FALSE;
1845 tv.v_type = VAR_STRING;
1846 tv.v_lock = VAR_FIXED;
1847 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1848 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001849 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001850 if (skip_assign)
1851 result = TRUE;
1852 else
1853 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001854 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001855 vim_free(tv.vval.v_string);
1856 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001857 }
1858
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 item = fi->fi_lw.lw_item;
1860 if (item == NULL)
1861 result = FALSE;
1862 else
1863 {
1864 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001865 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001866 if (skip_assign)
1867 result = TRUE;
1868 else
1869 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001870 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 }
1872 return result;
1873}
1874
1875/*
1876 * Free the structure used to store info used by ":for".
1877 */
1878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001879free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880{
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001883 if (fi == NULL)
1884 return;
1885 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001888 list_unref(fi->fi_list);
1889 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001890 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001891 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 else
1893 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 vim_free(fi);
1895}
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898set_context_for_expression(
1899 expand_T *xp,
1900 char_u *arg,
1901 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001903 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 if (cmdidx == CMD_let || cmdidx == CMD_var
1908 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 {
1910 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001914 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 {
1916 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001917 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001918 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 break;
1920 }
1921 return;
1922 }
1923 }
1924 else
1925 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1926 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 while ((xp->xp_pattern = vim_strpbrk(arg,
1928 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1929 {
1930 c = *xp->xp_pattern;
1931 if (c == '&')
1932 {
1933 c = xp->xp_pattern[1];
1934 if (c == '&')
1935 {
1936 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001937 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1943 xp->xp_pattern += 2;
1944
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947 else if (c == '$')
1948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 xp->xp_context = EXPAND_ENV_VARS;
1951 }
1952 else if (c == '=')
1953 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001954 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 xp->xp_context = EXPAND_EXPRESSION;
1956 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001957 else if (c == '#'
1958 && xp->xp_context == EXPAND_EXPRESSION)
1959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001961 break;
1962 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001963 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 && xp->xp_context == EXPAND_FUNCTIONS
1965 && vim_strchr(xp->xp_pattern, '(') == NULL)
1966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001967 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 break;
1969 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001970 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1975 if (c == '\\' && xp->xp_pattern[1] != NUL)
1976 ++xp->xp_pattern;
1977 xp->xp_context = EXPAND_NOTHING;
1978 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1983 /* skip */ ;
1984 xp->xp_context = EXPAND_NOTHING;
1985 }
1986 else if (c == '|')
1987 {
1988 if (xp->xp_pattern[1] == '|')
1989 {
1990 ++xp->xp_pattern;
1991 xp->xp_context = EXPAND_EXPRESSION;
1992 }
1993 else
1994 xp->xp_context = EXPAND_COMMANDS;
1995 }
1996 else
1997 xp->xp_context = EXPAND_EXPRESSION;
1998 }
1999 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002000 // Doesn't look like something valid, expand as an expression
2001 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 arg = xp->xp_pattern;
2004 if (*arg != NUL)
2005 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2006 /* skip */ ;
2007 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002008
2009 // ":exe one two" completes "two"
2010 if ((cmdidx == CMD_execute
2011 || cmdidx == CMD_echo
2012 || cmdidx == CMD_echon
2013 || cmdidx == CMD_echomsg)
2014 && xp->xp_context == EXPAND_EXPRESSION)
2015 {
2016 for (;;)
2017 {
2018 char_u *n = skiptowhite(arg);
2019
2020 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2021 break;
2022 arg = skipwhite(n);
2023 }
2024 }
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 xp->xp_pattern = arg;
2027}
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002030 * Return TRUE if "pat" matches "text".
2031 * Does not use 'cpo' and always uses 'magic'.
2032 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002033 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002034pattern_match(char_u *pat, char_u *text, int ic)
2035{
2036 int matches = FALSE;
2037 char_u *save_cpo;
2038 regmatch_T regmatch;
2039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002041 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002042 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2044 if (regmatch.regprog != NULL)
2045 {
2046 regmatch.rm_ic = ic;
2047 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2048 vim_regfree(regmatch.regprog);
2049 }
2050 p_cpo = save_cpo;
2051 return matches;
2052}
2053
2054/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 * Handle a name followed by "(". Both for just "name(arg)" and for
2056 * "expr->name(arg)".
2057 * Returns OK or FAIL.
2058 */
2059 static int
2060eval_func(
2061 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002062 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 char_u *name,
2064 int name_len,
2065 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002066 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 typval_T *basetv) // "expr" for "expr->name(arg)"
2068{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002069 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002070 char_u *s = name;
2071 int len = name_len;
2072 partial_T *partial;
2073 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002074 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002075 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076
2077 if (!evaluate)
2078 check_vars(s, len);
2079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002080 // If "s" is the name of a variable of type VAR_FUNC
2081 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002082 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002083 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 // Need to make a copy, in case evaluating the arguments makes
2086 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002087 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002088 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002089 ret = FAIL;
2090 else
2091 {
2092 funcexe_T funcexe;
2093
2094 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002095 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002096 funcexe.fe_firstline = curwin->w_cursor.lnum;
2097 funcexe.fe_lastline = curwin->w_cursor.lnum;
2098 funcexe.fe_evaluate = evaluate;
2099 funcexe.fe_partial = partial;
2100 funcexe.fe_basetv = basetv;
2101 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002102 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002103 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002104 }
2105 vim_free(s);
2106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 // If evaluate is FALSE rettv->v_type was not set in
2108 // get_func_tv, but it's needed in handle_subscript() to parse
2109 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002110 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2111 {
2112 rettv->vval.v_string = NULL;
2113 rettv->v_type = VAR_FUNC;
2114 }
2115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // Stop the expression evaluation when immediately
2117 // aborting on error, or when an interrupt occurred or
2118 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002119 if (evaluate && aborting())
2120 {
2121 if (ret == OK)
2122 clear_tv(rettv);
2123 ret = FAIL;
2124 }
2125 return ret;
2126}
2127
2128/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002129 * Get the next line source line without advancing. But do skip over comment
2130 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002131 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002132 */
2133 static char_u *
2134getline_peek_skip_comments(evalarg_T *evalarg)
2135{
2136 for (;;)
2137 {
2138 char_u *next = getline_peek(evalarg->eval_getline,
2139 evalarg->eval_cookie);
2140 char_u *p;
2141
2142 if (next == NULL)
2143 break;
2144 p = skipwhite(next);
2145 if (*p != NUL && !vim9_comment_start(p))
2146 return next;
Bram Moolenaar43216612022-03-25 11:16:28 +00002147 if (eval_next_line(evalarg) == NULL)
2148 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002149 }
2150 return NULL;
2151}
2152
2153/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002154 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2155 * comment) and there is a next line, return the next line (skipping blanks)
2156 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002157 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2158 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 * "arg" must point somewhere inside a line, not at the start.
2160 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002161 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002162eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2163{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002164 char_u *p = skipwhite(arg);
2165
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002166 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002167 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002169 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002170 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002172 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002173
2174 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002175 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002176 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002177 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178
Bram Moolenaar9d489562020-07-30 20:08:50 +02002179 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 {
2181 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 }
2184 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002185 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002186}
2187
2188/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002189 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002190 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002191 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002192 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002193eval_next_line(evalarg_T *evalarg)
2194{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002195 garray_T *gap = &evalarg->eval_ga;
2196 char_u *line;
2197
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002198 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002199 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2200 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002201 else
2202 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002203 if (line == NULL)
2204 return NULL;
2205
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002206 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002207 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2208 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002209 char_u *p = skipwhite(line);
2210
2211 // Going to concatenate the lines after parsing. For an empty or
2212 // comment line use an empty string.
2213 if (*p == NUL || vim9_comment_start(p))
2214 {
2215 vim_free(line);
2216 line = vim_strsave((char_u *)"");
2217 }
2218
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002219 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2220 ++gap->ga_len;
2221 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002222 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002223 {
2224 vim_free(evalarg->eval_tofree);
2225 evalarg->eval_tofree = line;
2226 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002227
2228 // Advanced to the next line, "arg" no longer points into the previous
2229 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002230 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002231 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002232}
2233
Bram Moolenaare6b53242020-07-01 17:28:33 +02002234/*
2235 * Call eval_next_non_blank() and get the next line if needed.
2236 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002237 char_u *
2238skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2239{
2240 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002241 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002242
Bram Moolenaar962d7212020-07-04 14:15:00 +02002243 if (evalarg == NULL)
2244 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002245 eval_next_non_blank(p, evalarg, &getnext);
2246 if (getnext)
2247 return eval_next_line(evalarg);
2248 return p;
2249}
2250
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002251/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002252 * Initialize "evalarg" for use.
2253 */
2254 void
2255init_evalarg(evalarg_T *evalarg)
2256{
2257 CLEAR_POINTER(evalarg);
2258 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2259}
2260
2261/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002262 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002263 */
2264 void
2265clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2266{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002267 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002268 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002269 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002270 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002271 if (eap != NULL)
2272 {
2273 // We may need to keep the original command line, e.g. for
2274 // ":let" it has the variable names. But we may also need the
2275 // new one, "nextcmd" points into it. Keep both.
2276 vim_free(eap->cmdline_tofree);
2277 eap->cmdline_tofree = *eap->cmdlinep;
2278 *eap->cmdlinep = evalarg->eval_tofree;
2279 }
2280 else
2281 vim_free(evalarg->eval_tofree);
2282 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002283 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002284
Bram Moolenaar844fb642021-10-23 13:32:30 +01002285 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002286 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002287 }
2288}
2289
2290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2294 */
2295
2296/*
2297 * Handle zero level expression.
2298 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002300 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * Return OK or FAIL.
2303 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002305eval0(
2306 char_u *arg,
2307 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002308 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002311 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2312}
2313
2314/*
2315 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2316 * expression and don't check what comes after the expression.
2317 */
2318 int
2319eval0_retarg(
2320 char_u *arg,
2321 typval_T *rettv,
2322 exarg_T *eap,
2323 evalarg_T *evalarg,
2324 char_u **retarg)
2325{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int ret;
2327 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002328 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002329 int did_emsg_before = did_emsg;
2330 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002331 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002332 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002333 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002336 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002337 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002338 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002339
Bram Moolenaar02929a32021-12-17 14:46:12 +00002340 // In Vim9 script a command block is not split at NL characters for
2341 // commands using an expression argument. Skip over a '#' comment to check
2342 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002343 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002344 while (*p == '#')
2345 {
2346 char_u *nl = vim_strchr(p, NL);
2347
2348 if (nl == NULL)
2349 break;
2350 p = skipwhite(nl + 1);
2351 if (eap != NULL && *p != NUL)
2352 eap->nextcmd = p;
2353 check_for_end = FALSE;
2354 }
2355
2356 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002357 end_error = !ends_excmd2(arg, p);
2358 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /*
2363 * Report the invalid expression unless the expression evaluation has
2364 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002365 * exception, or we already gave a more specific error.
2366 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002368 if (!aborting()
2369 && did_emsg == did_emsg_before
2370 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002371 && (flags & EVAL_CONSTANT) == 0
2372 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002373 {
2374 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002375 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002376 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002377 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002378 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002379
2380 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002381 // a next command to avoid more errors, unless "|" is following, which
2382 // could only be a command separator.
2383 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2384 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002385 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002387
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002388 if (retarg != NULL)
2389 *retarg = p;
2390 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002391 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return ret;
2394}
2395
2396/*
2397 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002398 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002399 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 *
2401 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002402 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002404 * Note: "rettv.v_lock" is not set.
2405 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 * Return OK or FAIL.
2407 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002408 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002409eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002411 char_u *p;
2412 int getnext;
2413
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002414 CLEAR_POINTER(rettv);
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 /*
2417 * Get the first variable.
2418 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return FAIL;
2421
Bram Moolenaar793648f2020-06-26 21:28:25 +02002422 p = eval_next_non_blank(*arg, evalarg, &getnext);
2423 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002425 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 int result;
2427 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428 evalarg_T *evalarg_used = evalarg;
2429 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002430 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002431 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002432 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433
2434 if (evalarg == NULL)
2435 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002436 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002438 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 orig_flags = evalarg_used->eval_flags;
2440 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 if (getnext)
2443 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002444 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002446 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002448 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002449 clear_tv(rettv);
2450 return FAIL;
2451 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002452 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002453 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 int error = FALSE;
2459
Bram Moolenaar13106602020-10-04 16:06:05 +02002460 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002461 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002462 else if (vim9script)
2463 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002464 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002466 if (error || !op_falsy || !result)
2467 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 if (error)
2469 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 }
2471
2472 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002473 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002475 if (op_falsy)
2476 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002477 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002478 {
mityu4ac198c2021-05-28 17:52:40 +02002479 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002480 clear_tv(rettv);
2481 return FAIL;
2482 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002483 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002484 evalarg_used->eval_flags = (op_falsy ? !result : result)
2485 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2486 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002487 {
2488 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002490 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 if (!op_falsy || !result)
2492 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002494 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002496 /*
2497 * Check for the ":".
2498 */
2499 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2500 if (*p != ':')
2501 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002502 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002503 if (evaluate && result)
2504 clear_tv(rettv);
2505 evalarg_used->eval_flags = orig_flags;
2506 return FAIL;
2507 }
2508 if (getnext)
2509 *arg = eval_next_line(evalarg_used);
2510 else
2511 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002512 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002513 {
2514 error_white_both(p, 1);
2515 clear_tv(rettv);
2516 evalarg_used->eval_flags = orig_flags;
2517 return FAIL;
2518 }
2519 *arg = p;
2520 }
2521
2522 /*
2523 * Get the third variable. Recursive!
2524 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002525 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002526 {
mityu4ac198c2021-05-28 17:52:40 +02002527 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002528 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002529 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002530 return FAIL;
2531 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002532 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2533 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002535 if (eval1(arg, &var2, evalarg_used) == FAIL)
2536 {
2537 if (evaluate && result)
2538 clear_tv(rettv);
2539 evalarg_used->eval_flags = orig_flags;
2540 return FAIL;
2541 }
2542 if (evaluate && !result)
2543 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002545
2546 if (evalarg == NULL)
2547 clear_evalarg(&local_evalarg, NULL);
2548 else
2549 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551
2552 return OK;
2553}
2554
2555/*
2556 * Handle first level expression:
2557 * expr2 || expr2 || expr2 logical OR
2558 *
2559 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002560 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 *
2562 * Return OK or FAIL.
2563 */
2564 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 char_u *p;
2568 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 /*
2571 * Get the first variable.
2572 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 return FAIL;
2575
2576 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002579 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 evalarg_T *evalarg_used = evalarg;
2583 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 int evaluate;
2585 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 long result = FALSE;
2587 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002588 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002589 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 if (evalarg == NULL)
2592 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002593 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 orig_flags = evalarg_used->eval_flags;
2597 evaluate = orig_flags & EVAL_EVALUATE;
2598 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002600 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002601 result = tv_get_bool_chk(rettv, &error);
2602 else if (tv_get_number_chk(rettv, &error) != 0)
2603 result = TRUE;
2604 clear_tv(rettv);
2605 if (error)
2606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
2609 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612 while (p[0] == '|' && p[1] == '|')
2613 {
2614 if (getnext)
2615 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002616 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002617 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002618 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 {
2620 error_white_both(p, 2);
2621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002624 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626
2627 /*
2628 * Get the second variable.
2629 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002630 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002631 {
mityu4ac198c2021-05-28 17:52:40 +02002632 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002633 clear_tv(rettv);
2634 return FAIL;
2635 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2637 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002638 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002640 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641
2642 /*
2643 * Compute the result.
2644 */
2645 if (evaluate && !result)
2646 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002647 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002648 result = tv_get_bool_chk(&var2, &error);
2649 else if (tv_get_number_chk(&var2, &error) != 0)
2650 result = TRUE;
2651 clear_tv(&var2);
2652 if (error)
2653 return FAIL;
2654 }
2655 if (evaluate)
2656 {
2657 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002658 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002659 rettv->v_type = VAR_BOOL;
2660 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002661 }
2662 else
2663 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002664 rettv->v_type = VAR_NUMBER;
2665 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002666 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668
2669 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002671
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672 if (evalarg == NULL)
2673 clear_evalarg(&local_evalarg, NULL);
2674 else
2675 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677
2678 return OK;
2679}
2680
2681/*
2682 * Handle second level expression:
2683 * expr3 && expr3 && expr3 logical AND
2684 *
2685 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002686 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 *
2688 * Return OK or FAIL.
2689 */
2690 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002693 char_u *p;
2694 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
2697 * Get the first variable.
2698 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return FAIL;
2701
2702 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002705 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 evalarg_T *evalarg_used = evalarg;
2709 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 int orig_flags;
2711 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 long result = TRUE;
2713 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002714 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002715 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002716
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717 if (evalarg == NULL)
2718 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002719 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002721 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002722 orig_flags = evalarg_used->eval_flags;
2723 evaluate = orig_flags & EVAL_EVALUATE;
2724 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002726 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002727 result = tv_get_bool_chk(rettv, &error);
2728 else if (tv_get_number_chk(rettv, &error) == 0)
2729 result = FALSE;
2730 clear_tv(rettv);
2731 if (error)
2732 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734
2735 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738 while (p[0] == '&' && p[1] == '&')
2739 {
2740 if (getnext)
2741 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002742 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002743 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002744 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002745 {
2746 error_white_both(p, 2);
2747 clear_tv(rettv);
2748 return FAIL;
2749 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002750 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002751 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002752
2753 /*
2754 * Get the second variable.
2755 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002756 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002757 {
mityu4ac198c2021-05-28 17:52:40 +02002758 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002759 clear_tv(rettv);
2760 return FAIL;
2761 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2763 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002764 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002765 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002766 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002768
2769 /*
2770 * Compute the result.
2771 */
2772 if (evaluate && result)
2773 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002774 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002775 result = tv_get_bool_chk(&var2, &error);
2776 else if (tv_get_number_chk(&var2, &error) == 0)
2777 result = FALSE;
2778 clear_tv(&var2);
2779 if (error)
2780 return FAIL;
2781 }
2782 if (evaluate)
2783 {
2784 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002785 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002786 rettv->v_type = VAR_BOOL;
2787 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002788 }
2789 else
2790 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002791 rettv->v_type = VAR_NUMBER;
2792 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002793 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002794 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002795
2796 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002798
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799 if (evalarg == NULL)
2800 clear_evalarg(&local_evalarg, NULL);
2801 else
2802 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804
2805 return OK;
2806}
2807
2808/*
2809 * Handle third level expression:
2810 * var1 == var2
2811 * var1 =~ var2
2812 * var1 != var2
2813 * var1 !~ var2
2814 * var1 > var2
2815 * var1 >= var2
2816 * var1 < var2
2817 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002818 * var1 is var2
2819 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 *
2821 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002822 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 *
2824 * Return OK or FAIL.
2825 */
2826 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002830 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002831 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002833 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /*
2836 * Get the first variable.
2837 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002838 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 return FAIL;
2840
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002841 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002842 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002845 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002847 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002849 typval_T var2;
2850 int ic;
2851 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002852 int evaluate = evalarg == NULL
2853 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002854 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002855
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002856 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002857 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002858 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002859 p = *arg;
2860 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002861 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2862 {
mityu4ac198c2021-05-28 17:52:40 +02002863 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002864 clear_tv(rettv);
2865 return FAIL;
2866 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002867
Bram Moolenaar696ba232020-07-29 21:20:41 +02002868 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2869 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002870 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002871 clear_tv(rettv);
2872 return FAIL;
2873 }
2874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002875 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (p[len] == '?')
2877 {
2878 ic = TRUE;
2879 ++len;
2880 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002881 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 else if (p[len] == '#')
2883 {
2884 ic = FALSE;
2885 ++len;
2886 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002887 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002889 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 /*
2892 * Get the second variable.
2893 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002894 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2895 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002896 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002897 clear_tv(rettv);
2898 return FAIL;
2899 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002900 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002901 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return FAIL;
2905 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002906 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002907 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002908 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002909
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002910 // use the line of the comparison for messages
2911 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002912 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002913 {
2914 ret = FAIL;
2915 clear_tv(rettv);
2916 }
2917 else
2918 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002919 clear_tv(&var2);
2920 return ret;
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
2923
2924 return OK;
2925}
2926
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002927/*
2928 * Make a copy of blob "tv1" and append blob "tv2".
2929 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 void
2931eval_addblob(typval_T *tv1, typval_T *tv2)
2932{
2933 blob_T *b1 = tv1->vval.v_blob;
2934 blob_T *b2 = tv2->vval.v_blob;
2935 blob_T *b = blob_alloc();
2936 int i;
2937
2938 if (b != NULL)
2939 {
2940 for (i = 0; i < blob_len(b1); i++)
2941 ga_append(&b->bv_ga, blob_get(b1, i));
2942 for (i = 0; i < blob_len(b2); i++)
2943 ga_append(&b->bv_ga, blob_get(b2, i));
2944
2945 clear_tv(tv1);
2946 rettv_blob_set(tv1, b);
2947 }
2948}
2949
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002950/*
2951 * Make a copy of list "tv1" and append list "tv2".
2952 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 int
2954eval_addlist(typval_T *tv1, typval_T *tv2)
2955{
2956 typval_T var3;
2957
2958 // concatenate Lists
2959 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2960 {
2961 clear_tv(tv1);
2962 clear_tv(tv2);
2963 return FAIL;
2964 }
2965 clear_tv(tv1);
2966 *tv1 = var3;
2967 return OK;
2968}
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002972 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002974 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002975 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 *
2977 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002978 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 *
2980 * Return OK or FAIL.
2981 */
2982 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002983eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 /*
2986 * Get the first variable.
2987 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 return FAIL;
2990
2991 /*
2992 * Repeat computing, until no '+', '-' or '.' is following.
2993 */
2994 for (;;)
2995 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002996 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997 int getnext;
2998 char_u *p;
2999 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003000 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003001 int concat;
3002 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003003 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003005 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003006 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003007 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003008 p = eval_next_non_blank(*arg, evalarg, &getnext);
3009 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003010 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003011 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3012 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003014 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3015 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003016
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003017 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003018 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003019 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003020 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003021 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003022 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003023 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003024 {
mityu4ac198c2021-05-28 17:52:40 +02003025 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003026 clear_tv(rettv);
3027 return FAIL;
3028 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003029 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003030 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003031 if ((op != '+' || (rettv->v_type != VAR_LIST
3032 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033#ifdef FEAT_FLOAT
3034 && (op == '.' || rettv->v_type != VAR_FLOAT)
3035#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003036 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003038 int error = FALSE;
3039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003040 // For "list + ...", an illegal use of the first operand as
3041 // a number cannot be determined before evaluating the 2nd
3042 // operand: if this is also a list, all is ok.
3043 // For "something . ...", "something - ..." or "non-list + ...",
3044 // we know that the first operand needs to be a string or number
3045 // without evaluating the 2nd operand. So check before to avoid
3046 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003047 if (op != '.')
3048 tv_get_number_chk(rettv, &error);
3049 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003050 {
3051 clear_tv(rettv);
3052 return FAIL;
3053 }
3054 }
3055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 /*
3057 * Get the second variable.
3058 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003059 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003060 {
mityu89dcb4d2021-05-28 13:50:17 +02003061 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003062 clear_tv(rettv);
3063 return FAIL;
3064 }
3065 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003066 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return FAIL;
3070 }
3071
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 /*
3075 * Compute the result.
3076 */
3077 if (op == '.')
3078 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3080 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003081 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003082
Bram Moolenaar2e086612020-08-25 22:37:48 +02003083 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003084 || var2.v_type == VAR_CHANNEL
3085 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003086 semsg(_(e_using_invalid_value_as_string_str),
3087 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003088#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003089 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003090 {
3091 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3092 var2.vval.v_float);
3093 s2 = buf2;
3094 }
3095#endif
3096 else
3097 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003099 {
3100 clear_tv(rettv);
3101 clear_tv(&var2);
3102 return FAIL;
3103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 clear_tv(rettv);
3106 rettv->v_type = VAR_STRING;
3107 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003109 else if (op == '+' && rettv->v_type == VAR_BLOB
3110 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003112 else if (op == '+' && rettv->v_type == VAR_LIST
3113 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003114 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003116 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else
3119 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 int error = FALSE;
3121 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003123 float_T f1 = 0, f2 = 0;
3124
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 f1 = rettv->vval.v_float;
3128 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130 else
3131#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003132 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003133 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134 if (error)
3135 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003136 // This can only happen for "list + non-list" or
3137 // "blob + non-blob". For "non-list + ..." or
3138 // "something - ...", we returned before evaluating the
3139 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003141 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 return FAIL;
3143 }
3144#ifdef FEAT_FLOAT
3145 if (var2.v_type == VAR_FLOAT)
3146 f1 = n1;
3147#endif
3148 }
3149#ifdef FEAT_FLOAT
3150 if (var2.v_type == VAR_FLOAT)
3151 {
3152 f2 = var2.vval.v_float;
3153 n2 = 0;
3154 }
3155 else
3156#endif
3157 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003158 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 if (error)
3160 {
3161 clear_tv(rettv);
3162 clear_tv(&var2);
3163 return FAIL;
3164 }
3165#ifdef FEAT_FLOAT
3166 if (rettv->v_type == VAR_FLOAT)
3167 f2 = n2;
3168#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171
3172#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003173 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3175 {
3176 if (op == '+')
3177 f1 = f1 + f2;
3178 else
3179 f1 = f1 - f2;
3180 rettv->v_type = VAR_FLOAT;
3181 rettv->vval.v_float = f1;
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003184#endif
3185 {
3186 if (op == '+')
3187 n1 = n1 + n2;
3188 else
3189 n1 = n1 - n2;
3190 rettv->v_type = VAR_NUMBER;
3191 rettv->vval.v_number = n1;
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 }
3197 return OK;
3198}
3199
3200/*
3201 * Handle fifth level expression:
3202 * * number multiplication
3203 * / number division
3204 * % number modulo
3205 *
3206 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003207 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 *
3209 * Return OK or FAIL.
3210 */
3211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003212eval6(
3213 char_u **arg,
3214 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003216 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003219 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 /*
3223 * Get the first variable.
3224 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003225 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 return FAIL;
3227
3228 /*
3229 * Repeat computing, until no '*', '/' or '%' is following.
3230 */
3231 for (;;)
3232 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003233 int evaluate;
3234 int getnext;
3235 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003236 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003237 int op;
3238 varnumber_T n1, n2;
3239#ifdef FEAT_FLOAT
3240 float_T f1, f2;
3241#endif
3242 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003243
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003244 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003245 p = eval_next_non_blank(*arg, evalarg, &getnext);
3246 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003247 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003249
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003250 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003251 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003252 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003253 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003254 {
3255 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3256 {
mityu4ac198c2021-05-28 17:52:40 +02003257 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003258 clear_tv(rettv);
3259 return FAIL;
3260 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003261 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003264#ifdef FEAT_FLOAT
3265 f1 = 0;
3266 f2 = 0;
3267#endif
3268 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003269 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271#ifdef FEAT_FLOAT
3272 if (rettv->v_type == VAR_FLOAT)
3273 {
3274 f1 = rettv->vval.v_float;
3275 use_float = TRUE;
3276 n1 = 0;
3277 }
3278 else
3279#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003280 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003281 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003282 if (error)
3283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285 else
3286 n1 = 0;
3287
3288 /*
3289 * Get the second variable.
3290 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003291 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3292 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003293 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003294 clear_tv(rettv);
3295 return FAIL;
3296 }
3297 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003298 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return FAIL;
3300
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003301 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003303#ifdef FEAT_FLOAT
3304 if (var2.v_type == VAR_FLOAT)
3305 {
3306 if (!use_float)
3307 {
3308 f1 = n1;
3309 use_float = TRUE;
3310 }
3311 f2 = var2.vval.v_float;
3312 n2 = 0;
3313 }
3314 else
3315#endif
3316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003317 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318 clear_tv(&var2);
3319 if (error)
3320 return FAIL;
3321#ifdef FEAT_FLOAT
3322 if (use_float)
3323 f2 = n2;
3324#endif
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /*
3328 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331#ifdef FEAT_FLOAT
3332 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334 if (op == '*')
3335 f1 = f1 * f2;
3336 else if (op == '/')
3337 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003338# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003339 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003340 if (f2 == 0.0)
3341 {
3342 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003343 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003344 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003345 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003346 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003347 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003348 }
3349 else
3350 f1 = f1 / f2;
3351# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 // We rely on the floating point library to handle divide
3353 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003355# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003358 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003359 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360 return FAIL;
3361 }
3362 rettv->v_type = VAR_FLOAT;
3363 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003368 int failed = FALSE;
3369
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003370 if (op == '*')
3371 n1 = n1 * n2;
3372 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003373 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003375 n1 = num_modulus(n1, n2, &failed);
3376 if (failed)
3377 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379 rettv->v_type = VAR_NUMBER;
3380 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 }
3384
3385 return OK;
3386}
3387
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003388/*
3389 * Handle a type cast before a base level expression.
3390 * "arg" must point to the first non-white of the expression.
3391 * "arg" is advanced to just after the recognized expression.
3392 * Return OK or FAIL.
3393 */
3394 static int
3395eval7t(
3396 char_u **arg,
3397 typval_T *rettv,
3398 evalarg_T *evalarg,
3399 int want_string) // after "." operator
3400{
3401 type_T *want_type = NULL;
3402 garray_T type_list; // list of pointers to allocated types
3403 int res;
3404 int evaluate = evalarg == NULL ? 0
3405 : (evalarg->eval_flags & EVAL_EVALUATE);
3406
3407 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003408 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3409 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003410 {
3411 ++*arg;
3412 ga_init2(&type_list, sizeof(type_T *), 10);
3413 want_type = parse_type(arg, &type_list, TRUE);
3414 if (want_type == NULL && (evaluate || **arg != '>'))
3415 {
3416 clear_type_list(&type_list);
3417 return FAIL;
3418 }
3419
3420 if (**arg != '>')
3421 {
3422 if (*skipwhite(*arg) == '>')
3423 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3424 else
3425 emsg(_(e_missing_gt));
3426 clear_type_list(&type_list);
3427 return FAIL;
3428 }
3429 ++*arg;
3430 *arg = skipwhite_and_linebreak(*arg, evalarg);
3431 }
3432
3433 res = eval7(arg, rettv, evalarg, want_string);
3434
3435 if (want_type != NULL && evaluate)
3436 {
3437 if (res == OK)
3438 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003439 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3440 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003441
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003442 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003443 {
3444 if (want_type == &t_bool && actual != &t_bool
3445 && (actual->tt_flags & TTFLAG_BOOL_OK))
3446 {
3447 int n = tv2bool(rettv);
3448
3449 // can use "0" and "1" for boolean in some places
3450 clear_tv(rettv);
3451 rettv->v_type = VAR_BOOL;
3452 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3453 }
3454 else
3455 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003456 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003457
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003458 where.wt_variable = TRUE;
3459 res = check_type(want_type, actual, TRUE, where);
3460 }
3461 }
3462 }
3463 clear_type_list(&type_list);
3464 }
3465
3466 return res;
3467}
3468
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003469 int
3470eval_leader(char_u **arg, int vim9)
3471{
3472 char_u *s = *arg;
3473 char_u *p = *arg;
3474
3475 while (*p == '!' || *p == '-' || *p == '+')
3476 {
3477 char_u *n = skipwhite(p + 1);
3478
3479 // ++, --, -+ and +- are not accepted in Vim9 script
3480 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3481 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003482 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003483 return FAIL;
3484 }
3485 p = n;
3486 }
3487 *arg = p;
3488 return OK;
3489}
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003492 * Check for a predefined value "true", "false" and "null.*".
3493 * Return OK when recognized.
3494 */
3495 int
3496handle_predefined(char_u *s, int len, typval_T *rettv)
3497{
3498 switch (len)
3499 {
3500 case 4: if (STRNCMP(s, "true", 4) == 0)
3501 {
3502 rettv->v_type = VAR_BOOL;
3503 rettv->vval.v_number = VVAL_TRUE;
3504 return OK;
3505 }
3506 if (STRNCMP(s, "null", 4) == 0)
3507 {
3508 rettv->v_type = VAR_SPECIAL;
3509 rettv->vval.v_number = VVAL_NULL;
3510 return OK;
3511 }
3512 break;
3513 case 5: if (STRNCMP(s, "false", 5) == 0)
3514 {
3515 rettv->v_type = VAR_BOOL;
3516 rettv->vval.v_number = VVAL_FALSE;
3517 return OK;
3518 }
3519 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003520 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3521 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003523 rettv->v_type = VAR_JOB;
3524 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003525#else
3526 rettv->v_type = VAR_SPECIAL;
3527 rettv->vval.v_number = VVAL_NULL;
3528#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003529 return OK;
3530 }
3531 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003532 case 9:
3533 if (STRNCMP(s, "null_", 5) != 0)
3534 break;
3535 if (STRNCMP(s + 5, "list", 4) == 0)
3536 {
3537 rettv->v_type = VAR_LIST;
3538 rettv->vval.v_list = NULL;
3539 return OK;
3540 }
3541 if (STRNCMP(s + 5, "dict", 4) == 0)
3542 {
3543 rettv->v_type = VAR_DICT;
3544 rettv->vval.v_dict = NULL;
3545 return OK;
3546 }
3547 if (STRNCMP(s + 5, "blob", 4) == 0)
3548 {
3549 rettv->v_type = VAR_BLOB;
3550 rettv->vval.v_blob = NULL;
3551 return OK;
3552 }
3553 break;
3554 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3555 {
3556 rettv->v_type = VAR_STRING;
3557 rettv->vval.v_string = NULL;
3558 return OK;
3559 }
3560 break;
3561 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003562 if (STRNCMP(s, "null_channel", 12) == 0)
3563 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003564#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003565 rettv->v_type = VAR_CHANNEL;
3566 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003567#else
3568 rettv->v_type = VAR_SPECIAL;
3569 rettv->vval.v_number = VVAL_NULL;
3570#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003571 return OK;
3572 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003573 if (STRNCMP(s, "null_partial", 12) == 0)
3574 {
3575 rettv->v_type = VAR_PARTIAL;
3576 rettv->vval.v_partial = NULL;
3577 return OK;
3578 }
3579 break;
3580 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3581 {
3582 rettv->v_type = VAR_FUNC;
3583 rettv->vval.v_string = NULL;
3584 return OK;
3585 }
3586 break;
3587 }
3588 return FAIL;
3589}
3590
3591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * Handle sixth level expression:
3593 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003594 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003595 * "string" string constant
3596 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * &option-name option value
3598 * @r register contents
3599 * identifier variable value
3600 * function() function call
3601 * $VAR environment variable
3602 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003605 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003606 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 *
3608 * Also handle:
3609 * ! in front logical NOT
3610 * - in front unary minus
3611 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 * trailing [] subscript in String or List
3613 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003614 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 *
3616 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003617 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 *
3619 * Return OK or FAIL.
3620 */
3621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003622eval7(
3623 char_u **arg,
3624 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003625 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003628 int evaluate = evalarg != NULL
3629 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 int len;
3631 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003632 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 char_u *start_leader, *end_leader;
3634 int ret = OK;
3635 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003636 static int recurse = 0;
3637 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003641 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003646 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003649 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 end_leader = *arg;
3652
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003653 if (**arg == '.' && (!isdigit(*(*arg + 1))
3654#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003655 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003656#endif
3657 ))
3658 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003659 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003660 ++*arg;
3661 return FAIL;
3662 }
3663
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003664 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003665 // and crash. With MSVC the stack is smaller.
3666 if (recurse ==
3667#ifdef _MSC_VER
3668 300
3669#else
3670 1000
3671#endif
3672 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003673 {
3674 semsg(_(e_expression_too_recursive_str), *arg);
3675 return FAIL;
3676 }
3677 ++recurse;
3678
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 switch (**arg)
3680 {
3681 /*
3682 * Number constant.
3683 */
3684 case '0':
3685 case '1':
3686 case '2':
3687 case '3':
3688 case '4':
3689 case '5':
3690 case '6':
3691 case '7':
3692 case '8':
3693 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003694 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003695
3696 // Apply prefixed "-" and "+" now. Matters especially when
3697 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003698 if (ret == OK && evaluate && end_leader > start_leader
3699 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003700 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702
3703 /*
3704 * String constant: "string".
3705 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003706 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 break;
3708
3709 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003712 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003713 break;
3714
3715 /*
3716 * List: [expr, expr]
3717 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003718 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 break;
3720
3721 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003722 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003723 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003724 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003725 {
3726 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3727 }
3728 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003729 {
3730 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003731 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003732 }
3733 else
3734 ret = NOTDONE;
3735 break;
3736
3737 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003738 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003739 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003740 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003741 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003742 ret = NOTDONE;
3743 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003744 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003745 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003746 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003747 break;
3748
3749 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003750 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003752 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 break;
3754
3755 /*
3756 * Environment variable: $VAR.
3757 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003758 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 break;
3760
3761 /*
3762 * Register contents: @r.
3763 */
3764 case '@': ++*arg;
3765 if (evaluate)
3766 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003767 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003768 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003769 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003770 emsg_invreg(**arg);
3771 else
3772 {
3773 rettv->v_type = VAR_STRING;
3774 rettv->vval.v_string = get_reg_contents(**arg,
3775 GREG_EXPR_SRC);
3776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 if (**arg != NUL)
3779 ++*arg;
3780 break;
3781
3782 /*
3783 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003784 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003786 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003787 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003788 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003789 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003790 if (ret == OK && evaluate)
3791 {
3792 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3793
Bram Moolenaara9931532021-06-12 15:58:16 +02003794 // Compile it here to get the return type. The return
3795 // type is optional, when it's missing use t_unknown.
3796 // This is recognized in compile_return().
3797 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3798 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003799 if (compile_def_function(ufunc, FALSE,
3800 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003801 {
3802 clear_tv(rettv);
3803 ret = FAIL;
3804 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003805 }
3806 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003807 if (ret == NOTDONE)
3808 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003809 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003810 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003811
Bram Moolenaar9215f012020-06-27 21:18:00 +02003812 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003813 if (**arg == ')')
3814 ++*arg;
3815 else if (ret == OK)
3816 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003817 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003818 clear_tv(rettv);
3819 ret = FAIL;
3820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 break;
3823
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 break;
3826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827
3828 if (ret == NOTDONE)
3829 {
3830 /*
3831 * Must be a variable or function name.
3832 * Can also be a curly-braces kind of name: {expr}.
3833 */
3834 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003835 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 if (alias != NULL)
3837 s = alias;
3838
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003839 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003840 ret = FAIL;
3841 else
3842 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003843 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3844
Bram Moolenaar4525a572022-02-13 11:57:33 +00003845 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003846 {
3847 emsg(_(e_cannot_use_underscore_here));
3848 ret = FAIL;
3849 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003850 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003851 && s[0] == 's' && s[1] == ':')
3852 {
3853 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3854 ret = FAIL;
3855 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003856 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003857 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003858 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003859 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003860 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003861 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003862 else if (flags & EVAL_CONSTANT)
3863 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003864 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003865 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003866 // get the value of "true", "false", etc. or a variable
3867 ret = FAIL;
3868 if (vim9script)
3869 ret = handle_predefined(s, len, rettv);
3870 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003871 {
3872 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003873 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003874 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003875 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003876 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003877 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003878 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003879 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003880 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003881 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003882 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003883 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003884 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 }
3886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3888 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003889 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003890 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 /*
3893 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3894 */
3895 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003896 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003897
3898 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003899 return ret;
3900}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003901
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003902/*
3903 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003904 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003905 * Adjusts "end_leaderp" until it is at "start_leader".
3906 */
3907 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003908eval7_leader(
3909 typval_T *rettv,
3910 int numeric_only,
3911 char_u *start_leader,
3912 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003913{
3914 char_u *end_leader = *end_leaderp;
3915 int ret = OK;
3916 int error = FALSE;
3917 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003918 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003919 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003920#ifdef FEAT_FLOAT
3921 float_T f = 0.0;
3922
3923 if (rettv->v_type == VAR_FLOAT)
3924 f = rettv->vval.v_float;
3925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003926#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003927 {
3928 while (VIM_ISWHITE(end_leader[-1]))
3929 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003930 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003931 val = tv2bool(rettv);
3932 else
3933 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003934 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003935 if (error)
3936 {
3937 clear_tv(rettv);
3938 ret = FAIL;
3939 }
3940 else
3941 {
3942 while (end_leader > start_leader)
3943 {
3944 --end_leader;
3945 if (*end_leader == '!')
3946 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003947 if (numeric_only)
3948 {
3949 ++end_leader;
3950 break;
3951 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003952#ifdef FEAT_FLOAT
3953 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003954 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003955 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003956 {
3957 rettv->v_type = VAR_BOOL;
3958 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3959 }
3960 else
3961 f = !f;
3962 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003963 else
3964#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003965 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003966 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003967 type = VAR_BOOL;
3968 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003969 }
3970 else if (*end_leader == '-')
3971 {
3972#ifdef FEAT_FLOAT
3973 if (rettv->v_type == VAR_FLOAT)
3974 f = -f;
3975 else
3976#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003977 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003978 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003979 type = VAR_NUMBER;
3980 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003981 }
3982 }
3983#ifdef FEAT_FLOAT
3984 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003987 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003989 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003990#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003992 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003993 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003994 rettv->v_type = type;
3995 else
3996 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003997 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004000 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return ret;
4002}
4003
4004/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004005 * Call the function referred to in "rettv".
4006 */
4007 static int
4008call_func_rettv(
4009 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004010 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004011 typval_T *rettv,
4012 int evaluate,
4013 dict_T *selfdict,
4014 typval_T *basetv)
4015{
4016 partial_T *pt = NULL;
4017 funcexe_T funcexe;
4018 typval_T functv;
4019 char_u *s;
4020 int ret;
4021
4022 // need to copy the funcref so that we can clear rettv
4023 if (evaluate)
4024 {
4025 functv = *rettv;
4026 rettv->v_type = VAR_UNKNOWN;
4027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004028 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004029 if (functv.v_type == VAR_PARTIAL)
4030 {
4031 pt = functv.vval.v_partial;
4032 s = partial_name(pt);
4033 }
4034 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004035 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004036 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004037 if (s == NULL || *s == NUL)
4038 {
4039 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004040 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004041 goto theend;
4042 }
4043 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004044 }
4045 else
4046 s = (char_u *)"";
4047
Bram Moolenaara80faa82020-04-12 19:37:17 +02004048 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004049 funcexe.fe_firstline = curwin->w_cursor.lnum;
4050 funcexe.fe_lastline = curwin->w_cursor.lnum;
4051 funcexe.fe_evaluate = evaluate;
4052 funcexe.fe_partial = pt;
4053 funcexe.fe_selfdict = selfdict;
4054 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004055 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004056
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004057theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004058 // Clear the funcref afterwards, so that deleting it while
4059 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004060 if (evaluate)
4061 clear_tv(&functv);
4062
4063 return ret;
4064}
4065
4066/*
4067 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004068 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004069 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4070 */
4071 static int
4072eval_lambda(
4073 char_u **arg,
4074 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004075 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004076 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004077{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004078 int evaluate = evalarg != NULL
4079 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004080 typval_T base = *rettv;
4081 int ret;
4082
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004083 rettv->v_type = VAR_UNKNOWN;
4084
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004085 if (**arg == '{')
4086 {
4087 // ->{lambda}()
4088 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4089 }
4090 else
4091 {
4092 // ->(lambda)()
4093 ++*arg;
4094 ret = eval1(arg, rettv, evalarg);
4095 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00004096 if (**arg == ')')
4097 {
4098 ++*arg;
4099 }
4100 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004101 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004102 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004103 ret = FAIL;
4104 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004105 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004106 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004107 return FAIL;
4108 else if (**arg != '(')
4109 {
4110 if (verbose)
4111 {
4112 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004113 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004114 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004115 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004116 }
4117 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004118 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004119 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004120 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004121 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004122
4123 // Clear the funcref afterwards, so that deleting it while
4124 // evaluating the arguments is possible (see test55).
4125 if (evaluate)
4126 clear_tv(&base);
4127
4128 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004129}
4130
4131/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004132 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004133 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004134 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4135 */
4136 static int
4137eval_method(
4138 char_u **arg,
4139 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004140 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004142{
4143 char_u *name;
4144 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004145 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004146 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004147 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004148 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004149 int evaluate = evalarg != NULL
4150 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004151
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004152 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004153
Bram Moolenaarac92e252019-08-03 21:58:38 +02004154 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004155 len = get_name_len(arg, &alias, evaluate, TRUE);
4156 if (alias != NULL)
4157 name = alias;
4158
4159 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004160 {
4161 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004162 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004163 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004164 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004165 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004166 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004167 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004168
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004169 // If there is no "(" immediately following, but there is further on,
4170 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4171 // Does not handle anything where "(" is part of the expression.
4172 *arg = skipwhite(*arg);
4173
4174 if (**arg != '(' && alias == NULL
4175 && (paren = vim_strchr(*arg, '(')) != NULL)
4176 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004177 char_u *deref;
4178
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004179 *arg = name;
4180 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004181 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4182 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004183 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004184 *arg = name + len;
4185 ret = FAIL;
4186 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004187 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004188 {
4189 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004190 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004191 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004192 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004193 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004194
4195 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004196 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004197 *arg = skipwhite(*arg);
4198
4199 if (**arg != '(')
4200 {
4201 if (verbose)
4202 semsg(_(e_missing_parenthesis_str), name);
4203 ret = FAIL;
4204 }
4205 else if (VIM_ISWHITE((*arg)[-1]))
4206 {
4207 if (verbose)
4208 emsg(_(e_no_white_space_allowed_before_parenthesis));
4209 ret = FAIL;
4210 }
4211 else
4212 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004213 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004214 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004215 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004216
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004217 // Clear the funcref afterwards, so that deleting it while
4218 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004219 if (evaluate)
4220 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004221 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004222
Bram Moolenaarac92e252019-08-03 21:58:38 +02004223 return ret;
4224}
4225
4226/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004227 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4228 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4230 */
4231 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004232eval_index(
4233 char_u **arg,
4234 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004235 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004236 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004238 int evaluate = evalarg != NULL
4239 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004240 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004242 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004244 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004245 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4248 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004249
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004250 init_tv(&var1);
4251 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004253 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004254 /*
4255 * dict.name
4256 */
4257 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004259 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004262 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004263 }
4264 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004266 /*
4267 * something[idx]
4268 *
4269 * Get the (first) variable from inside the [].
4270 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004271 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004272 if (**arg == ':')
4273 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004274 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004276 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004277 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004278 semsg(_(e_white_space_required_before_and_after_str_at_str),
4279 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004280 clear_tv(&var1);
4281 return FAIL;
4282 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004283 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004285 int error = FALSE;
4286
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004287#ifdef FEAT_FLOAT
4288 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004289 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004290 && var1.v_type == VAR_FLOAT)
4291 {
4292 var1.vval.v_string = typval_tostring(&var1, TRUE);
4293 var1.v_type = VAR_STRING;
4294 }
4295#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004296 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004297 tv_get_number_chk(&var1, &error);
4298 else
4299 error = tv_get_string_chk(&var1) == NULL;
4300 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004301 {
4302 // not a number or string
4303 clear_tv(&var1);
4304 return FAIL;
4305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004307
4308 /*
4309 * Get the second variable from inside the [:].
4310 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004311 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004312 if (**arg == ':')
4313 {
4314 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004315 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004316 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004317 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004318 semsg(_(e_white_space_required_before_and_after_str_at_str),
4319 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004320 if (!empty1)
4321 clear_tv(&var1);
4322 return FAIL;
4323 }
4324 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 if (**arg == ']')
4326 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004327 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (!empty1)
4330 clear_tv(&var1);
4331 return FAIL;
4332 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004333 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004335 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (!empty1)
4337 clear_tv(&var1);
4338 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 return FAIL;
4340 }
4341 }
4342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004343 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004344 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 if (**arg != ']')
4346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004347 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004348 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 clear_tv(&var1);
4350 if (range)
4351 clear_tv(&var2);
4352 return FAIL;
4353 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004354 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004355 }
4356
4357 if (evaluate)
4358 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004359 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004360 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004361 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004362
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004363 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004365 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004366 clear_tv(&var2);
4367 return res;
4368 }
4369 return OK;
4370}
4371
4372/*
4373 * Check if "rettv" can have an [index] or [sli:ce]
4374 */
4375 int
4376check_can_index(typval_T *rettv, int evaluate, int verbose)
4377{
4378 switch (rettv->v_type)
4379 {
4380 case VAR_FUNC:
4381 case VAR_PARTIAL:
4382 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004383 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004384 return FAIL;
4385 case VAR_FLOAT:
4386#ifdef FEAT_FLOAT
4387 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004388 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004389 return FAIL;
4390#endif
4391 case VAR_BOOL:
4392 case VAR_SPECIAL:
4393 case VAR_JOB:
4394 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004395 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004396 if (verbose)
4397 emsg(_(e_cannot_index_special_variable));
4398 return FAIL;
4399 case VAR_UNKNOWN:
4400 case VAR_ANY:
4401 case VAR_VOID:
4402 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004404 emsg(_(e_cannot_index_special_variable));
4405 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004406 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004407 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004409 case VAR_STRING:
4410 case VAR_LIST:
4411 case VAR_DICT:
4412 case VAR_BLOB:
4413 break;
4414 case VAR_NUMBER:
4415 if (in_vim9script())
4416 emsg(_(e_cannot_index_number));
4417 break;
4418 }
4419 return OK;
4420}
4421
4422/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004423 * slice() function
4424 */
4425 void
4426f_slice(typval_T *argvars, typval_T *rettv)
4427{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004428 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004429 && ((argvars[0].v_type != VAR_STRING
4430 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004431 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004432 && check_for_list_arg(argvars, 0) == FAIL)
4433 || check_for_number_arg(argvars, 1) == FAIL
4434 || check_for_opt_number_arg(argvars, 2) == FAIL))
4435 return;
4436
Bram Moolenaar6601b622021-01-13 21:47:15 +01004437 if (check_can_index(argvars, TRUE, FALSE) == OK)
4438 {
4439 copy_tv(argvars, rettv);
4440 eval_index_inner(rettv, TRUE, argvars + 1,
4441 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4442 TRUE, NULL, 0, FALSE);
4443 }
4444}
4445
4446/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004447 * Apply index or range to "rettv".
4448 * "var1" is the first index, NULL for [:expr].
4449 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004450 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4451 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004452 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4453 */
4454 int
4455eval_index_inner(
4456 typval_T *rettv,
4457 int is_range,
4458 typval_T *var1,
4459 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004460 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004461 char_u *key,
4462 int keylen,
4463 int verbose)
4464{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004465 varnumber_T n1, n2 = 0;
4466 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004467
4468 n1 = 0;
4469 if (var1 != NULL && rettv->v_type != VAR_DICT)
4470 n1 = tv_get_number(var1);
4471
4472 if (is_range)
4473 {
4474 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004476 if (verbose)
4477 emsg(_(e_cannot_slice_dictionary));
4478 return FAIL;
4479 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004480 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004481 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004482 else
4483 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004484 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004485
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004486 switch (rettv->v_type)
4487 {
4488 case VAR_UNKNOWN:
4489 case VAR_ANY:
4490 case VAR_VOID:
4491 case VAR_FUNC:
4492 case VAR_PARTIAL:
4493 case VAR_FLOAT:
4494 case VAR_BOOL:
4495 case VAR_SPECIAL:
4496 case VAR_JOB:
4497 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004498 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004499 break; // not evaluating, skipping over subscript
4500
4501 case VAR_NUMBER:
4502 case VAR_STRING:
4503 {
4504 char_u *s = tv_get_string(rettv);
4505
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004507 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004508 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004509 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004510 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004511 else
4512 s = char_from_string(s, n1);
4513 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004514 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 // The resulting variable is a substring. If the indexes
4517 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004518 if (n1 < 0)
4519 {
4520 n1 = len + n1;
4521 if (n1 < 0)
4522 n1 = 0;
4523 }
4524 if (n2 < 0)
4525 n2 = len + n2;
4526 else if (n2 >= len)
4527 n2 = len;
4528 if (n1 >= len || n2 < 0 || n1 > n2)
4529 s = NULL;
4530 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004531 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 }
4533 else
4534 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // The resulting variable is a string of a single
4536 // character. If the index is too big or negative the
4537 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538 if (n1 >= len || n1 < 0)
4539 s = NULL;
4540 else
4541 s = vim_strnsave(s + n1, 1);
4542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 clear_tv(rettv);
4544 rettv->v_type = VAR_STRING;
4545 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004546 }
4547 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004549 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004550 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4551 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004552 break;
4553
4554 case VAR_LIST:
4555 if (var1 == NULL)
4556 n1 = 0;
4557 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004558 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004559 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004560 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004561 return FAIL;
4562 break;
4563
4564 case VAR_DICT:
4565 {
4566 dictitem_T *item;
4567 typval_T tmp;
4568
4569 if (key == NULL)
4570 {
4571 key = tv_get_string_chk(var1);
4572 if (key == NULL)
4573 return FAIL;
4574 }
4575
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004576 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004577
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004578 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004579 {
4580 if (verbose)
4581 {
4582 if (keylen > 0)
4583 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004584 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004585 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004587 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004588
4589 copy_tv(&item->di_tv, &tmp);
4590 clear_tv(rettv);
4591 *rettv = tmp;
4592 }
4593 break;
4594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 return OK;
4596}
4597
4598/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004599 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004600 */
4601 char_u *
4602partial_name(partial_T *pt)
4603{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004604 if (pt != NULL)
4605 {
4606 if (pt->pt_name != NULL)
4607 return pt->pt_name;
4608 if (pt->pt_func != NULL)
4609 return pt->pt_func->uf_name;
4610 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004611 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004612}
4613
Bram Moolenaarddecc252016-04-06 22:59:37 +02004614 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004615partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004616{
4617 int i;
4618
4619 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004620 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004621 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004622 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004623 if (pt->pt_name != NULL)
4624 {
4625 func_unref(pt->pt_name);
4626 vim_free(pt->pt_name);
4627 }
4628 else
4629 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004630
Bram Moolenaar54656012021-06-09 20:50:46 +02004631 // "out_up" is no longer used, decrement refcount on partial that owns it.
4632 partial_unref(pt->pt_outer.out_up_partial);
4633
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004634 // Using pt_outer from another partial.
4635 partial_unref(pt->pt_outer_partial);
4636
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004637 // Decrease the reference count for the context of a closure. If down
4638 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004639 if (pt->pt_funcstack != NULL)
4640 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004641 --pt->pt_funcstack->fs_refcount;
4642 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004643 }
4644
Bram Moolenaarddecc252016-04-06 22:59:37 +02004645 vim_free(pt);
4646}
4647
4648/*
4649 * Unreference a closure: decrement the reference count and free it when it
4650 * becomes zero.
4651 */
4652 void
4653partial_unref(partial_T *pt)
4654{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004655 if (pt != NULL)
4656 {
4657 if (--pt->pt_refcount <= 0)
4658 partial_free(pt);
4659
4660 // If the reference count goes down to one, the funcstack may be the
4661 // only reference and can be freed if no other partials reference it.
4662 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4663 funcstack_check_refcount(pt->pt_funcstack);
4664 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004665}
4666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004667/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004668 * Return the next (unique) copy ID.
4669 * Used for serializing nested structures.
4670 */
4671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004672get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004673{
4674 current_copyID += COPYID_INC;
4675 return current_copyID;
4676}
4677
4678/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004679 * Garbage collection for lists and dictionaries.
4680 *
4681 * We use reference counts to be able to free most items right away when they
4682 * are no longer used. But for composite items it's possible that it becomes
4683 * unused while the reference count is > 0: When there is a recursive
4684 * reference. Example:
4685 * :let l = [1, 2, 3]
4686 * :let d = {9: l}
4687 * :let l[1] = d
4688 *
4689 * Since this is quite unusual we handle this with garbage collection: every
4690 * once in a while find out which lists and dicts are not referenced from any
4691 * variable.
4692 *
4693 * Here is a good reference text about garbage collection (refers to Python
4694 * but it applies to all reference-counting mechanisms):
4695 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004696 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004697
4698/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004699 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004700 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004701 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004702 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004703 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004704garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004705{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004706 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004708 buf_T *buf;
4709 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004710 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004711 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004712
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004713 if (!testing)
4714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004715 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004716 want_garbage_collect = FALSE;
4717 may_garbage_collect = FALSE;
4718 garbage_collect_at_exit = FALSE;
4719 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004720
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004721 // The execution stack can grow big, limit the size.
4722 if (exestack.ga_maxlen - exestack.ga_len > 500)
4723 {
4724 size_t new_len;
4725 char_u *pp;
4726 int n;
4727
4728 // Keep 150% of the current size, with a minimum of the growth size.
4729 n = exestack.ga_len / 2;
4730 if (n < exestack.ga_growsize)
4731 n = exestack.ga_growsize;
4732
4733 // Don't make it bigger though.
4734 if (exestack.ga_len + n < exestack.ga_maxlen)
4735 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004736 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004737 pp = vim_realloc(exestack.ga_data, new_len);
4738 if (pp == NULL)
4739 return FAIL;
4740 exestack.ga_maxlen = exestack.ga_len + n;
4741 exestack.ga_data = pp;
4742 }
4743 }
4744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004745 // We advance by two because we add one for items referenced through
4746 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004747 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004748
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004749 /*
4750 * 1. Go through all accessible variables and mark all lists and dicts
4751 * with copyID.
4752 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // Don't free variables in the previous_funccal list unless they are only
4755 // referenced through previous_funccal. This must be first, because if
4756 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004757 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004760 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004763 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004764 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4765 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004768 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004769 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4770 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004771 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004772 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4773 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004774#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004775 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004776 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4777 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004778 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004779 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004780 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4781 NULL, NULL);
4782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004785 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004786 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4787 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004788 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004789 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004795 abort = abort || set_ref_in_functions(copyID);
4796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004798 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004799
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004800 // funcstacks keep variables for closures
4801 abort = abort || set_ref_in_funcstacks(copyID);
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004804 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004805
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004806 // callbacks in buffers
4807 abort = abort || set_ref_in_buffers(copyID);
4808
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004809 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4810 abort = abort || set_ref_in_insexpand_funcs(copyID);
4811
4812 // 'operatorfunc' callback
4813 abort = abort || set_ref_in_opfunc(copyID);
4814
4815 // 'tagfunc' callback
4816 abort = abort || set_ref_in_tagfunc(copyID);
4817
4818 // 'imactivatefunc' and 'imstatusfunc' callbacks
4819 abort = abort || set_ref_in_im_funcs(copyID);
4820
Bram Moolenaar1dced572012-04-05 16:54:08 +02004821#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004822 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004823#endif
4824
Bram Moolenaardb913952012-06-29 12:54:53 +02004825#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004826 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004827#endif
4828
4829#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004830 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004831#endif
4832
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004833#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004834 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004835 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004836#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004837#ifdef FEAT_NETBEANS_INTG
4838 abort = abort || set_ref_in_nb_channel(copyID);
4839#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004840
Bram Moolenaare3188e22016-05-31 21:13:04 +02004841#ifdef FEAT_TIMERS
4842 abort = abort || set_ref_in_timer(copyID);
4843#endif
4844
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004845#ifdef FEAT_QUICKFIX
4846 abort = abort || set_ref_in_quickfix(copyID);
4847#endif
4848
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004849#ifdef FEAT_TERMINAL
4850 abort = abort || set_ref_in_term(copyID);
4851#endif
4852
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004853#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004854 abort = abort || set_ref_in_popups(copyID);
4855#endif
4856
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004857 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004858 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004859 /*
4860 * 2. Free lists and dictionaries that are not referenced.
4861 */
4862 did_free = free_unref_items(copyID);
4863
4864 /*
4865 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004867 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004869 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004870 else if (p_verbose > 0)
4871 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004872 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004873 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004874
4875 return did_free;
4876}
4877
4878/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004879 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004880 */
4881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004882free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004883{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004884 int did_free = FALSE;
4885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // Let all "free" functions know that we are here. This means no
4887 // dictionaries, lists, channels or jobs are to be freed, because we will
4888 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004889 in_free_unref_items = TRUE;
4890
4891 /*
4892 * PASS 1: free the contents of the items. We don't free the items
4893 * themselves yet, so that it is possible to decrement refcount counters
4894 */
4895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004896 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004897 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004900 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901
4902#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // Go through the list of jobs and free items without the copyID. This
4904 // must happen before doing channels, because jobs refer to channels, but
4905 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004906 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004909 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4910#endif
4911
4912 /*
4913 * PASS 2: free the items themselves.
4914 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004915 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004916 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004917
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004919 // Go through the list of jobs and free items without the copyID. This
4920 // must happen before doing channels, because jobs refer to channels, but
4921 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004922 free_unused_jobs(copyID, COPYID_MASK);
4923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004925 free_unused_channels(copyID, COPYID_MASK);
4926#endif
4927
4928 in_free_unref_items = FALSE;
4929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004930 return did_free;
4931}
4932
4933/*
4934 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004935 * "list_stack" is used to add lists to be marked. Can be NULL.
4936 *
4937 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004938 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004940set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004941{
4942 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004943 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004945 hashtab_T *cur_ht;
4946 ht_stack_T *ht_stack = NULL;
4947 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004948
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004949 cur_ht = ht;
4950 for (;;)
4951 {
4952 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Mark each item in the hashtab. If the item contains a hashtab
4955 // it is added to ht_stack, if it contains a list it is added to
4956 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004957 todo = (int)cur_ht->ht_used;
4958 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4959 if (!HASHITEM_EMPTY(hi))
4960 {
4961 --todo;
4962 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4963 &ht_stack, list_stack);
4964 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004965 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004966
4967 if (ht_stack == NULL)
4968 break;
4969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004971 cur_ht = ht_stack->ht;
4972 tempitem = ht_stack;
4973 ht_stack = ht_stack->prev;
4974 free(tempitem);
4975 }
4976
4977 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004978}
4979
Dominique Pelle748b3082022-01-08 12:41:16 +00004980#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4981 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004982/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004983 * Mark a dict and its items with "copyID".
4984 * Returns TRUE if setting references failed somehow.
4985 */
4986 int
4987set_ref_in_dict(dict_T *d, int copyID)
4988{
4989 if (d != NULL && d->dv_copyID != copyID)
4990 {
4991 d->dv_copyID = copyID;
4992 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4993 }
4994 return FALSE;
4995}
Dominique Pelle748b3082022-01-08 12:41:16 +00004996#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004997
4998/*
4999 * Mark a list and its items with "copyID".
5000 * Returns TRUE if setting references failed somehow.
5001 */
5002 int
5003set_ref_in_list(list_T *ll, int copyID)
5004{
5005 if (ll != NULL && ll->lv_copyID != copyID)
5006 {
5007 ll->lv_copyID = copyID;
5008 return set_ref_in_list_items(ll, copyID, NULL);
5009 }
5010 return FALSE;
5011}
5012
5013/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005014 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005015 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5016 *
5017 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005018 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005019 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005020set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005021{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005022 listitem_T *li;
5023 int abort = FALSE;
5024 list_T *cur_l;
5025 list_stack_T *list_stack = NULL;
5026 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005027
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005028 cur_l = l;
5029 for (;;)
5030 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005031 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // Mark each item in the list. If the item contains a hashtab
5033 // it is added to ht_stack, if it contains a list it is added to
5034 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005035 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5036 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5037 ht_stack, &list_stack);
5038 if (list_stack == NULL)
5039 break;
5040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005042 cur_l = list_stack->list;
5043 tempitem = list_stack;
5044 list_stack = list_stack->prev;
5045 free(tempitem);
5046 }
5047
5048 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005049}
5050
5051/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005052 * Mark the partial in callback 'cb' with "copyID".
5053 */
5054 int
5055set_ref_in_callback(callback_T *cb, int copyID)
5056{
5057 typval_T tv;
5058
5059 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5060 return FALSE;
5061
5062 tv.v_type = VAR_PARTIAL;
5063 tv.vval.v_partial = cb->cb_partial;
5064 return set_ref_in_item(&tv, copyID, NULL, NULL);
5065}
5066
5067/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005068 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005069 * "list_stack" is used to add lists to be marked. Can be NULL.
5070 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5071 *
5072 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005073 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005075set_ref_in_item(
5076 typval_T *tv,
5077 int copyID,
5078 ht_stack_T **ht_stack,
5079 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005080{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005082
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005083 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005084 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005085 dict_T *dd = tv->vval.v_dict;
5086
Bram Moolenaara03f2332016-02-06 18:09:59 +01005087 if (dd != NULL && dd->dv_copyID != copyID)
5088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005090 dd->dv_copyID = copyID;
5091 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005092 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005093 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5094 }
5095 else
5096 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005097 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5098
Bram Moolenaara03f2332016-02-06 18:09:59 +01005099 if (newitem == NULL)
5100 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005101 else
5102 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005103 newitem->ht = &dd->dv_hashtab;
5104 newitem->prev = *ht_stack;
5105 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005106 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005107 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005108 }
5109 }
5110 else if (tv->v_type == VAR_LIST)
5111 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 list_T *ll = tv->vval.v_list;
5113
Bram Moolenaara03f2332016-02-06 18:09:59 +01005114 if (ll != NULL && ll->lv_copyID != copyID)
5115 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005117 ll->lv_copyID = copyID;
5118 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005119 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005120 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005121 }
5122 else
5123 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005124 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5125
Bram Moolenaara03f2332016-02-06 18:09:59 +01005126 if (newitem == NULL)
5127 abort = TRUE;
5128 else
5129 {
5130 newitem->list = ll;
5131 newitem->prev = *list_stack;
5132 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005133 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005134 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005135 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005136 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005137 else if (tv->v_type == VAR_FUNC)
5138 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005139 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005140 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005141 else if (tv->v_type == VAR_PARTIAL)
5142 {
5143 partial_T *pt = tv->vval.v_partial;
5144 int i;
5145
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005146 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005147 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005148 // Didn't see this partial yet.
5149 pt->pt_copyID = copyID;
5150
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005151 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005152
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005153 if (pt->pt_dict != NULL)
5154 {
5155 typval_T dtv;
5156
5157 dtv.v_type = VAR_DICT;
5158 dtv.vval.v_dict = pt->pt_dict;
5159 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5160 }
5161
5162 for (i = 0; i < pt->pt_argc; ++i)
5163 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5164 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005165 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166 }
5167 }
5168#ifdef FEAT_JOB_CHANNEL
5169 else if (tv->v_type == VAR_JOB)
5170 {
5171 job_T *job = tv->vval.v_job;
5172 typval_T dtv;
5173
5174 if (job != NULL && job->jv_copyID != copyID)
5175 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005176 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005177 if (job->jv_channel != NULL)
5178 {
5179 dtv.v_type = VAR_CHANNEL;
5180 dtv.vval.v_channel = job->jv_channel;
5181 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5182 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005183 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005184 {
5185 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005186 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005187 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5188 }
5189 }
5190 }
5191 else if (tv->v_type == VAR_CHANNEL)
5192 {
5193 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005194 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005195 typval_T dtv;
5196 jsonq_T *jq;
5197 cbq_T *cq;
5198
5199 if (ch != NULL && ch->ch_copyID != copyID)
5200 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005201 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005202 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005203 {
5204 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5205 jq = jq->jq_next)
5206 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5207 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5208 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005209 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005210 {
5211 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005212 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005213 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5214 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005215 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005216 {
5217 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005218 dtv.vval.v_partial =
5219 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005220 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5221 }
5222 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005223 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005224 {
5225 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005226 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005227 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5228 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005229 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005230 {
5231 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005232 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005233 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5234 }
5235 }
5236 }
5237#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005238 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005239}
5240
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 * Return a string with the string representation of a variable.
5243 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005244 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005245 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005246 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005247 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005248 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005249 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5250 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005251 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005253 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005254echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005255 typval_T *tv,
5256 char_u **tofree,
5257 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005258 int copyID,
5259 int echo_style,
5260 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005261 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005263 static int recurse = 0;
5264 char_u *r = NULL;
5265
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005268 if (!did_echo_string_emsg)
5269 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005270 // Only give this message once for a recursive call to avoid
5271 // flooding the user with errors. And stop iterating over lists
5272 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005273 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005274 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005275 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005276 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005277 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005278 }
5279 ++recurse;
5280
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 switch (tv->v_type)
5282 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005283 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005284 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005285 {
5286 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005287 r = tv->vval.v_string;
5288 if (r == NULL)
5289 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005290 }
5291 else
5292 {
5293 *tofree = string_quote(tv->vval.v_string, FALSE);
5294 r = *tofree;
5295 }
5296 break;
5297
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005299 if (echo_style)
5300 {
5301 *tofree = NULL;
5302 r = tv->vval.v_string;
5303 }
5304 else
5305 {
5306 *tofree = string_quote(tv->vval.v_string, TRUE);
5307 r = *tofree;
5308 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005309 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005310
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005311 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005312 {
5313 partial_T *pt = tv->vval.v_partial;
5314 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005315 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005316 garray_T ga;
5317 int i;
5318 char_u *tf;
5319
5320 ga_init2(&ga, 1, 100);
5321 ga_concat(&ga, (char_u *)"function(");
5322 if (fname != NULL)
5323 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005324 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005325 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005326 && vim_isupper(fname[1]))
5327 {
5328 ga_concat(&ga, (char_u *)"'g:");
5329 ga_concat(&ga, fname + 1);
5330 }
5331 else
5332 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005333 vim_free(fname);
5334 }
5335 if (pt != NULL && pt->pt_argc > 0)
5336 {
5337 ga_concat(&ga, (char_u *)", [");
5338 for (i = 0; i < pt->pt_argc; ++i)
5339 {
5340 if (i > 0)
5341 ga_concat(&ga, (char_u *)", ");
5342 ga_concat(&ga,
5343 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5344 vim_free(tf);
5345 }
5346 ga_concat(&ga, (char_u *)"]");
5347 }
5348 if (pt != NULL && pt->pt_dict != NULL)
5349 {
5350 typval_T dtv;
5351
5352 ga_concat(&ga, (char_u *)", ");
5353 dtv.v_type = VAR_DICT;
5354 dtv.vval.v_dict = pt->pt_dict;
5355 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5356 vim_free(tf);
5357 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005358 // terminate with ')' and a NUL
5359 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005360
5361 *tofree = ga.ga_data;
5362 r = *tofree;
5363 break;
5364 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005365
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005366 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005367 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005368 break;
5369
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005371 if (tv->vval.v_list == NULL)
5372 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005373 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005374 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005375 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005376 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005377 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5378 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005379 {
5380 *tofree = NULL;
5381 r = (char_u *)"[...]";
5382 }
5383 else
5384 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005385 int old_copyID = tv->vval.v_list->lv_copyID;
5386
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005387 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005388 *tofree = list2string(tv, copyID, restore_copyID);
5389 if (restore_copyID)
5390 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005391 r = *tofree;
5392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005393 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005394
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005396 if (tv->vval.v_dict == NULL)
5397 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005398 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005399 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005400 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005401 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005402 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5403 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005404 {
5405 *tofree = NULL;
5406 r = (char_u *)"{...}";
5407 }
5408 else
5409 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005410 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005411
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005412 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005413 *tofree = dict2string(tv, copyID, restore_copyID);
5414 if (restore_copyID)
5415 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005416 r = *tofree;
5417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005418 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005422 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005424 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005425 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005426 break;
5427
Bram Moolenaar835dc632016-02-07 14:27:38 +01005428 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005429 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005430#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005431 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005432 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5433 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005434 if (composite_val)
5435 {
5436 *tofree = string_quote(r, FALSE);
5437 r = *tofree;
5438 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005439#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005441
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005442 case VAR_INSTR:
5443 *tofree = NULL;
5444 r = (char_u *)"instructions";
5445 break;
5446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005447 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005448#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005449 *tofree = NULL;
5450 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5451 r = numbuf;
5452 break;
5453#endif
5454
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005455 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005456 case VAR_SPECIAL:
5457 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005458 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005459 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005460 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005461
Bram Moolenaar8502c702014-06-17 12:51:16 +02005462 if (--recurse == 0)
5463 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005464 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005465}
5466
5467/*
5468 * Return a string with the string representation of a variable.
5469 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5470 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005471 * Does not put quotes around strings, as ":echo" displays values.
5472 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5473 * May return NULL.
5474 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005476echo_string(
5477 typval_T *tv,
5478 char_u **tofree,
5479 char_u *numbuf,
5480 int copyID)
5481{
5482 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5483}
5484
5485/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005486 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5487 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005488 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005489 */
5490 int
5491buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5492{
5493 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005494 char_u *t;
5495 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005496
5497 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5498 return -1;
5499
5500 if (lnum > buf->b_ml.ml_line_count)
5501 lnum = buf->b_ml.ml_line_count;
5502
5503 str = ml_get_buf(buf, lnum, FALSE);
5504 if (str == NULL)
5505 return -1;
5506
5507 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005508 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005509
Bram Moolenaar91458462021-01-13 20:08:38 +01005510 // count the number of characters
5511 t = str;
5512 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5513 t += mb_ptr2len(t);
5514
5515 // In insert mode, when the cursor is at the end of a non-empty line,
5516 // byteidx points to the NUL character immediately past the end of the
5517 // string. In this case, add one to the character count.
5518 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5519 count++;
5520
5521 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005522}
5523
5524/*
5525 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005526 * byte index. Works only for loaded buffers. Returns -1 on failure.
5527 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005528 */
5529 int
5530buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5531{
5532 char_u *str;
5533 char_u *t;
5534
5535 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5536 return -1;
5537
5538 if (lnum > buf->b_ml.ml_line_count)
5539 lnum = buf->b_ml.ml_line_count;
5540
5541 str = ml_get_buf(buf, lnum, FALSE);
5542 if (str == NULL)
5543 return -1;
5544
5545 // Convert the character offset to a byte offset
5546 t = str;
5547 while (*t != NUL && --charidx > 0)
5548 t += mb_ptr2len(t);
5549
Bram Moolenaar91458462021-01-13 20:08:38 +01005550 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005551}
5552
5553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005555 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005558var2fpos(
5559 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005561 int *fnum, // set to fnum for '0, 'A, etc.
5562 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005564 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005566 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005569 if (varp->v_type == VAR_LIST)
5570 {
5571 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005572 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005573 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005574 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005575
5576 l = varp->vval.v_list;
5577 if (l == NULL)
5578 return NULL;
5579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005580 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005581 pos.lnum = list_find_nr(l, 0L, &error);
5582 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005584 if (charcol)
5585 len = (long)mb_charlen(ml_get(pos.lnum));
5586 else
5587 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005588
Bram Moolenaarec65d772020-08-20 22:29:12 +02005589 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005591 li = list_find(l, 1L);
5592 if (li != NULL && li->li_tv.v_type == VAR_STRING
5593 && li->li_tv.vval.v_string != NULL
5594 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005595 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005596 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005597 }
5598 else
5599 {
5600 pos.col = list_find_nr(l, 1L, &error);
5601 if (error)
5602 return NULL;
5603 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005606 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005607 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005608 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005610 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005611 pos.coladd = list_find_nr(l, 2L, &error);
5612 if (error)
5613 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005614
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005615 return &pos;
5616 }
5617
Bram Moolenaarc5809432021-03-27 21:23:30 +01005618 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5619 return NULL;
5620
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005622 if (name == NULL)
5623 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005624 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005625 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005626 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005627 pos = curwin->w_cursor;
5628 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005629 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005630 return &pos;
5631 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005632 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005633 {
5634 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005635 pos = VIsual;
5636 else
5637 pos = curwin->w_cursor;
5638 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005639 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005640 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005641 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005642 if (name[0] == '\'' && (!in_vim9script()
5643 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005645 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005646 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5648 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005649 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005650 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 return pp;
5652 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005653
Bram Moolenaara5525202006-03-02 22:52:09 +00005654 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005655
Bram Moolenaar477933c2007-07-17 14:32:23 +00005656 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005657 {
5658 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005660 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005661 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // In silent Ex mode topline is zero, but that's not a valid line
5663 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005664 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005665 return &pos;
5666 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005668 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005669 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005670 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005671 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005672 return &pos;
5673 }
5674 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005677 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
5679 pos.lnum = curbuf->b_ml.ml_line_count;
5680 pos.col = 0;
5681 }
5682 else
5683 {
5684 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005685 if (charcol)
5686 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5687 else
5688 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 return &pos;
5691 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005692 if (in_vim9script())
5693 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 return NULL;
5695}
5696
5697/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005698 * Convert list in "arg" into a position and optional file number.
5699 * When "fnump" is NULL there is no file number, only 3 items.
5700 * Note that the column is passed on as-is, the caller may want to decrement
5701 * it to use 1 for the first column.
5702 * Return FAIL when conversion is not possible, doesn't check the position for
5703 * validity.
5704 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706list2fpos(
5707 typval_T *arg,
5708 pos_T *posp,
5709 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005710 colnr_T *curswantp,
5711 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005712{
5713 list_T *l = arg->vval.v_list;
5714 long i = 0;
5715 long n;
5716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5718 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005719 if (arg->v_type != VAR_LIST
5720 || l == NULL
5721 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005722 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005723 return FAIL;
5724
5725 if (fnump != NULL)
5726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005727 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005728 if (n < 0)
5729 return FAIL;
5730 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005731 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005732 *fnump = n;
5733 }
5734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005735 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005736 if (n < 0)
5737 return FAIL;
5738 posp->lnum = n;
5739
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005740 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005741 if (n < 0)
5742 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005743 // If character position is specified, then convert to byte position
5744 if (charcol)
5745 {
5746 buf_T *buf;
5747
5748 // Get the text for the specified line in a loaded buffer
5749 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5750 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5751 return FAIL;
5752
Bram Moolenaar91458462021-01-13 20:08:38 +01005753 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005754 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005755 posp->col = n;
5756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005757 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005758 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005759 posp->coladd = 0;
5760 else
5761 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005762
Bram Moolenaar493c1782014-05-28 14:34:46 +02005763 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005764 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005765
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005766 return OK;
5767}
5768
5769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 * Get the length of an environment variable name.
5771 * Advance "arg" to the first character after the name.
5772 * Return 0 for error.
5773 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005775get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 char_u *p;
5778 int len;
5779
5780 for (p = *arg; vim_isIDc(*p); ++p)
5781 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 return 0;
5784
5785 len = (int)(p - *arg);
5786 *arg = p;
5787 return len;
5788}
5789
5790/*
5791 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005792 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * Return 0 if something is wrong.
5794 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005796get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 char_u *p;
5799 int len;
5800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005803 {
5804 if (*p == ':')
5805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // "s:" is start of "s:var", but "n:" is not and can be used in
5807 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005808 len = (int)(p - *arg);
5809 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5810 || len > 1)
5811 break;
5812 }
5813 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 return 0;
5816
5817 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005818 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 return len;
5821}
5822
5823/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005824 * Get the length of the name of a variable or function.
5825 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005827 * Return -1 if curly braces expansion failed.
5828 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 * If the name contains 'magic' {}'s, expand them and return the
5830 * expanded name in an allocated string via 'alias' - caller must free.
5831 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005833get_name_len(
5834 char_u **arg,
5835 char_u **alias,
5836 int evaluate,
5837 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 char_u *p;
5841 char_u *expr_start;
5842 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005844 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5847 && (*arg)[2] == (int)KE_SNR)
5848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005849 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 *arg += 3;
5851 return get_id_len(arg) + 3;
5852 }
5853 len = eval_fname_script(*arg);
5854 if (len > 0)
5855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 *arg += len;
5858 }
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005861 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005863 p = find_name_end(*arg, &expr_start, &expr_end,
5864 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 if (expr_start != NULL)
5866 {
5867 char_u *temp_string;
5868
5869 if (!evaluate)
5870 {
5871 len += (int)(p - *arg);
5872 *arg = skipwhite(p);
5873 return len;
5874 }
5875
5876 /*
5877 * Include any <SID> etc in the expanded string:
5878 * Thus the -len here.
5879 */
5880 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5881 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005882 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 *alias = temp_string;
5884 *arg = skipwhite(p);
5885 return (int)STRLEN(temp_string);
5886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
5888 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005889 // Only give an error when there is something, otherwise it will be
5890 // reported at a higher level.
5891 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005892 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 return len;
5895}
5896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005897/*
5898 * Find the end of a variable or function name, taking care of magic braces.
5899 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5900 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005901 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005902 * Return a pointer to just after the name. Equal to "arg" if there is no
5903 * valid name.
5904 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906find_name_end(
5907 char_u *arg,
5908 char_u **expr_start,
5909 char_u **expr_end,
5910 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005912 int mb_nest = 0;
5913 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005915 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005916 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005918 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005920 *expr_start = NULL;
5921 *expr_end = NULL;
5922 }
5923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005924 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005925 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5926 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005927 return arg;
5928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005929 for (p = arg; *p != NUL
5930 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005931 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005932 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005933 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005935 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005936 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005937 if (*p == '\'')
5938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005939 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005940 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005941 ;
5942 if (*p == NUL)
5943 break;
5944 }
5945 else if (*p == '"')
5946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005947 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005948 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005949 if (*p == '\\' && p[1] != NUL)
5950 ++p;
5951 if (*p == NUL)
5952 break;
5953 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005954 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005956 // "s:" is start of "s:var", but "n:" is not and can be used in
5957 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005958 len = (int)(p - arg);
5959 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005960 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005961 break;
5962 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005966 if (*p == '[')
5967 ++br_nest;
5968 else if (*p == ']')
5969 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005971
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005972 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 if (*p == '{')
5975 {
5976 mb_nest++;
5977 if (expr_start != NULL && *expr_start == NULL)
5978 *expr_start = p;
5979 }
5980 else if (*p == '}')
5981 {
5982 mb_nest--;
5983 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5984 *expr_end = p;
5985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988
5989 return p;
5990}
5991
5992/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005993 * Expands out the 'magic' {}'s in a variable/function name.
5994 * Note that this can call itself recursively, to deal with
5995 * constructs like foo{bar}{baz}{bam}
5996 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5997 * "in_start" ^
5998 * "expr_start" ^
5999 * "expr_end" ^
6000 * "in_end" ^
6001 *
6002 * Returns a new allocated string, which the caller must free.
6003 * Returns NULL for failure.
6004 */
6005 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006make_expanded_name(
6007 char_u *in_start,
6008 char_u *expr_start,
6009 char_u *expr_end,
6010 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006011{
6012 char_u c1;
6013 char_u *retval = NULL;
6014 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006015
6016 if (expr_end == NULL || in_end == NULL)
6017 return NULL;
6018 *expr_start = NUL;
6019 *expr_end = NUL;
6020 c1 = *in_end;
6021 *in_end = NUL;
6022
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006023 temp_result = eval_to_string(expr_start + 1, FALSE);
6024 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006025 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006026 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6027 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006028 if (retval != NULL)
6029 {
6030 STRCPY(retval, in_start);
6031 STRCAT(retval, temp_result);
6032 STRCAT(retval, expr_end + 1);
6033 }
6034 }
6035 vim_free(temp_result);
6036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006037 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006038 *expr_start = '{';
6039 *expr_end = '}';
6040
6041 if (retval != NULL)
6042 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006043 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006044 if (expr_start != NULL)
6045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006047 temp_result = make_expanded_name(retval, expr_start,
6048 expr_end, temp_result);
6049 vim_free(retval);
6050 retval = temp_result;
6051 }
6052 }
6053
6054 return retval;
6055}
6056
6057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006059 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006064 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006065}
6066
6067/*
6068 * Return TRUE if character "c" can be used as the first character in a
6069 * variable or function name (excluding '{' and '}').
6070 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006073{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006074 return ASCII_ISALPHA(c) || c == '_';
6075}
6076
6077/*
6078 * Return TRUE if character "c" can be used as the first character of a
6079 * dictionary key.
6080 */
6081 int
6082eval_isdictc(int c)
6083{
6084 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085}
6086
6087/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006088 * Handle:
6089 * - expr[expr], expr[expr:expr] subscript
6090 * - ".name" lookup
6091 * - function call with Funcref variable: func(expr)
6092 * - method call: var->method()
6093 *
6094 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006095 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098handle_subscript(
6099 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006100 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006102 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006103 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006104{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006105 int evaluate = evalarg != NULL
6106 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006107 int ret = OK;
6108 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006109 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006110 int getnext;
6111 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006112
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006113 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006114 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006115 // When at the end of the line and ".name" or "->{" or "->X" follows in
6116 // the next line then consume the line break.
6117 p = eval_next_non_blank(*arg, evalarg, &getnext);
6118 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006119 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006120 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6121 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6122 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006123 {
6124 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006125 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006126 check_white = FALSE;
6127 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006128
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006129 if (rettv->v_type == VAR_ANY)
6130 {
6131 char_u *exp_name;
6132 int cc;
6133 int idx;
6134 ufunc_T *ufunc;
6135 type_T *type;
6136
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006137 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006138 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006139 if (**arg != '.')
6140 {
6141 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006142 semsg(_(e_expected_dot_after_name_str),
6143 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006144 ret = FAIL;
6145 break;
6146 }
6147 ++*arg;
6148 if (IS_WHITE_OR_NUL(**arg))
6149 {
6150 if (verbose)
6151 emsg(_(e_no_white_space_allowed_after_dot));
6152 ret = FAIL;
6153 break;
6154 }
6155
6156 // isolate the name
6157 exp_name = *arg;
6158 while (eval_isnamec(**arg))
6159 ++*arg;
6160 cc = **arg;
6161 **arg = NUL;
6162
6163 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006164 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006165 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006166
6167 if (idx < 0 && ufunc == NULL)
6168 {
6169 ret = FAIL;
6170 break;
6171 }
6172 if (idx >= 0)
6173 {
6174 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6175 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6176
6177 copy_tv(sv->sv_tv, rettv);
6178 }
6179 else
6180 {
6181 rettv->v_type = VAR_FUNC;
6182 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6183 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006184 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006185 }
6186
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006187 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6188 || rettv->v_type == VAR_PARTIAL))
6189 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006190 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006191 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6192 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006193
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006194 // Stop the expression evaluation when immediately aborting on
6195 // error, or when an interrupt occurred or an exception was thrown
6196 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006197 if (aborting())
6198 {
6199 if (ret == OK)
6200 clear_tv(rettv);
6201 ret = FAIL;
6202 }
6203 dict_unref(selfdict);
6204 selfdict = NULL;
6205 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006206 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006207 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006208 if (in_vim9script())
6209 *arg = skipwhite(p + 2);
6210 else
6211 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006212 if (ret == OK)
6213 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006214 if (VIM_ISWHITE(**arg))
6215 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006216 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006217 ret = FAIL;
6218 }
6219 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006220 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006221 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006222 else
6223 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006224 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006225 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006226 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006227 // "." is ".name" lookup when we found a dict or when evaluating and
6228 // scriptversion is at least 2, where string concatenation is "..".
6229 else if (**arg == '['
6230 || (**arg == '.' && (rettv->v_type == VAR_DICT
6231 || (!evaluate
6232 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006233 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006234 {
6235 dict_unref(selfdict);
6236 if (rettv->v_type == VAR_DICT)
6237 {
6238 selfdict = rettv->vval.v_dict;
6239 if (selfdict != NULL)
6240 ++selfdict->dv_refcount;
6241 }
6242 else
6243 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006244 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006245 {
6246 clear_tv(rettv);
6247 ret = FAIL;
6248 }
6249 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006250 else
6251 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006252 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006254 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6255 // Don't do this when "Func" is already a partial that was bound
6256 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006257 if (selfdict != NULL
6258 && (rettv->v_type == VAR_FUNC
6259 || (rettv->v_type == VAR_PARTIAL
6260 && (rettv->vval.v_partial->pt_auto
6261 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006262 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006263
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006264 dict_unref(selfdict);
6265 return ret;
6266}
6267
6268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006269 * Make a copy of an item.
6270 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006271 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006272 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6273 * reference to an already copied list/dict can be used.
6274 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006277item_copy(
6278 typval_T *from,
6279 typval_T *to,
6280 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006281 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006283{
6284 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006285 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006289 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006290 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006291 }
6292 ++recurse;
6293
6294 switch (from->v_type)
6295 {
6296 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006297 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298 case VAR_STRING:
6299 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006300 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006301 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006302 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006303 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006304 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006305 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 copy_tv(from, to);
6307 break;
6308 case VAR_LIST:
6309 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006310 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006311 if (from->vval.v_list == NULL)
6312 to->vval.v_list = NULL;
6313 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006315 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006316 to->vval.v_list = from->vval.v_list->lv_copylist;
6317 ++to->vval.v_list->lv_refcount;
6318 }
6319 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006320 to->vval.v_list = list_copy(from->vval.v_list,
6321 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006322 if (to->vval.v_list == NULL)
6323 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006324 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006325 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006327 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328 case VAR_DICT:
6329 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006330 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006331 if (from->vval.v_dict == NULL)
6332 to->vval.v_dict = NULL;
6333 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006336 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6337 ++to->vval.v_dict->dv_refcount;
6338 }
6339 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006340 to->vval.v_dict = dict_copy(from->vval.v_dict,
6341 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006342 if (to->vval.v_dict == NULL)
6343 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006344 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006345 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006346 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006348 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006349 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 }
6351 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353}
6354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 void
6356echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6357{
6358 char_u *tofree;
6359 char_u numbuf[NUMBUFLEN];
6360 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6361
6362 if (*atstart)
6363 {
6364 *atstart = FALSE;
6365 // Call msg_start() after eval1(), evaluating the expression
6366 // may cause a message to appear.
6367 if (with_space)
6368 {
6369 // Mark the saved text as finishing the line, so that what
6370 // follows is displayed on a new line when scrolling back
6371 // at the more prompt.
6372 msg_sb_eol();
6373 msg_start();
6374 }
6375 }
6376 else if (with_space)
6377 msg_puts_attr(" ", echo_attr);
6378
6379 if (p != NULL)
6380 for ( ; *p != NUL && !got_int; ++p)
6381 {
6382 if (*p == '\n' || *p == '\r' || *p == TAB)
6383 {
6384 if (*p != TAB && *needclr)
6385 {
6386 // remove any text still there from the command
6387 msg_clr_eos();
6388 *needclr = FALSE;
6389 }
6390 msg_putchar_attr(*p, echo_attr);
6391 }
6392 else
6393 {
6394 if (has_mbyte)
6395 {
6396 int i = (*mb_ptr2len)(p);
6397
6398 (void)msg_outtrans_len_attr(p, i, echo_attr);
6399 p += i - 1;
6400 }
6401 else
6402 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6403 }
6404 }
6405 vim_free(tofree);
6406}
6407
Bram Moolenaare9a41262005-01-15 22:18:47 +00006408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 * ":echo expr1 ..." print each argument separated with a space, add a
6410 * newline at the end.
6411 * ":echon expr1 ..." print each argument plain.
6412 */
6413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006418 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 int needclr = TRUE;
6420 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006421 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006422 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006423 evalarg_T evalarg;
6424
Bram Moolenaare707c882020-07-01 13:07:37 +02006425 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 if (eap->skip)
6428 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006429 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 // If eval1() causes an error message the text from the command may
6432 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006433 need_clr_eos = needclr;
6434
Bram Moolenaar68db9962021-05-09 23:19:22 +02006435 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006436 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
6438 /*
6439 * Report the invalid expression unless the expression evaluation
6440 * has been cancelled due to an aborting error, an interrupt, or an
6441 * exception.
6442 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006443 if (!aborting() && did_emsg == did_emsg_before
6444 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006445 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006446 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 break;
6448 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006449 need_clr_eos = FALSE;
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006452 {
6453 if (rettv.v_type == VAR_VOID)
6454 {
6455 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6456 break;
6457 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006458 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006459 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 arg = skipwhite(arg);
6463 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006464 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006465 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 if (eap->skip)
6468 --emsg_skip;
6469 else
6470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (needclr)
6473 msg_clr_eos();
6474 if (eap->cmdidx == CMD_echo)
6475 msg_end();
6476 }
6477}
6478
6479/*
6480 * ":echohl {name}".
6481 */
6482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006485 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486}
6487
6488/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006489 * Returns the :echo attribute
6490 */
6491 int
6492get_echo_attr(void)
6493{
6494 return echo_attr;
6495}
6496
6497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 * ":execute expr1 ..." execute the result of an expression.
6499 * ":echomsg expr1 ..." Print a message
6500 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006501 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 * Each gets spaces around each argument and a newline at the end for
6503 * echo commands
6504 */
6505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006506ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 int ret = OK;
6511 char_u *p;
6512 garray_T ga;
6513 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006514 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 ga_init2(&ga, 1, 80);
6517
6518 if (eap->skip)
6519 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006520 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006522 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006523 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
6526 if (!eap->skip)
6527 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006528 char_u buf[NUMBUFLEN];
6529
6530 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006531 {
6532 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6533 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006534 semsg(_(e_using_invalid_value_as_string_str),
6535 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006536 p = NULL;
6537 }
6538 else
6539 p = tv_get_string_buf(&rettv, buf);
6540 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006541 else
6542 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006543 if (p == NULL)
6544 {
6545 clear_tv(&rettv);
6546 ret = FAIL;
6547 break;
6548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 len = (int)STRLEN(p);
6550 if (ga_grow(&ga, len + 2) == FAIL)
6551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006552 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 ret = FAIL;
6554 break;
6555 }
6556 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 ga.ga_len += len;
6560 }
6561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 arg = skipwhite(arg);
6564 }
6565
6566 if (ret != FAIL && ga.ga_data != NULL)
6567 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006568 // use the first line of continuation lines for messages
6569 SOURCING_LNUM = start_lnum;
6570
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006571 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006573 // Mark the already saved text as finishing the line, so that what
6574 // follows is displayed on a new line when scrolling back at the
6575 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006576 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006577 }
6578
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006580 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006581 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006582 out_flush();
6583 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006584 else if (eap->cmdidx == CMD_echoconsole)
6585 {
6586 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6587 ui_write((char_u *)"\r\n", 2, TRUE);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 else if (eap->cmdidx == CMD_echoerr)
6590 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006591 int save_did_emsg = did_emsg;
6592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006594 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 if (!force_abort)
6596 did_emsg = save_did_emsg;
6597 }
6598 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006599 {
6600 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6601
6602 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6603 sticky_cmdmod_flags = cmdmod.cmod_flags
6604 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 do_cmdline((char_u *)ga.ga_data,
6606 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006607 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
6610
6611 ga_clear(&ga);
6612
6613 if (eap->skip)
6614 --emsg_skip;
6615
Bram Moolenaar63b91732021-08-05 20:40:03 +02006616 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618
6619/*
6620 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6621 * "arg" points to the "&" or '+' when called, to "option" when returning.
6622 * Returns NULL when no option name found. Otherwise pointer to the char
6623 * after the option name.
6624 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006625 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006626find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 char_u *p = *arg;
6629
6630 ++p;
6631 if (*p == 'g' && p[1] == ':')
6632 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006633 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 p += 2;
6635 }
6636 else if (*p == 'l' && p[1] == ':')
6637 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006638 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 p += 2;
6640 }
6641 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006642 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 if (!ASCII_ISALPHA(*p))
6645 return NULL;
6646 *arg = p;
6647
6648 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 else
6651 while (ASCII_ISALPHA(*p))
6652 ++p;
6653 return p;
6654}
6655
6656/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006657 * Display script name where an item was last set.
6658 * Should only be invoked when 'verbose' is non-zero.
6659 */
6660 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006661last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006662{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006663 char_u *p;
6664
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006665 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006666 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006667 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006668 if (p != NULL)
6669 {
6670 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006671 msg_puts(_("\n\tLast set from "));
6672 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006673 if (script_ctx.sc_lnum > 0)
6674 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006675 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006676 msg_outnum((long)script_ctx.sc_lnum);
6677 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006678 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006679 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006680 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006681 }
6682}
6683
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006684#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686/*
6687 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006688 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 * "flags" can be "g" to do a global substitute.
6690 * Returns an allocated string, NULL for error.
6691 */
6692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693do_string_sub(
6694 char_u *str,
6695 char_u *pat,
6696 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006697 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 int sublen;
6701 regmatch_T regmatch;
6702 int i;
6703 int do_all;
6704 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006705 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 garray_T ga;
6707 char_u *ret;
6708 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006709 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006713 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 ga_init2(&ga, 1, 200);
6716
6717 do_all = (flags[0] == 'g');
6718
6719 regmatch.rm_ic = p_ic;
6720 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6721 if (regmatch.regprog != NULL)
6722 {
6723 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006724 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006727 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006728 if (regmatch.startp[0] == regmatch.endp[0])
6729 {
6730 if (zero_width == regmatch.startp[0])
6731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006732 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006733 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006734 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6735 (size_t)i);
6736 ga.ga_len += i;
6737 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006738 continue;
6739 }
6740 zero_width = regmatch.startp[0];
6741 }
6742
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 /*
6744 * Get some space for a temporary buffer to do the substitution
6745 * into. It will contain:
6746 * - The text up to where the match is.
6747 * - The substituted text.
6748 * - The text after the match.
6749 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006750 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006751 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6753 {
6754 ga_clear(&ga);
6755 break;
6756 }
6757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006758 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 i = (int)(regmatch.startp[0] - tail);
6760 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006761 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006762 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 + ga.ga_len + i, TRUE, TRUE, FALSE);
6764 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006765 tail = regmatch.endp[0];
6766 if (*tail == NUL)
6767 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (!do_all)
6769 break;
6770 }
6771
6772 if (ga.ga_data != NULL)
6773 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6774
Bram Moolenaar473de612013-06-08 18:19:48 +02006775 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777
6778 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6779 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006780 if (p_cpo == empty_option)
6781 p_cpo = save_cpo;
6782 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006784 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006785 // If it's still empty it was changed and restored, need to restore in
6786 // the complicated way.
6787 if (*p_cpo == NUL)
6788 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006789 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792 return ret;
6793}