blob: 62266f884b2a4f6fa3ff6b029a90d902f85f1fec [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000114}
115
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000116#if defined(EXITFREE) || defined(PROTO)
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200120 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100121 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200122 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200124 // autoloaded script names
125 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000126
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200127 // unreferenced lists and dicts
128 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200129
130 // functions not garbage collected
131 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132}
133#endif
134
Bram Moolenaare6b53242020-07-01 17:28:33 +0200135 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200136fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
137{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100138 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200140 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200141 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200142 evalarg->eval_cstack = eap->cstack;
143 if (getline_equal(eap->getline, eap->cookie, getsourceline))
144 {
145 evalarg->eval_getline = eap->getline;
146 evalarg->eval_cookie = eap->cookie;
147 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 }
149}
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Top level evaluation function, returning a boolean.
153 * Sets "error" to TRUE if there was an error.
154 * Return TRUE or FALSE.
155 */
156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100157eval_to_bool(
158 char_u *arg,
159 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200160 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaar33570922005-01-25 22:26:29 +0000163 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200164 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200165 evalarg_T evalarg;
166
Bram Moolenaar37c83712020-06-30 21:18:36 +0200167 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 if (skip)
170 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 else
174 {
175 *error = FALSE;
176 if (!skip)
177 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200179 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200180 else
181 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
184 }
185 if (skip)
186 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200187 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192/*
193 * Call eval1() and give an error message if not done at a lower level.
194 */
195 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200196eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100198 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199 int ret;
200 int did_emsg_before = did_emsg;
201 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
205
206 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200215 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000251 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
276 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200281 else if (expr->v_type == VAR_INSTR)
282 {
283 return exe_typval_instr(expr, rettv);
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 else
286 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000287 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200291 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200293 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200296 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200318 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar37c83712020-06-30 21:18:36 +0200338 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (skip)
340 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 retval = NULL;
343 else
344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100345 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348 if (skip)
349 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return retval;
353}
354
355/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356 * Skip over an expression at "*pp".
357 * Return FAIL for an error, OK otherwise.
358 */
359 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200360skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361{
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363
364 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366}
367
368/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200369 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200370 * If in Vim9 script and line breaks are encountered, the lines are
371 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200372 * "arg" is advanced to just after the expression.
373 * "start" is set to the start of the expression, "end" to just after the end.
374 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Return FAIL for an error, OK otherwise.
376 */
377 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200378skip_expr_concatenate(
379 char_u **arg,
380 char_u **start,
381 char_u **end,
382 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383{
384 typval_T rettv;
385 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200386 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200387 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200388 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200390 int evaluate = evalarg == NULL
391 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200393 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200400 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403
404 // Don't evaluate the expression.
405 if (evalarg != NULL)
406 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *arg = skipwhite(*arg);
408 res = eval1(arg, &rettv, evalarg);
409 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 if (evalarg != NULL)
411 evalarg->eval_flags = save_flags;
412
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200414 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200416 if (evalarg->eval_ga.ga_len == 1)
417 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200418 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 ga_clear(gap);
420 gap->ga_itemsize = 0;
421 }
422 else
423 {
424 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200425 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // Line breaks encountered, concatenate all the lines.
428 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200430
431 // free the lines only when using getsourceline()
432 if (evalarg->eval_cookie != NULL)
433 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 // Do not free the last line, "arg" points into it, free it
437 // later.
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree =
440 ((char_u **)gap->ga_data)[gap->ga_len - 1];
441 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 ga_clear_strings(gap);
443 }
444 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200445 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447
448 // free lines that were explicitly marked for freeing
449 ga_clear_strings(freegap);
450 }
451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100467 * Convert "tv" to a string.
468 * When "convert" is TRUE convert a List into a sequence of lines and convert
469 * a Float to a String.
470 * Returns an allocated string (NULL when out of memory).
471 */
472 char_u *
473typval2string(typval_T *tv, int convert)
474{
475 garray_T ga;
476 char_u *retval;
477#ifdef FEAT_FLOAT
478 char_u numbuf[NUMBUFLEN];
479#endif
480
481 if (convert && tv->v_type == VAR_LIST)
482 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000483 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100484 if (tv->vval.v_list != NULL)
485 {
486 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
487 if (tv->vval.v_list->lv_len > 0)
488 ga_append(&ga, NL);
489 }
490 ga_append(&ga, NUL);
491 retval = (char_u *)ga.ga_data;
492 }
493#ifdef FEAT_FLOAT
494 else if (convert && tv->v_type == VAR_FLOAT)
495 {
496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
497 retval = vim_strsave(numbuf);
498 }
499#endif
500 else
501 retval = vim_strsave(tv_get_string(tv));
502 return retval;
503}
504
505/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 * Top level evaluation function, returning a string. Does not handle line
507 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000508 * When "convert" is TRUE convert a List into a sequence of lines and convert
509 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Return pointer to allocated memory, or NULL for failure.
511 */
512 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100513eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100514 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100515 int convert,
516 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaar33570922005-01-25 22:26:29 +0000518 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
523 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 retval = NULL;
525 else
526 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 return retval;
533}
534
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 char_u *
536eval_to_string(
537 char_u *arg,
538 int convert)
539{
540 return eval_to_string_eap(arg, convert, NULL);
541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000544 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200545 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200546 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100549eval_to_string_safe(
550 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000551 int use_sandbox,
552 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200555 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200556 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200557 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar9530b582022-01-22 13:39:08 +0000559 if (!keep_script_version)
560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200565 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000624 * "*arg" points to what can be a function name in the form of "import.Name" or
625 * "Funcref". Return the name of the function. Set "tofree" to something that
626 * was allocated.
627 * If "verbose" is FALSE no errors are given.
628 * Return NULL for any failure.
629 */
630 static char_u *
631deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000632 char_u **arg,
633 char_u **tofree,
634 evalarg_T *evalarg,
635 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000636{
637 typval_T ref;
638 char_u *name = *arg;
639
640 ref.v_type = VAR_UNKNOWN;
641 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
642 return NULL;
643 if (*skipwhite(*arg) != NUL)
644 {
645 if (verbose)
646 semsg(_(e_trailing_characters_str), *arg);
647 name = NULL;
648 }
649 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
650 {
651 name = ref.vval.v_string;
652 ref.vval.v_string = NULL;
653 *tofree = name;
654 }
655 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
656 {
657 if (ref.vval.v_partial->pt_argc > 0
658 || ref.vval.v_partial->pt_dict != NULL)
659 {
660 if (verbose)
661 emsg(_(e_cannot_use_partial_here));
662 name = NULL;
663 }
664 else
665 {
666 name = vim_strsave(partial_name(ref.vval.v_partial));
667 *tofree = name;
668 }
669 }
670 else
671 {
672 if (verbose)
673 semsg(_(e_not_callable_type_str), name);
674 name = NULL;
675 }
676 clear_tv(&ref);
677 return name;
678}
679
680/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100681 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200682 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
683 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687call_vim_function(
688 char_u *func,
689 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200691 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200694 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000695 char_u *arg;
696 char_u *name;
697 char_u *tofree = NULL;
Bram 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
708 // autoload script has errors. Guess that when there is a dot or '#' in
709 // the name showing errors is the right choice.
710 ignore_errors = vim_strchr(func, '.') == NULL
711 && vim_strchr(func, AUTOLOAD_CHAR) == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000712 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000713 if (ignore_errors)
714 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000715 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000716 if (ignore_errors)
717 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000718 if (name == NULL)
719 name = func;
720
721 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
722
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 if (ret == FAIL)
724 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000725 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000726
727 return ret;
728}
729
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100730/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100731 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000732 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
733 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000734 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000735 */
736 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100737call_func_retstr(
738 char_u *func,
739 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200740 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741{
742 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000743 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000744
Bram Moolenaarded27a12018-08-01 19:06:03 +0200745 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000746 return NULL;
747
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100748 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 return retval;
751}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000752
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000753/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100754 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000755 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000756 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000757 */
758 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100759call_func_retlist(
760 char_u *func,
761 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200762 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000763{
764 typval_T rettv;
765
Bram Moolenaarded27a12018-08-01 19:06:03 +0200766 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000767 return NULL;
768
769 if (rettv.v_type != VAR_LIST)
770 {
771 clear_tv(&rettv);
772 return NULL;
773 }
774
775 return rettv.vval.v_list;
776}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaare70dd112022-01-21 16:31:11 +0000778#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100780 * Evaluate "arg", which is 'foldexpr'.
781 * Note: caller must set "curwin" to match "arg".
782 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
783 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000786eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000788 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000789 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200790 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000792 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaare70dd112022-01-21 16:31:11 +0000796 arg = wp->w_p_fde;
797 current_sctx = wp->w_p_script_ctx[WV_FDE];
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000800 if (use_sandbox)
801 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200802 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200804 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 retval = 0;
806 else
807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100808 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 if (tv.v_type == VAR_NUMBER)
810 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000811 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 retval = 0;
813 else
814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 // If the result is a string, check if there is a non-digit before
816 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (!VIM_ISDIGIT(*s) && *s != '-')
819 *cp = *s++;
820 retval = atol((char *)s);
821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000825 if (use_sandbox)
826 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200827 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200828 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000829 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200831 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833#endif
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 * Get an lval: variable, Dict item or List item that can be assigned a value
837 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
838 * "name.key", "name.key[expr]" etc.
839 * Indexing only works if "name" is an existing List or Dictionary.
840 * "name" points to the start of the name.
841 * If "rettv" is not NULL it points to the value to be assigned.
842 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
843 * wrong; must end in space or cmd separator.
844 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100845 * flags:
846 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100847 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100848 * GLV_NO_AUTOLOAD: do not use script autoloading
849 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200854 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100855get_lval(
856 char_u *name,
857 typval_T *rettv,
858 lval_T *lp,
859 int unlet,
860 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 int flags, // GLV_ values
862 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000864 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 char_u *expr_start, *expr_end;
866 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 dictitem_T *v;
868 typval_T var1;
869 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000872 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100873 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100874 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100875 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000876 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100878 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200879 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100881 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100883 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200885 lp->ll_name_end = find_name_end(name, NULL, NULL,
886 FNE_INCL_BR | fne_flags);
887 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 }
889
Bram Moolenaara749a422022-02-12 19:52:25 +0000890 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000891 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000892 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
893 {
894 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
895 return NULL;
896 }
897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100898 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000899 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if (expr_start != NULL)
902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100903 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100904 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 && *p != '[' && *p != '.')
906 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000907 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 return NULL;
909 }
910
911 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
912 if (lp->ll_exp_name == NULL)
913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100914 // Report an invalid expression in braces, unless the
915 // expression evaluation has been cancelled due to an
916 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 if (!aborting() && !quiet)
918 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000919 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000920 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 return NULL;
922 }
923 }
924 lp->ll_name = lp->ll_exp_name;
925 }
926 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 lp->ll_name = name;
929
Bram Moolenaar4525a572022-02-13 11:57:33 +0000930 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200932 // "a: type" is declaring variable "a" with a type, not "a:".
933 if (p == name + 2 && p[-1] == ':')
934 {
935 --p;
936 lp->ll_name_end = p;
937 }
938 if (*p == ':')
939 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000940 garray_T tmp_type_list;
941 garray_T *type_list;
942 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200943
944 if (tp == p + 1 && !quiet)
945 {
946 semsg(_(e_white_space_required_after_str_str), ":", p);
947 return NULL;
948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000950 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
951 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
952 else
953 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000954 // TODO: should we give an error here?
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000955 type_list = &tmp_type_list;
956 ga_init2(type_list, sizeof(type_T), 10);
957 }
958
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200959 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000960 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100961 if (lp->ll_type == NULL && !quiet)
962 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200963 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000964
965 // drop the type when not in a script
966 if (type_list == &tmp_type_list)
967 {
968 lp->ll_type = NULL;
969 clear_type_list(type_list);
970 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 }
973 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000974 if (lp->ll_name == NULL)
975 return p;
976
Bram Moolenaar62aec932022-01-29 21:45:34 +0000977 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000978 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000979 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000980
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000981 if (import != NULL)
982 {
983 ufunc_T *ufunc;
984 type_T *type;
985
986 lp->ll_sid = import->imp_sid;
987 lp->ll_name = skipwhite(p + 1);
988 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
989 lp->ll_name_end = p;
990
991 // check the item is exported
992 cc = *p;
993 *p = NUL;
994 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000995 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000996 {
997 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000998 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000999 }
1000 *p = cc;
1001 }
1002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001005 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return p;
1007
Bram Moolenaar4525a572022-02-13 11:57:33 +00001008 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001009 {
1010 // using local variable
1011 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001012 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001013 }
1014 else
1015 {
1016 cc = *p;
1017 *p = NUL;
1018 // When we would write to the variable pass &ht and prevent autoload.
1019 writing = !(flags & GLV_READ_ONLY);
1020 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001021 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001022 if (v == NULL && !quiet)
1023 semsg(_(e_undefined_variable_str), lp->ll_name);
1024 *p = cc;
1025 if (v == NULL)
1026 return NULL;
1027 lp->ll_tv = &v->di_tv;
1028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001029
Bram Moolenaar4525a572022-02-13 11:57:33 +00001030 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001031 {
1032 if (!quiet)
1033 semsg(_(e_variable_already_declared), lp->ll_name);
1034 return NULL;
1035 }
1036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 /*
1038 * Loop until no more [idx] or .key is following.
1039 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 var1.v_type = VAR_UNKNOWN;
1041 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001042 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001044 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1045 {
1046 if (!quiet)
1047 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1048 return NULL;
1049 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001050 if (lp->ll_tv->v_type != VAR_LIST
1051 && lp->ll_tv->v_type != VAR_DICT
1052 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001053 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001055 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001057 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001058
1059 // a NULL list/blob works like an empty list/blob, allocate one now.
1060 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1061 rettv_list_alloc(lp->ll_tv);
1062 else if (lp->ll_tv->v_type == VAR_BLOB
1063 && lp->ll_tv->vval.v_blob == NULL)
1064 rettv_blob_alloc(lp->ll_tv);
1065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001069 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001071 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001072
Bram Moolenaar4525a572022-02-13 11:57:33 +00001073 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001074 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001075 && lp->ll_tv == &v->di_tv
1076 && ht != NULL && ht == get_script_local_ht())
1077 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001078 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001079
1080 // Vim9 script local variable: get the type
1081 if (sv != NULL)
1082 lp->ll_valtype = sv->sv_type;
1083 }
1084
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 len = -1;
1086 if (*p == '.')
1087 {
1088 key = p + 1;
1089 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1090 ;
1091 if (len == 0)
1092 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001094 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001095 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001096 }
1097 p = key + len;
1098 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001099 else
1100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001101 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001102 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001103 if (*p == ':')
1104 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001105 else
1106 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001108 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001110 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001111 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001112 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001113 clear_tv(&var1);
1114 return NULL;
1115 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001116 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001117 }
1118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001120 if (*p == ':')
1121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001125 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001126 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001128 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001129 if (rettv != NULL
1130 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001132 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001133 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001136 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001137 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 }
1140 p = skipwhite(p + 1);
1141 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 else
1144 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001146 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001147 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001149 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001152 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001155 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001156 clear_tv(&var2);
1157 return NULL;
1158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001159 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001164
Bram Moolenaar8c711452005-01-14 21:53:12 +00001165 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001168 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001169 clear_tv(&var1);
1170 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001171 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001172 }
1173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 ++p;
1176 }
1177
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 {
1180 if (len == -1)
1181 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001182 // "[key]": get key from "var1"
1183 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001184 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 }
1189 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001190 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001191
1192 // a NULL dict is equivalent with an empty dict
1193 if (lp->ll_tv->vval.v_dict == NULL)
1194 {
1195 lp->ll_tv->vval.v_dict = dict_alloc();
1196 if (lp->ll_tv->vval.v_dict == NULL)
1197 {
1198 clear_tv(&var1);
1199 return NULL;
1200 }
1201 ++lp->ll_tv->vval.v_dict->dv_refcount;
1202 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001203 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001204
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001205 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001207 // When assigning to a scope dictionary check that a function and
1208 // variable name is valid (only variable name unless it is l: or
1209 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001210 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001211 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001212 int prevval;
1213 int wrong;
1214
1215 if (len != -1)
1216 {
1217 prevval = key[len];
1218 key[len] = NUL;
1219 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001220 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001221 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001222 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1223 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001224 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001225 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001226 if (len != -1)
1227 key[len] = prevval;
1228 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001229 {
1230 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001231 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001232 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001233 }
1234
Bram Moolenaar10c65862020-10-08 21:16:42 +02001235 if (lp->ll_valtype != NULL)
1236 // use the type of the member
1237 lp->ll_valtype = lp->ll_valtype->tt_member;
1238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001241 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001242 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001243 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001244 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001245 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001246 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001247 return NULL;
1248 }
1249
Bram Moolenaar31b81602019-02-10 22:14:27 +01001250 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001254 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001255 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 }
1258 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001262 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001264 p = NULL;
1265 break;
1266 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001267 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001268 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001269 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1270 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001271 {
1272 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001273 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001274 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001275
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001276 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 else if (lp->ll_tv->v_type == VAR_BLOB)
1280 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001281 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1282
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001283 /*
1284 * Get the number and item for the only or first index of the List.
1285 */
1286 if (empty1)
1287 lp->ll_n1 = 0;
1288 else
1289 // is number or string
1290 lp->ll_n1 = (long)tv_get_number(&var1);
1291 clear_tv(&var1);
1292
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001293 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 return NULL;
1297 }
1298 if (lp->ll_range && !lp->ll_empty2)
1299 {
1300 lp->ll_n2 = (long)tv_get_number(&var2);
1301 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001302 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1303 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001304 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001305 }
1306 lp->ll_blob = lp->ll_tv->vval.v_blob;
1307 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001308 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 else
1311 {
1312 /*
1313 * Get the number and item for the only or first index of the List.
1314 */
1315 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001318 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001320 clear_tv(&var1);
1321
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001322 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001324 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001325 if (lp->ll_li == NULL)
1326 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001327 clear_tv(&var2);
1328 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 }
1330
Bram Moolenaar10c65862020-10-08 21:16:42 +02001331 if (lp->ll_valtype != NULL)
1332 // use the type of the member
1333 lp->ll_valtype = lp->ll_valtype->tt_member;
1334
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 /*
1336 * May need to find the item or absolute index for the second
1337 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 * When no index given: "lp->ll_empty2" is TRUE.
1339 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001343 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001346 if (check_range_index_two(lp->ll_list,
1347 &lp->ll_n1, lp->ll_li,
1348 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001350 }
1351
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 }
1355
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001356 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 return p;
1359}
1360
1361/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001365clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366{
1367 vim_free(lp->ll_exp_name);
1368 vim_free(lp->ll_newkey);
1369}
1370
1371/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001372 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001374 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1375 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378set_var_lval(
1379 lval_T *lp,
1380 char_u *endp,
1381 typval_T *rettv,
1382 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001383 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1384 char_u *op,
1385 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386{
1387 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389
1390 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001392 cc = *endp;
1393 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001394 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1395 return;
1396
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001397 if (lp->ll_blob != NULL)
1398 {
1399 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001401 if (op != NULL && *op != '=')
1402 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001403 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001404 return;
1405 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001406 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001407 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408
1409 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1410 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001411 if (lp->ll_empty2)
1412 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1413
Bram Moolenaar68452172021-04-12 21:21:02 +02001414 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1415 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001417 }
1418 else
1419 {
1420 val = (int)tv_get_number_chk(rettv, &error);
1421 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001422 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001423 }
1424 }
1425 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001429 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1430 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001431 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001432 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 *endp = cc;
1434 return;
1435 }
1436
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001438 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001439 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001440 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 {
1442 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001443 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1444 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001445 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001446 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001447 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001448 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001451 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001452 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001453 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1454 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001455 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001456 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001457 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001458 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001459 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001461 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001462 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001463 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001464 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 else if (lp->ll_range)
1466 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001467 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1468 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001469 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001470 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001471 return;
1472 }
1473
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001474 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1475 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 }
1477 else
1478 {
1479 /*
1480 * Assign to a List or Dictionary item.
1481 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001482 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1483 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001484 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001485 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001486 return;
1487 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001488
1489 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001490 && check_typval_arg_type(lp->ll_valtype, rettv,
1491 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001492 return;
1493
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 if (lp->ll_newkey != NULL)
1495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 if (op != NULL && *op != '=')
1497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001498 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 return;
1500 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001501 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1502 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001503 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001505 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001506 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 if (di == NULL)
1508 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001509 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1510 {
1511 vim_free(di);
1512 return;
1513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 else if (op != NULL && *op != '=')
1517 {
1518 tv_op(lp->ll_tv, rettv, op);
1519 return;
1520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001522 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 /*
1525 * Assign the value to the variable or list item.
1526 */
1527 if (copy)
1528 copy_tv(rettv, lp->ll_tv);
1529 else
1530 {
1531 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001532 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 }
1535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536}
1537
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001539 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1540 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 * Returns OK or FAIL.
1542 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001546 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 char_u numbuf[NUMBUFLEN];
1548 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001549 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001551 // Can't do anything with a Funcref or Dict on the right.
1552 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001553 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001554 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1555 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 {
1557 switch (tv1->v_type)
1558 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001559 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001560 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 case VAR_DICT:
1563 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001564 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001565 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001566 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001567 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001568 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001569 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 break;
1571
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001572 case VAR_BLOB:
1573 if (*op != '+' || tv2->v_type != VAR_BLOB)
1574 break;
1575 // BLOB += BLOB
1576 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1577 {
1578 blob_T *b1 = tv1->vval.v_blob;
1579 blob_T *b2 = tv2->vval.v_blob;
1580 int i, len = blob_len(b2);
1581 for (i = 0; i < len; i++)
1582 ga_append(&b1->bv_ga, blob_get(b2, i));
1583 }
1584 return OK;
1585
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 case VAR_LIST:
1587 if (*op != '+' || tv2->v_type != VAR_LIST)
1588 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001590 if (tv2->vval.v_list != NULL)
1591 {
1592 if (tv1->vval.v_list == NULL)
1593 {
1594 tv1->vval.v_list = tv2->vval.v_list;
1595 ++tv1->vval.v_list->lv_refcount;
1596 }
1597 else
1598 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1599 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 return OK;
1601
1602 case VAR_NUMBER:
1603 case VAR_STRING:
1604 if (tv2->v_type == VAR_LIST)
1605 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610#ifdef FEAT_FLOAT
1611 if (tv2->v_type == VAR_FLOAT)
1612 {
1613 float_T f = n;
1614
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 if (*op == '%')
1616 break;
1617 switch (*op)
1618 {
1619 case '+': f += tv2->vval.v_float; break;
1620 case '-': f -= tv2->vval.v_float; break;
1621 case '*': f *= tv2->vval.v_float; break;
1622 case '/': f /= tv2->vval.v_float; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 clear_tv(tv1);
1625 tv1->v_type = VAR_FLOAT;
1626 tv1->vval.v_float = f;
1627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629#endif
1630 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 switch (*op)
1632 {
1633 case '+': n += tv_get_number(tv2); break;
1634 case '-': n -= tv_get_number(tv2); break;
1635 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001636 case '/': n = num_divide(n, tv_get_number(tv2),
1637 &failed); break;
1638 case '%': n = num_modulus(n, tv_get_number(tv2),
1639 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 clear_tv(tv1);
1642 tv1->v_type = VAR_NUMBER;
1643 tv1->vval.v_number = n;
1644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 }
1646 else
1647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648 if (tv2->v_type == VAR_FLOAT)
1649 break;
1650
Bram Moolenaarff697e62019-02-12 22:28:33 +01001651 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001652 s = tv_get_string(tv1);
1653 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 clear_tv(tv1);
1655 tv1->v_type = VAR_STRING;
1656 tv1->vval.v_string = s;
1657 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001658 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001660 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001661#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001662 {
1663 float_T f;
1664
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 if (*op == '%' || *op == '.'
1666 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001667 && tv2->v_type != VAR_NUMBER
1668 && tv2->v_type != VAR_STRING))
1669 break;
1670 if (tv2->v_type == VAR_FLOAT)
1671 f = tv2->vval.v_float;
1672 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001673 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001674 switch (*op)
1675 {
1676 case '+': tv1->vval.v_float += f; break;
1677 case '-': tv1->vval.v_float -= f; break;
1678 case '*': tv1->vval.v_float *= f; break;
1679 case '/': tv1->vval.v_float /= f; break;
1680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001683 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 }
1685 }
1686
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001687 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 return FAIL;
1689}
1690
1691/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 * Evaluate the expression used in a ":for var in expr" command.
1693 * "arg" points to "var".
1694 * Set "*errp" to TRUE for an error, FALSE otherwise;
1695 * Return a pointer that holds the info. Null when there is an error.
1696 */
1697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001698eval_for_line(
1699 char_u *arg,
1700 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001701 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001702 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703{
Bram Moolenaar33570922005-01-25 22:26:29 +00001704 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001705 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001707 typval_T tv;
1708 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001713 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (fi == NULL)
1715 return NULL;
1716
Bram Moolenaar404557e2021-07-05 21:41:48 +02001717 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1718 &fi->fi_semicolon, FALSE);
1719 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return fi;
1721
Bram Moolenaar404557e2021-07-05 21:41:48 +02001722 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001723 if (expr[0] != 'i' || expr[1] != 'n'
1724 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001726 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1727 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1728 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001729 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 return fi;
1731 }
1732
1733 if (skip)
1734 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001735 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1736 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 {
1738 *errp = FALSE;
1739 if (!skip)
1740 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001742 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 l = tv.vval.v_list;
1744 if (l == NULL)
1745 {
1746 // a null list is like an empty list: do nothing
1747 clear_tv(&tv);
1748 }
1749 else
1750 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001752 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 // No need to increment the refcount, it's already set for
1755 // the list being used in "tv".
1756 fi->fi_list = l;
1757 list_add_watch(l, &fi->fi_lw);
1758 fi->fi_lw.lw_item = l->lv_first;
1759 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001760 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001762 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 fi->fi_bi = 0;
1764 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001765 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001766 typval_T btv;
1767
1768 // Make a copy, so that the iteration still works when the
1769 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001771 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001772 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001773 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001774 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 else if (tv.v_type == VAR_STRING)
1776 {
1777 fi->fi_byte_idx = 0;
1778 fi->fi_string = tv.vval.v_string;
1779 tv.vval.v_string = NULL;
1780 if (fi->fi_string == NULL)
1781 fi->fi_string = vim_strsave((char_u *)"");
1782 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 else
1784 {
Sean Dewar80d73952021-08-04 19:25:54 +02001785 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001786 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 }
1788 }
1789 }
1790 if (skip)
1791 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001792 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793
1794 return fi;
1795}
1796
1797/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001798 * Used when looping over a :for line, skip the "in expr" part.
1799 */
1800 void
1801skip_for_lines(void *fi_void, evalarg_T *evalarg)
1802{
1803 forinfo_T *fi = (forinfo_T *)fi_void;
1804 int i;
1805
1806 for (i = 0; i < fi->fi_break_count; ++i)
1807 eval_next_line(evalarg);
1808}
1809
1810/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 * Use the first item in a ":for" list. Advance to the next.
1812 * Assign the values to the variable (list). "arg" points to the first one.
1813 * Return TRUE when a valid item was found, FALSE when at end of list or
1814 * something wrong.
1815 */
1816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001819 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001821 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001822 ? (ASSIGN_FINAL
1823 // first round: error if variable exists
1824 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1825 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001826 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001828 int skip_assign = in_vim9script() && arg[0] == '_'
1829 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001831 if (fi->fi_blob != NULL)
1832 {
1833 typval_T tv;
1834
1835 if (fi->fi_bi >= blob_len(fi->fi_blob))
1836 return FALSE;
1837 tv.v_type = VAR_NUMBER;
1838 tv.v_lock = VAR_FIXED;
1839 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1840 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001841 if (skip_assign)
1842 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001843 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 fi->fi_varcount, flag, NULL) == OK;
1845 }
1846
1847 if (fi->fi_string != NULL)
1848 {
1849 typval_T tv;
1850 int len;
1851
1852 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1853 if (len == 0)
1854 return FALSE;
1855 tv.v_type = VAR_STRING;
1856 tv.v_lock = VAR_FIXED;
1857 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1858 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001859 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001860 if (skip_assign)
1861 result = TRUE;
1862 else
1863 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001864 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001865 vim_free(tv.vval.v_string);
1866 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 }
1868
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 item = fi->fi_lw.lw_item;
1870 if (item == NULL)
1871 result = FALSE;
1872 else
1873 {
1874 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001875 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001876 if (skip_assign)
1877 result = TRUE;
1878 else
1879 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001880 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 }
1882 return result;
1883}
1884
1885/*
1886 * Free the structure used to store info used by ":for".
1887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001893 if (fi == NULL)
1894 return;
1895 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001898 list_unref(fi->fi_list);
1899 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001900 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001901 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001902 else
1903 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 vim_free(fi);
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908set_context_for_expression(
1909 expand_T *xp,
1910 char_u *arg,
1911 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001917 if (cmdidx == CMD_let || cmdidx == CMD_var
1918 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 {
1920 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 {
1926 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001927 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 break;
1930 }
1931 return;
1932 }
1933 }
1934 else
1935 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1936 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 while ((xp->xp_pattern = vim_strpbrk(arg,
1938 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1939 {
1940 c = *xp->xp_pattern;
1941 if (c == '&')
1942 {
1943 c = xp->xp_pattern[1];
1944 if (c == '&')
1945 {
1946 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001947 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001952 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1953 xp->xp_pattern += 2;
1954
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 else if (c == '$')
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 xp->xp_context = EXPAND_ENV_VARS;
1961 }
1962 else if (c == '=')
1963 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001964 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 xp->xp_context = EXPAND_EXPRESSION;
1966 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001967 else if (c == '#'
1968 && xp->xp_context == EXPAND_EXPRESSION)
1969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001971 break;
1972 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001973 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 && xp->xp_context == EXPAND_FUNCTIONS
1975 && vim_strchr(xp->xp_pattern, '(') == NULL)
1976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
1979 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001980 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1985 if (c == '\\' && xp->xp_pattern[1] != NUL)
1986 ++xp->xp_pattern;
1987 xp->xp_context = EXPAND_NOTHING;
1988 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001989 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001991 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1993 /* skip */ ;
1994 xp->xp_context = EXPAND_NOTHING;
1995 }
1996 else if (c == '|')
1997 {
1998 if (xp->xp_pattern[1] == '|')
1999 {
2000 ++xp->xp_pattern;
2001 xp->xp_context = EXPAND_EXPRESSION;
2002 }
2003 else
2004 xp->xp_context = EXPAND_COMMANDS;
2005 }
2006 else
2007 xp->xp_context = EXPAND_EXPRESSION;
2008 }
2009 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // Doesn't look like something valid, expand as an expression
2011 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 arg = xp->xp_pattern;
2014 if (*arg != NUL)
2015 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2016 /* skip */ ;
2017 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002018
2019 // ":exe one two" completes "two"
2020 if ((cmdidx == CMD_execute
2021 || cmdidx == CMD_echo
2022 || cmdidx == CMD_echon
2023 || cmdidx == CMD_echomsg)
2024 && xp->xp_context == EXPAND_EXPRESSION)
2025 {
2026 for (;;)
2027 {
2028 char_u *n = skiptowhite(arg);
2029
2030 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2031 break;
2032 arg = skipwhite(n);
2033 }
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 xp->xp_pattern = arg;
2037}
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002040 * Return TRUE if "pat" matches "text".
2041 * Does not use 'cpo' and always uses 'magic'.
2042 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002043 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002044pattern_match(char_u *pat, char_u *text, int ic)
2045{
2046 int matches = FALSE;
2047 char_u *save_cpo;
2048 regmatch_T regmatch;
2049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002051 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002052 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002053 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2054 if (regmatch.regprog != NULL)
2055 {
2056 regmatch.rm_ic = ic;
2057 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2058 vim_regfree(regmatch.regprog);
2059 }
2060 p_cpo = save_cpo;
2061 return matches;
2062}
2063
2064/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 * Handle a name followed by "(". Both for just "name(arg)" and for
2066 * "expr->name(arg)".
2067 * Returns OK or FAIL.
2068 */
2069 static int
2070eval_func(
2071 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002072 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002073 char_u *name,
2074 int name_len,
2075 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002076 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077 typval_T *basetv) // "expr" for "expr->name(arg)"
2078{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002079 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002080 char_u *s = name;
2081 int len = name_len;
2082 partial_T *partial;
2083 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002084 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002085 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086
2087 if (!evaluate)
2088 check_vars(s, len);
2089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002090 // If "s" is the name of a variable of type VAR_FUNC
2091 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002092 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002093 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // Need to make a copy, in case evaluating the arguments makes
2096 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002097 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002098 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002099 ret = FAIL;
2100 else
2101 {
2102 funcexe_T funcexe;
2103
2104 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002105 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002106 funcexe.fe_firstline = curwin->w_cursor.lnum;
2107 funcexe.fe_lastline = curwin->w_cursor.lnum;
2108 funcexe.fe_evaluate = evaluate;
2109 funcexe.fe_partial = partial;
2110 funcexe.fe_basetv = basetv;
2111 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002112 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002113 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002114 }
2115 vim_free(s);
2116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002117 // If evaluate is FALSE rettv->v_type was not set in
2118 // get_func_tv, but it's needed in handle_subscript() to parse
2119 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002120 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2121 {
2122 rettv->vval.v_string = NULL;
2123 rettv->v_type = VAR_FUNC;
2124 }
2125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002126 // Stop the expression evaluation when immediately
2127 // aborting on error, or when an interrupt occurred or
2128 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002129 if (evaluate && aborting())
2130 {
2131 if (ret == OK)
2132 clear_tv(rettv);
2133 ret = FAIL;
2134 }
2135 return ret;
2136}
2137
2138/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002139 * Get the next line source line without advancing. But do skip over comment
2140 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002141 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002142 */
2143 static char_u *
2144getline_peek_skip_comments(evalarg_T *evalarg)
2145{
2146 for (;;)
2147 {
2148 char_u *next = getline_peek(evalarg->eval_getline,
2149 evalarg->eval_cookie);
2150 char_u *p;
2151
2152 if (next == NULL)
2153 break;
2154 p = skipwhite(next);
2155 if (*p != NUL && !vim9_comment_start(p))
2156 return next;
2157 (void)eval_next_line(evalarg);
2158 }
2159 return NULL;
2160}
2161
2162/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002163 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2164 * comment) and there is a next line, return the next line (skipping blanks)
2165 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002166 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2167 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 * "arg" must point somewhere inside a line, not at the start.
2169 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002170 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2172{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 char_u *p = skipwhite(arg);
2174
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002176 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002178 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002179 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002181 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002182
2183 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002184 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002185 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002186 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187
Bram Moolenaar9d489562020-07-30 20:08:50 +02002188 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002189 {
2190 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002192 }
2193 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002194 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002195}
2196
2197/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002198 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002199 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002200 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002201 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002202eval_next_line(evalarg_T *evalarg)
2203{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002204 garray_T *gap = &evalarg->eval_ga;
2205 char_u *line;
2206
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002207 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002208 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2209 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002210 else
2211 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002212 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002213 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2214 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002215 char_u *p = skipwhite(line);
2216
2217 // Going to concatenate the lines after parsing. For an empty or
2218 // comment line use an empty string.
2219 if (*p == NUL || vim9_comment_start(p))
2220 {
2221 vim_free(line);
2222 line = vim_strsave((char_u *)"");
2223 }
2224
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002225 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2226 ++gap->ga_len;
2227 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002228 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002229 {
2230 vim_free(evalarg->eval_tofree);
2231 evalarg->eval_tofree = line;
2232 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002233
2234 // Advanced to the next line, "arg" no longer points into the previous
2235 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002236 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002237 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002238}
2239
Bram Moolenaare6b53242020-07-01 17:28:33 +02002240/*
2241 * Call eval_next_non_blank() and get the next line if needed.
2242 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002243 char_u *
2244skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2245{
2246 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002247 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002248
Bram Moolenaar962d7212020-07-04 14:15:00 +02002249 if (evalarg == NULL)
2250 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002251 eval_next_non_blank(p, evalarg, &getnext);
2252 if (getnext)
2253 return eval_next_line(evalarg);
2254 return p;
2255}
2256
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002257/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002258 * Initialize "evalarg" for use.
2259 */
2260 void
2261init_evalarg(evalarg_T *evalarg)
2262{
2263 CLEAR_POINTER(evalarg);
2264 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2265}
2266
2267/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002268 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002269 */
2270 void
2271clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2272{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002273 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002274 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002275 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002276 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002277 if (eap != NULL)
2278 {
2279 // We may need to keep the original command line, e.g. for
2280 // ":let" it has the variable names. But we may also need the
2281 // new one, "nextcmd" points into it. Keep both.
2282 vim_free(eap->cmdline_tofree);
2283 eap->cmdline_tofree = *eap->cmdlinep;
2284 *eap->cmdlinep = evalarg->eval_tofree;
2285 }
2286 else
2287 vim_free(evalarg->eval_tofree);
2288 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002289 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002290
Bram Moolenaar844fb642021-10-23 13:32:30 +01002291 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002292 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002293 }
2294}
2295
2296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2300 */
2301
2302/*
2303 * Handle zero level expression.
2304 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002306 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * Return OK or FAIL.
2309 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002311eval0(
2312 char_u *arg,
2313 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002314 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002317 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2318}
2319
2320/*
2321 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2322 * expression and don't check what comes after the expression.
2323 */
2324 int
2325eval0_retarg(
2326 char_u *arg,
2327 typval_T *rettv,
2328 exarg_T *eap,
2329 evalarg_T *evalarg,
2330 char_u **retarg)
2331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 int ret;
2333 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002334 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002335 int did_emsg_before = did_emsg;
2336 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002338 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002339 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
2341 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002342 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002343 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002344 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002345
Bram Moolenaar02929a32021-12-17 14:46:12 +00002346 // In Vim9 script a command block is not split at NL characters for
2347 // commands using an expression argument. Skip over a '#' comment to check
2348 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002349 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002350 while (*p == '#')
2351 {
2352 char_u *nl = vim_strchr(p, NL);
2353
2354 if (nl == NULL)
2355 break;
2356 p = skipwhite(nl + 1);
2357 if (eap != NULL && *p != NUL)
2358 eap->nextcmd = p;
2359 check_for_end = FALSE;
2360 }
2361
2362 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002363 end_error = !ends_excmd2(arg, p);
2364 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /*
2369 * Report the invalid expression unless the expression evaluation has
2370 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002371 * exception, or we already gave a more specific error.
2372 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002374 if (!aborting()
2375 && did_emsg == did_emsg_before
2376 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002377 && (flags & EVAL_CONSTANT) == 0
2378 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002379 {
2380 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002381 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002382 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002383 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002384 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002385
2386 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002387 // a next command to avoid more errors, unless "|" is following, which
2388 // could only be a command separator.
2389 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2390 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002393
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002394 if (retarg != NULL)
2395 *retarg = p;
2396 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002397 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 return ret;
2400}
2401
2402/*
2403 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002404 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002405 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 *
2407 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002408 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002410 * Note: "rettv.v_lock" is not set.
2411 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 * Return OK or FAIL.
2413 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002414 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002417 char_u *p;
2418 int getnext;
2419
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002420 CLEAR_POINTER(rettv);
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 /*
2423 * Get the first variable.
2424 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return FAIL;
2427
Bram Moolenaar793648f2020-06-26 21:28:25 +02002428 p = eval_next_non_blank(*arg, evalarg, &getnext);
2429 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002431 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002432 int result;
2433 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 evalarg_T *evalarg_used = evalarg;
2435 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002437 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002438 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439
2440 if (evalarg == NULL)
2441 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002442 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002443 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002445 orig_flags = evalarg_used->eval_flags;
2446 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 if (getnext)
2449 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002450 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002451 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002452 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002453 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002454 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002455 clear_tv(rettv);
2456 return FAIL;
2457 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002458 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002459 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 int error = FALSE;
2465
Bram Moolenaar13106602020-10-04 16:06:05 +02002466 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002467 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002468 else if (vim9script)
2469 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002470 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002472 if (error || !op_falsy || !result)
2473 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 if (error)
2475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002479 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002481 if (op_falsy)
2482 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002483 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002484 {
mityu4ac198c2021-05-28 17:52:40 +02002485 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002486 clear_tv(rettv);
2487 return FAIL;
2488 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 evalarg_used->eval_flags = (op_falsy ? !result : result)
2491 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2492 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002493 {
2494 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002496 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002497 if (!op_falsy || !result)
2498 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002500 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002502 /*
2503 * Check for the ":".
2504 */
2505 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2506 if (*p != ':')
2507 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002508 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002509 if (evaluate && result)
2510 clear_tv(rettv);
2511 evalarg_used->eval_flags = orig_flags;
2512 return FAIL;
2513 }
2514 if (getnext)
2515 *arg = eval_next_line(evalarg_used);
2516 else
2517 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002518 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002519 {
2520 error_white_both(p, 1);
2521 clear_tv(rettv);
2522 evalarg_used->eval_flags = orig_flags;
2523 return FAIL;
2524 }
2525 *arg = p;
2526 }
2527
2528 /*
2529 * Get the third variable. Recursive!
2530 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002531 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002532 {
mityu4ac198c2021-05-28 17:52:40 +02002533 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002534 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002535 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002536 return FAIL;
2537 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002538 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2539 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002541 if (eval1(arg, &var2, evalarg_used) == FAIL)
2542 {
2543 if (evaluate && result)
2544 clear_tv(rettv);
2545 evalarg_used->eval_flags = orig_flags;
2546 return FAIL;
2547 }
2548 if (evaluate && !result)
2549 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551
2552 if (evalarg == NULL)
2553 clear_evalarg(&local_evalarg, NULL);
2554 else
2555 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557
2558 return OK;
2559}
2560
2561/*
2562 * Handle first level expression:
2563 * expr2 || expr2 || expr2 logical OR
2564 *
2565 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002566 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 *
2568 * Return OK or FAIL.
2569 */
2570 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002573 char_u *p;
2574 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 /*
2577 * Get the first variable.
2578 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 return FAIL;
2581
2582 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002585 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 evalarg_T *evalarg_used = evalarg;
2589 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002590 int evaluate;
2591 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 long result = FALSE;
2593 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002594 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002595 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002596
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 if (evalarg == NULL)
2598 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002599 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002601 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002602 orig_flags = evalarg_used->eval_flags;
2603 evaluate = orig_flags & EVAL_EVALUATE;
2604 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002606 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002607 result = tv_get_bool_chk(rettv, &error);
2608 else if (tv_get_number_chk(rettv, &error) != 0)
2609 result = TRUE;
2610 clear_tv(rettv);
2611 if (error)
2612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 while (p[0] == '|' && p[1] == '|')
2619 {
2620 if (getnext)
2621 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002622 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002623 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002624 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 {
2626 error_white_both(p, 2);
2627 clear_tv(rettv);
2628 return FAIL;
2629 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002630 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002631 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002632
2633 /*
2634 * Get the second variable.
2635 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002636 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002637 {
mityu4ac198c2021-05-28 17:52:40 +02002638 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002639 clear_tv(rettv);
2640 return FAIL;
2641 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002642 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2643 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002646 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 /*
2649 * Compute the result.
2650 */
2651 if (evaluate && !result)
2652 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002654 result = tv_get_bool_chk(&var2, &error);
2655 else if (tv_get_number_chk(&var2, &error) != 0)
2656 result = TRUE;
2657 clear_tv(&var2);
2658 if (error)
2659 return FAIL;
2660 }
2661 if (evaluate)
2662 {
2663 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002664 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002665 rettv->v_type = VAR_BOOL;
2666 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 }
2668 else
2669 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002670 rettv->v_type = VAR_NUMBER;
2671 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002672 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674
2675 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002677
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002678 if (evalarg == NULL)
2679 clear_evalarg(&local_evalarg, NULL);
2680 else
2681 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683
2684 return OK;
2685}
2686
2687/*
2688 * Handle second level expression:
2689 * expr3 && expr3 && expr3 logical AND
2690 *
2691 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002692 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 *
2694 * Return OK or FAIL.
2695 */
2696 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002699 char_u *p;
2700 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 /*
2703 * Get the first variable.
2704 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707
2708 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002711 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 evalarg_T *evalarg_used = evalarg;
2715 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 int orig_flags;
2717 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 long result = TRUE;
2719 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002720 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002721 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002722
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (evalarg == NULL)
2724 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002725 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002727 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728 orig_flags = evalarg_used->eval_flags;
2729 evaluate = orig_flags & EVAL_EVALUATE;
2730 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002731 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002732 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002733 result = tv_get_bool_chk(rettv, &error);
2734 else if (tv_get_number_chk(rettv, &error) == 0)
2735 result = FALSE;
2736 clear_tv(rettv);
2737 if (error)
2738 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740
2741 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002742 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002744 while (p[0] == '&' && p[1] == '&')
2745 {
2746 if (getnext)
2747 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002748 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002749 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002750 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002751 {
2752 error_white_both(p, 2);
2753 clear_tv(rettv);
2754 return FAIL;
2755 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002756 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002757 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002758
2759 /*
2760 * Get the second variable.
2761 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002762 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002763 {
mityu4ac198c2021-05-28 17:52:40 +02002764 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002765 clear_tv(rettv);
2766 return FAIL;
2767 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002768 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2769 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002771 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002774
2775 /*
2776 * Compute the result.
2777 */
2778 if (evaluate && result)
2779 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002780 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002781 result = tv_get_bool_chk(&var2, &error);
2782 else if (tv_get_number_chk(&var2, &error) == 0)
2783 result = FALSE;
2784 clear_tv(&var2);
2785 if (error)
2786 return FAIL;
2787 }
2788 if (evaluate)
2789 {
2790 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002791 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002792 rettv->v_type = VAR_BOOL;
2793 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002794 }
2795 else
2796 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002797 rettv->v_type = VAR_NUMBER;
2798 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002799 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002800 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801
2802 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002804
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 if (evalarg == NULL)
2806 clear_evalarg(&local_evalarg, NULL);
2807 else
2808 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
2811 return OK;
2812}
2813
2814/*
2815 * Handle third level expression:
2816 * var1 == var2
2817 * var1 =~ var2
2818 * var1 != var2
2819 * var1 !~ var2
2820 * var1 > var2
2821 * var1 >= var2
2822 * var1 < var2
2823 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002824 * var1 is var2
2825 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 *
2827 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002828 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 *
2830 * Return OK or FAIL.
2831 */
2832 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002833eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002836 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002837 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002839 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 /*
2842 * Get the first variable.
2843 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 return FAIL;
2846
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002847 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002848 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002851 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002853 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002855 typval_T var2;
2856 int ic;
2857 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002858 int evaluate = evalarg == NULL
2859 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002860 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002861
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002862 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002863 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002864 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002865 p = *arg;
2866 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002867 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2868 {
mityu4ac198c2021-05-28 17:52:40 +02002869 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002870 clear_tv(rettv);
2871 return FAIL;
2872 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002873
Bram Moolenaar696ba232020-07-29 21:20:41 +02002874 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2875 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002876 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002877 clear_tv(rettv);
2878 return FAIL;
2879 }
2880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002881 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (p[len] == '?')
2883 {
2884 ic = TRUE;
2885 ++len;
2886 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002887 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 else if (p[len] == '#')
2889 {
2890 ic = FALSE;
2891 ++len;
2892 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002893 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002895 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896
2897 /*
2898 * Get the second variable.
2899 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002900 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2901 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002902 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002903 clear_tv(rettv);
2904 return FAIL;
2905 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002906 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002907 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 return FAIL;
2911 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002912 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002913 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002914 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002915
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002916 // use the line of the comparison for messages
2917 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002918 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002919 {
2920 ret = FAIL;
2921 clear_tv(rettv);
2922 }
2923 else
2924 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002925 clear_tv(&var2);
2926 return ret;
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929
2930 return OK;
2931}
2932
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002933/*
2934 * Make a copy of blob "tv1" and append blob "tv2".
2935 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 void
2937eval_addblob(typval_T *tv1, typval_T *tv2)
2938{
2939 blob_T *b1 = tv1->vval.v_blob;
2940 blob_T *b2 = tv2->vval.v_blob;
2941 blob_T *b = blob_alloc();
2942 int i;
2943
2944 if (b != NULL)
2945 {
2946 for (i = 0; i < blob_len(b1); i++)
2947 ga_append(&b->bv_ga, blob_get(b1, i));
2948 for (i = 0; i < blob_len(b2); i++)
2949 ga_append(&b->bv_ga, blob_get(b2, i));
2950
2951 clear_tv(tv1);
2952 rettv_blob_set(tv1, b);
2953 }
2954}
2955
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002956/*
2957 * Make a copy of list "tv1" and append list "tv2".
2958 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 int
2960eval_addlist(typval_T *tv1, typval_T *tv2)
2961{
2962 typval_T var3;
2963
2964 // concatenate Lists
2965 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2966 {
2967 clear_tv(tv1);
2968 clear_tv(tv2);
2969 return FAIL;
2970 }
2971 clear_tv(tv1);
2972 *tv1 = var3;
2973 return OK;
2974}
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002978 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002980 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002981 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 *
2983 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002984 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 *
2986 * Return OK or FAIL.
2987 */
2988 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 /*
2992 * Get the first variable.
2993 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 return FAIL;
2996
2997 /*
2998 * Repeat computing, until no '+', '-' or '.' is following.
2999 */
3000 for (;;)
3001 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003002 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003003 int getnext;
3004 char_u *p;
3005 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003006 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003007 int concat;
3008 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003009 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003010
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003011 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003012 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003013 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003014 p = eval_next_non_blank(*arg, evalarg, &getnext);
3015 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003016 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003017 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3018 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003020 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3021 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003022
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003023 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003024 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003025 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003026 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003027 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003028 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003029 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003030 {
mityu4ac198c2021-05-28 17:52:40 +02003031 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003032 clear_tv(rettv);
3033 return FAIL;
3034 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003035 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003036 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003037 if ((op != '+' || (rettv->v_type != VAR_LIST
3038 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 && (op == '.' || rettv->v_type != VAR_FLOAT)
3041#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003042 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003043 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003044 int error = FALSE;
3045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003046 // For "list + ...", an illegal use of the first operand as
3047 // a number cannot be determined before evaluating the 2nd
3048 // operand: if this is also a list, all is ok.
3049 // For "something . ...", "something - ..." or "non-list + ...",
3050 // we know that the first operand needs to be a string or number
3051 // without evaluating the 2nd operand. So check before to avoid
3052 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003053 if (op != '.')
3054 tv_get_number_chk(rettv, &error);
3055 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 {
3057 clear_tv(rettv);
3058 return FAIL;
3059 }
3060 }
3061
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 /*
3063 * Get the second variable.
3064 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003065 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003066 {
mityu89dcb4d2021-05-28 13:50:17 +02003067 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003068 clear_tv(rettv);
3069 return FAIL;
3070 }
3071 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003072 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 return FAIL;
3076 }
3077
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003078 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 /*
3081 * Compute the result.
3082 */
3083 if (op == '.')
3084 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003085 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3086 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003087 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088
Bram Moolenaar2e086612020-08-25 22:37:48 +02003089 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003090 || var2.v_type == VAR_CHANNEL
3091 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003092 semsg(_(e_using_invalid_value_as_string_str),
3093 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003094#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003095 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003096 {
3097 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3098 var2.vval.v_float);
3099 s2 = buf2;
3100 }
3101#endif
3102 else
3103 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003104 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003105 {
3106 clear_tv(rettv);
3107 clear_tv(&var2);
3108 return FAIL;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003111 clear_tv(rettv);
3112 rettv->v_type = VAR_STRING;
3113 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003115 else if (op == '+' && rettv->v_type == VAR_BLOB
3116 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003118 else if (op == '+' && rettv->v_type == VAR_LIST
3119 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003120 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003122 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 else
3125 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 int error = FALSE;
3127 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 float_T f1 = 0, f2 = 0;
3130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133 f1 = rettv->vval.v_float;
3134 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 else
3137#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003139 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (error)
3141 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003142 // This can only happen for "list + non-list" or
3143 // "blob + non-blob". For "non-list + ..." or
3144 // "something - ...", we returned before evaluating the
3145 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003147 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148 return FAIL;
3149 }
3150#ifdef FEAT_FLOAT
3151 if (var2.v_type == VAR_FLOAT)
3152 f1 = n1;
3153#endif
3154 }
3155#ifdef FEAT_FLOAT
3156 if (var2.v_type == VAR_FLOAT)
3157 {
3158 f2 = var2.vval.v_float;
3159 n2 = 0;
3160 }
3161 else
3162#endif
3163 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003164 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 if (error)
3166 {
3167 clear_tv(rettv);
3168 clear_tv(&var2);
3169 return FAIL;
3170 }
3171#ifdef FEAT_FLOAT
3172 if (rettv->v_type == VAR_FLOAT)
3173 f2 = n2;
3174#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003176 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177
3178#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003179 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3181 {
3182 if (op == '+')
3183 f1 = f1 + f2;
3184 else
3185 f1 = f1 - f2;
3186 rettv->v_type = VAR_FLOAT;
3187 rettv->vval.v_float = f1;
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190#endif
3191 {
3192 if (op == '+')
3193 n1 = n1 + n2;
3194 else
3195 n1 = n1 - n2;
3196 rettv->v_type = VAR_NUMBER;
3197 rettv->vval.v_number = n1;
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003200 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 }
3203 return OK;
3204}
3205
3206/*
3207 * Handle fifth level expression:
3208 * * number multiplication
3209 * / number division
3210 * % number modulo
3211 *
3212 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003213 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 *
3215 * Return OK or FAIL.
3216 */
3217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218eval6(
3219 char_u **arg,
3220 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003221 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003222 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003224#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003225 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 /*
3229 * Get the first variable.
3230 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003231 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return FAIL;
3233
3234 /*
3235 * Repeat computing, until no '*', '/' or '%' is following.
3236 */
3237 for (;;)
3238 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003239 int evaluate;
3240 int getnext;
3241 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003242 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003243 int op;
3244 varnumber_T n1, n2;
3245#ifdef FEAT_FLOAT
3246 float_T f1, f2;
3247#endif
3248 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003249
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003250 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003251 p = eval_next_non_blank(*arg, evalarg, &getnext);
3252 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003253 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003255
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003256 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003257 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003258 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003259 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003260 {
3261 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3262 {
mityu4ac198c2021-05-28 17:52:40 +02003263 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003264 clear_tv(rettv);
3265 return FAIL;
3266 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003267 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003270#ifdef FEAT_FLOAT
3271 f1 = 0;
3272 f2 = 0;
3273#endif
3274 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003275 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003277#ifdef FEAT_FLOAT
3278 if (rettv->v_type == VAR_FLOAT)
3279 {
3280 f1 = rettv->vval.v_float;
3281 use_float = TRUE;
3282 n1 = 0;
3283 }
3284 else
3285#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003286 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 if (error)
3289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 else
3292 n1 = 0;
3293
3294 /*
3295 * Get the second variable.
3296 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003297 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3298 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003299 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003300 clear_tv(rettv);
3301 return FAIL;
3302 }
3303 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003304 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 return FAIL;
3306
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003307 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003309#ifdef FEAT_FLOAT
3310 if (var2.v_type == VAR_FLOAT)
3311 {
3312 if (!use_float)
3313 {
3314 f1 = n1;
3315 use_float = TRUE;
3316 }
3317 f2 = var2.vval.v_float;
3318 n2 = 0;
3319 }
3320 else
3321#endif
3322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003323 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003324 clear_tv(&var2);
3325 if (error)
3326 return FAIL;
3327#ifdef FEAT_FLOAT
3328 if (use_float)
3329 f2 = n2;
3330#endif
3331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 /*
3334 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003335 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337#ifdef FEAT_FLOAT
3338 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340 if (op == '*')
3341 f1 = f1 * f2;
3342 else if (op == '/')
3343 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003344# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003345 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003346 if (f2 == 0.0)
3347 {
3348 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003349 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003350 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003351 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003352 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003353 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003354 }
3355 else
3356 f1 = f1 / f2;
3357# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // We rely on the floating point library to handle divide
3359 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003360 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003361# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003365 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003366 return FAIL;
3367 }
3368 rettv->v_type = VAR_FLOAT;
3369 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003374 int failed = FALSE;
3375
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003376 if (op == '*')
3377 n1 = n1 * n2;
3378 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003379 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003381 n1 = num_modulus(n1, n2, &failed);
3382 if (failed)
3383 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003384
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003385 rettv->v_type = VAR_NUMBER;
3386 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389 }
3390
3391 return OK;
3392}
3393
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003394/*
3395 * Handle a type cast before a base level expression.
3396 * "arg" must point to the first non-white of the expression.
3397 * "arg" is advanced to just after the recognized expression.
3398 * Return OK or FAIL.
3399 */
3400 static int
3401eval7t(
3402 char_u **arg,
3403 typval_T *rettv,
3404 evalarg_T *evalarg,
3405 int want_string) // after "." operator
3406{
3407 type_T *want_type = NULL;
3408 garray_T type_list; // list of pointers to allocated types
3409 int res;
3410 int evaluate = evalarg == NULL ? 0
3411 : (evalarg->eval_flags & EVAL_EVALUATE);
3412
3413 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003414 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3415 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003416 {
3417 ++*arg;
3418 ga_init2(&type_list, sizeof(type_T *), 10);
3419 want_type = parse_type(arg, &type_list, TRUE);
3420 if (want_type == NULL && (evaluate || **arg != '>'))
3421 {
3422 clear_type_list(&type_list);
3423 return FAIL;
3424 }
3425
3426 if (**arg != '>')
3427 {
3428 if (*skipwhite(*arg) == '>')
3429 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3430 else
3431 emsg(_(e_missing_gt));
3432 clear_type_list(&type_list);
3433 return FAIL;
3434 }
3435 ++*arg;
3436 *arg = skipwhite_and_linebreak(*arg, evalarg);
3437 }
3438
3439 res = eval7(arg, rettv, evalarg, want_string);
3440
3441 if (want_type != NULL && evaluate)
3442 {
3443 if (res == OK)
3444 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003445 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3446 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003447
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003448 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003449 {
3450 if (want_type == &t_bool && actual != &t_bool
3451 && (actual->tt_flags & TTFLAG_BOOL_OK))
3452 {
3453 int n = tv2bool(rettv);
3454
3455 // can use "0" and "1" for boolean in some places
3456 clear_tv(rettv);
3457 rettv->v_type = VAR_BOOL;
3458 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3459 }
3460 else
3461 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003462 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003463
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003464 where.wt_variable = TRUE;
3465 res = check_type(want_type, actual, TRUE, where);
3466 }
3467 }
3468 }
3469 clear_type_list(&type_list);
3470 }
3471
3472 return res;
3473}
3474
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003475 int
3476eval_leader(char_u **arg, int vim9)
3477{
3478 char_u *s = *arg;
3479 char_u *p = *arg;
3480
3481 while (*p == '!' || *p == '-' || *p == '+')
3482 {
3483 char_u *n = skipwhite(p + 1);
3484
3485 // ++, --, -+ and +- are not accepted in Vim9 script
3486 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3487 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003488 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003489 return FAIL;
3490 }
3491 p = n;
3492 }
3493 *arg = p;
3494 return OK;
3495}
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003498 * Check for a predefined value "true", "false" and "null.*".
3499 * Return OK when recognized.
3500 */
3501 int
3502handle_predefined(char_u *s, int len, typval_T *rettv)
3503{
3504 switch (len)
3505 {
3506 case 4: if (STRNCMP(s, "true", 4) == 0)
3507 {
3508 rettv->v_type = VAR_BOOL;
3509 rettv->vval.v_number = VVAL_TRUE;
3510 return OK;
3511 }
3512 if (STRNCMP(s, "null", 4) == 0)
3513 {
3514 rettv->v_type = VAR_SPECIAL;
3515 rettv->vval.v_number = VVAL_NULL;
3516 return OK;
3517 }
3518 break;
3519 case 5: if (STRNCMP(s, "false", 5) == 0)
3520 {
3521 rettv->v_type = VAR_BOOL;
3522 rettv->vval.v_number = VVAL_FALSE;
3523 return OK;
3524 }
3525 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003526 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3527 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003529 rettv->v_type = VAR_JOB;
3530 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003531#else
3532 rettv->v_type = VAR_SPECIAL;
3533 rettv->vval.v_number = VVAL_NULL;
3534#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003535 return OK;
3536 }
3537 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003538 case 9:
3539 if (STRNCMP(s, "null_", 5) != 0)
3540 break;
3541 if (STRNCMP(s + 5, "list", 4) == 0)
3542 {
3543 rettv->v_type = VAR_LIST;
3544 rettv->vval.v_list = NULL;
3545 return OK;
3546 }
3547 if (STRNCMP(s + 5, "dict", 4) == 0)
3548 {
3549 rettv->v_type = VAR_DICT;
3550 rettv->vval.v_dict = NULL;
3551 return OK;
3552 }
3553 if (STRNCMP(s + 5, "blob", 4) == 0)
3554 {
3555 rettv->v_type = VAR_BLOB;
3556 rettv->vval.v_blob = NULL;
3557 return OK;
3558 }
3559 break;
3560 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3561 {
3562 rettv->v_type = VAR_STRING;
3563 rettv->vval.v_string = NULL;
3564 return OK;
3565 }
3566 break;
3567 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003568 if (STRNCMP(s, "null_channel", 12) == 0)
3569 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003571 rettv->v_type = VAR_CHANNEL;
3572 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003573#else
3574 rettv->v_type = VAR_SPECIAL;
3575 rettv->vval.v_number = VVAL_NULL;
3576#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003577 return OK;
3578 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003579 if (STRNCMP(s, "null_partial", 12) == 0)
3580 {
3581 rettv->v_type = VAR_PARTIAL;
3582 rettv->vval.v_partial = NULL;
3583 return OK;
3584 }
3585 break;
3586 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3587 {
3588 rettv->v_type = VAR_FUNC;
3589 rettv->vval.v_string = NULL;
3590 return OK;
3591 }
3592 break;
3593 }
3594 return FAIL;
3595}
3596
3597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 * Handle sixth level expression:
3599 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003600 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003601 * "string" string constant
3602 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 * &option-name option value
3604 * @r register contents
3605 * identifier variable value
3606 * function() function call
3607 * $VAR environment variable
3608 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003611 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003612 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 *
3614 * Also handle:
3615 * ! in front logical NOT
3616 * - in front unary minus
3617 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003618 * trailing [] subscript in String or List
3619 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003620 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 *
3622 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003623 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 *
3625 * Return OK or FAIL.
3626 */
3627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628eval7(
3629 char_u **arg,
3630 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003631 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003634 int evaluate = evalarg != NULL
3635 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 int len;
3637 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003638 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 char_u *start_leader, *end_leader;
3640 int ret = OK;
3641 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003642 static int recurse = 0;
3643 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003646 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003647 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003652 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003655 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 end_leader = *arg;
3658
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003659 if (**arg == '.' && (!isdigit(*(*arg + 1))
3660#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003661 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003662#endif
3663 ))
3664 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003665 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003666 ++*arg;
3667 return FAIL;
3668 }
3669
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003670 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003671 // and crash. With MSVC the stack is smaller.
3672 if (recurse ==
3673#ifdef _MSC_VER
3674 300
3675#else
3676 1000
3677#endif
3678 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003679 {
3680 semsg(_(e_expression_too_recursive_str), *arg);
3681 return FAIL;
3682 }
3683 ++recurse;
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 switch (**arg)
3686 {
3687 /*
3688 * Number constant.
3689 */
3690 case '0':
3691 case '1':
3692 case '2':
3693 case '3':
3694 case '4':
3695 case '5':
3696 case '6':
3697 case '7':
3698 case '8':
3699 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003700 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003701
3702 // Apply prefixed "-" and "+" now. Matters especially when
3703 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003704 if (ret == OK && evaluate && end_leader > start_leader
3705 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003706 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 break;
3708
3709 /*
3710 * String constant: "string".
3711 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003712 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 break;
3714
3715 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003718 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003719 break;
3720
3721 /*
3722 * List: [expr, expr]
3723 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003724 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 break;
3726
3727 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003728 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003729 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003730 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003731 {
3732 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3733 }
3734 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003735 {
3736 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003737 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003738 }
3739 else
3740 ret = NOTDONE;
3741 break;
3742
3743 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003744 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003745 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003746 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003747 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003748 ret = NOTDONE;
3749 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003750 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003751 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003752 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753 break;
3754
3755 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003756 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003758 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 break;
3760
3761 /*
3762 * Environment variable: $VAR.
3763 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003764 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 break;
3766
3767 /*
3768 * Register contents: @r.
3769 */
3770 case '@': ++*arg;
3771 if (evaluate)
3772 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003773 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003774 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003775 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003776 emsg_invreg(**arg);
3777 else
3778 {
3779 rettv->v_type = VAR_STRING;
3780 rettv->vval.v_string = get_reg_contents(**arg,
3781 GREG_EXPR_SRC);
3782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 if (**arg != NUL)
3785 ++*arg;
3786 break;
3787
3788 /*
3789 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003790 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003792 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003793 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003794 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003795 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003796 if (ret == OK && evaluate)
3797 {
3798 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3799
Bram Moolenaara9931532021-06-12 15:58:16 +02003800 // Compile it here to get the return type. The return
3801 // type is optional, when it's missing use t_unknown.
3802 // This is recognized in compile_return().
3803 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3804 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003805 if (compile_def_function(ufunc, FALSE,
3806 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003807 {
3808 clear_tv(rettv);
3809 ret = FAIL;
3810 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003811 }
3812 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003813 if (ret == NOTDONE)
3814 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003815 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003816 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003817
Bram Moolenaar9215f012020-06-27 21:18:00 +02003818 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003819 if (**arg == ')')
3820 ++*arg;
3821 else if (ret == OK)
3822 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003823 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003824 clear_tv(rettv);
3825 ret = FAIL;
3826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 break;
3829
Bram Moolenaar8c711452005-01-14 21:53:12 +00003830 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003833
3834 if (ret == NOTDONE)
3835 {
3836 /*
3837 * Must be a variable or function name.
3838 * Can also be a curly-braces kind of name: {expr}.
3839 */
3840 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003841 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003842 if (alias != NULL)
3843 s = alias;
3844
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003845 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003846 ret = FAIL;
3847 else
3848 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003849 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3850
Bram Moolenaar4525a572022-02-13 11:57:33 +00003851 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003852 {
3853 emsg(_(e_cannot_use_underscore_here));
3854 ret = FAIL;
3855 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003856 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003857 && s[0] == 's' && s[1] == ':')
3858 {
3859 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3860 ret = FAIL;
3861 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003862 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003863 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003864 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003865 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003866 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003867 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003868 else if (flags & EVAL_CONSTANT)
3869 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003870 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003871 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003872 // get the value of "true", "false", etc. or a variable
3873 ret = FAIL;
3874 if (vim9script)
3875 ret = handle_predefined(s, len, rettv);
3876 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003877 {
3878 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003879 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003880 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003881 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003882 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003883 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003884 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003885 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003886 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003887 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003889 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003890 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 }
3892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3894 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003895 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003896 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 /*
3899 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3900 */
3901 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003902 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003903
3904 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003905 return ret;
3906}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003908/*
3909 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003910 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003911 * Adjusts "end_leaderp" until it is at "start_leader".
3912 */
3913 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003914eval7_leader(
3915 typval_T *rettv,
3916 int numeric_only,
3917 char_u *start_leader,
3918 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003919{
3920 char_u *end_leader = *end_leaderp;
3921 int ret = OK;
3922 int error = FALSE;
3923 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003924 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003925 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003926#ifdef FEAT_FLOAT
3927 float_T f = 0.0;
3928
3929 if (rettv->v_type == VAR_FLOAT)
3930 f = rettv->vval.v_float;
3931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003932#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003933 {
3934 while (VIM_ISWHITE(end_leader[-1]))
3935 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003936 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003937 val = tv2bool(rettv);
3938 else
3939 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003940 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003941 if (error)
3942 {
3943 clear_tv(rettv);
3944 ret = FAIL;
3945 }
3946 else
3947 {
3948 while (end_leader > start_leader)
3949 {
3950 --end_leader;
3951 if (*end_leader == '!')
3952 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003953 if (numeric_only)
3954 {
3955 ++end_leader;
3956 break;
3957 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003958#ifdef FEAT_FLOAT
3959 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003960 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003961 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003962 {
3963 rettv->v_type = VAR_BOOL;
3964 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3965 }
3966 else
3967 f = !f;
3968 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003969 else
3970#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003971 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003972 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003973 type = VAR_BOOL;
3974 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003975 }
3976 else if (*end_leader == '-')
3977 {
3978#ifdef FEAT_FLOAT
3979 if (rettv->v_type == VAR_FLOAT)
3980 f = -f;
3981 else
3982#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003983 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003984 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003985 type = VAR_NUMBER;
3986 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003987 }
3988 }
3989#ifdef FEAT_FLOAT
3990 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003993 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003996#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003998 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003999 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004000 rettv->v_type = type;
4001 else
4002 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004003 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004006 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return ret;
4008}
4009
4010/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004011 * Call the function referred to in "rettv".
4012 */
4013 static int
4014call_func_rettv(
4015 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004016 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004017 typval_T *rettv,
4018 int evaluate,
4019 dict_T *selfdict,
4020 typval_T *basetv)
4021{
4022 partial_T *pt = NULL;
4023 funcexe_T funcexe;
4024 typval_T functv;
4025 char_u *s;
4026 int ret;
4027
4028 // need to copy the funcref so that we can clear rettv
4029 if (evaluate)
4030 {
4031 functv = *rettv;
4032 rettv->v_type = VAR_UNKNOWN;
4033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004034 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004035 if (functv.v_type == VAR_PARTIAL)
4036 {
4037 pt = functv.vval.v_partial;
4038 s = partial_name(pt);
4039 }
4040 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004041 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004042 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004043 if (s == NULL || *s == NUL)
4044 {
4045 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004046 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004047 goto theend;
4048 }
4049 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004050 }
4051 else
4052 s = (char_u *)"";
4053
Bram Moolenaara80faa82020-04-12 19:37:17 +02004054 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004055 funcexe.fe_firstline = curwin->w_cursor.lnum;
4056 funcexe.fe_lastline = curwin->w_cursor.lnum;
4057 funcexe.fe_evaluate = evaluate;
4058 funcexe.fe_partial = pt;
4059 funcexe.fe_selfdict = selfdict;
4060 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004061 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004062
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004063theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // Clear the funcref afterwards, so that deleting it while
4065 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004066 if (evaluate)
4067 clear_tv(&functv);
4068
4069 return ret;
4070}
4071
4072/*
4073 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004074 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004075 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4076 */
4077 static int
4078eval_lambda(
4079 char_u **arg,
4080 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004081 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004082 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004083{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004084 int evaluate = evalarg != NULL
4085 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004086 typval_T base = *rettv;
4087 int ret;
4088
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004089 rettv->v_type = VAR_UNKNOWN;
4090
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004091 if (**arg == '{')
4092 {
4093 // ->{lambda}()
4094 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4095 }
4096 else
4097 {
4098 // ->(lambda)()
4099 ++*arg;
4100 ret = eval1(arg, rettv, evalarg);
4101 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00004102 if (**arg == ')')
4103 {
4104 ++*arg;
4105 }
4106 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004107 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004108 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004109 ret = FAIL;
4110 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004111 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004112 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004113 return FAIL;
4114 else if (**arg != '(')
4115 {
4116 if (verbose)
4117 {
4118 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004119 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004120 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004121 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004122 }
4123 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004124 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004125 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004126 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004127 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004128
4129 // Clear the funcref afterwards, so that deleting it while
4130 // evaluating the arguments is possible (see test55).
4131 if (evaluate)
4132 clear_tv(&base);
4133
4134 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004135}
4136
4137/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004138 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004139 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004140 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4141 */
4142 static int
4143eval_method(
4144 char_u **arg,
4145 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004146 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004148{
4149 char_u *name;
4150 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004151 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004152 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004153 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004154 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004155 int evaluate = evalarg != NULL
4156 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004157
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004158 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004159
Bram Moolenaarac92e252019-08-03 21:58:38 +02004160 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004161 len = get_name_len(arg, &alias, evaluate, TRUE);
4162 if (alias != NULL)
4163 name = alias;
4164
4165 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004166 {
4167 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004168 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004169 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004170 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004171 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004172 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004173 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004174
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004175 // If there is no "(" immediately following, but there is further on,
4176 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4177 // Does not handle anything where "(" is part of the expression.
4178 *arg = skipwhite(*arg);
4179
4180 if (**arg != '(' && alias == NULL
4181 && (paren = vim_strchr(*arg, '(')) != NULL)
4182 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004183 char_u *deref;
4184
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004185 *arg = name;
4186 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004187 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4188 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004189 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004190 *arg = name + len;
4191 ret = FAIL;
4192 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004193 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004194 {
4195 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004196 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004197 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004198 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004199 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004200
4201 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004202 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004203 *arg = skipwhite(*arg);
4204
4205 if (**arg != '(')
4206 {
4207 if (verbose)
4208 semsg(_(e_missing_parenthesis_str), name);
4209 ret = FAIL;
4210 }
4211 else if (VIM_ISWHITE((*arg)[-1]))
4212 {
4213 if (verbose)
4214 emsg(_(e_no_white_space_allowed_before_parenthesis));
4215 ret = FAIL;
4216 }
4217 else
4218 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004219 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004220 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004221 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004222
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004223 // Clear the funcref afterwards, so that deleting it while
4224 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004225 if (evaluate)
4226 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004227 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004228
Bram Moolenaarac92e252019-08-03 21:58:38 +02004229 return ret;
4230}
4231
4232/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004233 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4234 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4236 */
4237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238eval_index(
4239 char_u **arg,
4240 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004241 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004242 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004243{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004244 int evaluate = evalarg != NULL
4245 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004248 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004250 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004251 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004252
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4254 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004256 init_tv(&var1);
4257 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004258 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004260 /*
4261 * dict.name
4262 */
4263 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004264 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004266 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004268 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004269 }
4270 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004272 /*
4273 * something[idx]
4274 *
4275 * Get the (first) variable from inside the [].
4276 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004277 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 if (**arg == ':')
4279 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004280 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004281 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004282 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004283 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004284 semsg(_(e_white_space_required_before_and_after_str_at_str),
4285 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004286 clear_tv(&var1);
4287 return FAIL;
4288 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004289 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004291 int error = FALSE;
4292
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004293#ifdef FEAT_FLOAT
4294 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004295 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004296 && var1.v_type == VAR_FLOAT)
4297 {
4298 var1.vval.v_string = typval_tostring(&var1, TRUE);
4299 var1.v_type = VAR_STRING;
4300 }
4301#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004302 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004303 tv_get_number_chk(&var1, &error);
4304 else
4305 error = tv_get_string_chk(&var1) == NULL;
4306 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004307 {
4308 // not a number or string
4309 clear_tv(&var1);
4310 return FAIL;
4311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004313
4314 /*
4315 * Get the second variable from inside the [:].
4316 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004317 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318 if (**arg == ':')
4319 {
4320 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004321 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004322 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004323 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004324 semsg(_(e_white_space_required_before_and_after_str_at_str),
4325 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004326 if (!empty1)
4327 clear_tv(&var1);
4328 return FAIL;
4329 }
4330 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004331 if (**arg == ']')
4332 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004333 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (!empty1)
4336 clear_tv(&var1);
4337 return FAIL;
4338 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004339 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (!empty1)
4343 clear_tv(&var1);
4344 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 return FAIL;
4346 }
4347 }
4348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004349 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004350 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004351 if (**arg != ']')
4352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004353 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004354 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 clear_tv(&var1);
4356 if (range)
4357 clear_tv(&var2);
4358 return FAIL;
4359 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004360 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 }
4362
4363 if (evaluate)
4364 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004365 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004366 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004367 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004368
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004369 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004371 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004372 clear_tv(&var2);
4373 return res;
4374 }
4375 return OK;
4376}
4377
4378/*
4379 * Check if "rettv" can have an [index] or [sli:ce]
4380 */
4381 int
4382check_can_index(typval_T *rettv, int evaluate, int verbose)
4383{
4384 switch (rettv->v_type)
4385 {
4386 case VAR_FUNC:
4387 case VAR_PARTIAL:
4388 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004389 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004390 return FAIL;
4391 case VAR_FLOAT:
4392#ifdef FEAT_FLOAT
4393 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004394 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004395 return FAIL;
4396#endif
4397 case VAR_BOOL:
4398 case VAR_SPECIAL:
4399 case VAR_JOB:
4400 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004401 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004402 if (verbose)
4403 emsg(_(e_cannot_index_special_variable));
4404 return FAIL;
4405 case VAR_UNKNOWN:
4406 case VAR_ANY:
4407 case VAR_VOID:
4408 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004410 emsg(_(e_cannot_index_special_variable));
4411 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004413 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 case VAR_STRING:
4416 case VAR_LIST:
4417 case VAR_DICT:
4418 case VAR_BLOB:
4419 break;
4420 case VAR_NUMBER:
4421 if (in_vim9script())
4422 emsg(_(e_cannot_index_number));
4423 break;
4424 }
4425 return OK;
4426}
4427
4428/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004429 * slice() function
4430 */
4431 void
4432f_slice(typval_T *argvars, typval_T *rettv)
4433{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004434 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004435 && ((argvars[0].v_type != VAR_STRING
4436 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004437 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004438 && check_for_list_arg(argvars, 0) == FAIL)
4439 || check_for_number_arg(argvars, 1) == FAIL
4440 || check_for_opt_number_arg(argvars, 2) == FAIL))
4441 return;
4442
Bram Moolenaar6601b622021-01-13 21:47:15 +01004443 if (check_can_index(argvars, TRUE, FALSE) == OK)
4444 {
4445 copy_tv(argvars, rettv);
4446 eval_index_inner(rettv, TRUE, argvars + 1,
4447 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4448 TRUE, NULL, 0, FALSE);
4449 }
4450}
4451
4452/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004453 * Apply index or range to "rettv".
4454 * "var1" is the first index, NULL for [:expr].
4455 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004456 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4457 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004458 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4459 */
4460 int
4461eval_index_inner(
4462 typval_T *rettv,
4463 int is_range,
4464 typval_T *var1,
4465 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004466 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004467 char_u *key,
4468 int keylen,
4469 int verbose)
4470{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004471 varnumber_T n1, n2 = 0;
4472 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004473
4474 n1 = 0;
4475 if (var1 != NULL && rettv->v_type != VAR_DICT)
4476 n1 = tv_get_number(var1);
4477
4478 if (is_range)
4479 {
4480 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004482 if (verbose)
4483 emsg(_(e_cannot_slice_dictionary));
4484 return FAIL;
4485 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004486 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004487 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004488 else
4489 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004490 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004491
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004492 switch (rettv->v_type)
4493 {
4494 case VAR_UNKNOWN:
4495 case VAR_ANY:
4496 case VAR_VOID:
4497 case VAR_FUNC:
4498 case VAR_PARTIAL:
4499 case VAR_FLOAT:
4500 case VAR_BOOL:
4501 case VAR_SPECIAL:
4502 case VAR_JOB:
4503 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004504 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 break; // not evaluating, skipping over subscript
4506
4507 case VAR_NUMBER:
4508 case VAR_STRING:
4509 {
4510 char_u *s = tv_get_string(rettv);
4511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004512 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004513 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004514 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004515 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004516 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004517 else
4518 s = char_from_string(s, n1);
4519 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004520 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // The resulting variable is a substring. If the indexes
4523 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 if (n1 < 0)
4525 {
4526 n1 = len + n1;
4527 if (n1 < 0)
4528 n1 = 0;
4529 }
4530 if (n2 < 0)
4531 n2 = len + n2;
4532 else if (n2 >= len)
4533 n2 = len;
4534 if (n1 >= len || n2 < 0 || n1 > n2)
4535 s = NULL;
4536 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004537 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538 }
4539 else
4540 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 // The resulting variable is a string of a single
4542 // character. If the index is too big or negative the
4543 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 if (n1 >= len || n1 < 0)
4545 s = NULL;
4546 else
4547 s = vim_strnsave(s + n1, 1);
4548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 clear_tv(rettv);
4550 rettv->v_type = VAR_STRING;
4551 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004552 }
4553 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004556 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4557 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004558 break;
4559
4560 case VAR_LIST:
4561 if (var1 == NULL)
4562 n1 = 0;
4563 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004564 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004565 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004566 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004567 return FAIL;
4568 break;
4569
4570 case VAR_DICT:
4571 {
4572 dictitem_T *item;
4573 typval_T tmp;
4574
4575 if (key == NULL)
4576 {
4577 key = tv_get_string_chk(var1);
4578 if (key == NULL)
4579 return FAIL;
4580 }
4581
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004582 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004583
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004585 {
4586 if (verbose)
4587 {
4588 if (keylen > 0)
4589 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004590 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004591 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004592 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004593 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004594
4595 copy_tv(&item->di_tv, &tmp);
4596 clear_tv(rettv);
4597 *rettv = tmp;
4598 }
4599 break;
4600 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 return OK;
4602}
4603
4604/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004605 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004606 */
4607 char_u *
4608partial_name(partial_T *pt)
4609{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004610 if (pt != NULL)
4611 {
4612 if (pt->pt_name != NULL)
4613 return pt->pt_name;
4614 if (pt->pt_func != NULL)
4615 return pt->pt_func->uf_name;
4616 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004617 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004618}
4619
Bram Moolenaarddecc252016-04-06 22:59:37 +02004620 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004622{
4623 int i;
4624
4625 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004626 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004627 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004628 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004629 if (pt->pt_name != NULL)
4630 {
4631 func_unref(pt->pt_name);
4632 vim_free(pt->pt_name);
4633 }
4634 else
4635 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004636
Bram Moolenaar54656012021-06-09 20:50:46 +02004637 // "out_up" is no longer used, decrement refcount on partial that owns it.
4638 partial_unref(pt->pt_outer.out_up_partial);
4639
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004640 // Using pt_outer from another partial.
4641 partial_unref(pt->pt_outer_partial);
4642
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004643 // Decrease the reference count for the context of a closure. If down
4644 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004645 if (pt->pt_funcstack != NULL)
4646 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004647 --pt->pt_funcstack->fs_refcount;
4648 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004649 }
4650
Bram Moolenaarddecc252016-04-06 22:59:37 +02004651 vim_free(pt);
4652}
4653
4654/*
4655 * Unreference a closure: decrement the reference count and free it when it
4656 * becomes zero.
4657 */
4658 void
4659partial_unref(partial_T *pt)
4660{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004661 if (pt != NULL)
4662 {
4663 if (--pt->pt_refcount <= 0)
4664 partial_free(pt);
4665
4666 // If the reference count goes down to one, the funcstack may be the
4667 // only reference and can be freed if no other partials reference it.
4668 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4669 funcstack_check_refcount(pt->pt_funcstack);
4670 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004671}
4672
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004673/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004674 * Return the next (unique) copy ID.
4675 * Used for serializing nested structures.
4676 */
4677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004678get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004679{
4680 current_copyID += COPYID_INC;
4681 return current_copyID;
4682}
4683
4684/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004685 * Garbage collection for lists and dictionaries.
4686 *
4687 * We use reference counts to be able to free most items right away when they
4688 * are no longer used. But for composite items it's possible that it becomes
4689 * unused while the reference count is > 0: When there is a recursive
4690 * reference. Example:
4691 * :let l = [1, 2, 3]
4692 * :let d = {9: l}
4693 * :let l[1] = d
4694 *
4695 * Since this is quite unusual we handle this with garbage collection: every
4696 * once in a while find out which lists and dicts are not referenced from any
4697 * variable.
4698 *
4699 * Here is a good reference text about garbage collection (refers to Python
4700 * but it applies to all reference-counting mechanisms):
4701 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004702 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004703
4704/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004705 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004706 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004708 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004709 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004710garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004711{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004712 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004713 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004714 buf_T *buf;
4715 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004716 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004717 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004718
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004719 if (!testing)
4720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004721 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004722 want_garbage_collect = FALSE;
4723 may_garbage_collect = FALSE;
4724 garbage_collect_at_exit = FALSE;
4725 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004726
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004727 // The execution stack can grow big, limit the size.
4728 if (exestack.ga_maxlen - exestack.ga_len > 500)
4729 {
4730 size_t new_len;
4731 char_u *pp;
4732 int n;
4733
4734 // Keep 150% of the current size, with a minimum of the growth size.
4735 n = exestack.ga_len / 2;
4736 if (n < exestack.ga_growsize)
4737 n = exestack.ga_growsize;
4738
4739 // Don't make it bigger though.
4740 if (exestack.ga_len + n < exestack.ga_maxlen)
4741 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004742 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004743 pp = vim_realloc(exestack.ga_data, new_len);
4744 if (pp == NULL)
4745 return FAIL;
4746 exestack.ga_maxlen = exestack.ga_len + n;
4747 exestack.ga_data = pp;
4748 }
4749 }
4750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 // We advance by two because we add one for items referenced through
4752 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004753 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004754
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004755 /*
4756 * 1. Go through all accessible variables and mark all lists and dicts
4757 * with copyID.
4758 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // Don't free variables in the previous_funccal list unless they are only
4761 // referenced through previous_funccal. This must be first, because if
4762 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004766 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004768 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004769 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004770 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4771 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004774 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004775 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4776 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004777 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004778 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4779 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004780#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004781 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004782 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4783 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004784 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004785 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004786 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4787 NULL, NULL);
4788#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004790 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004791 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004792 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4793 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004795 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004798 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004801 abort = abort || set_ref_in_functions(copyID);
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004805
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004806 // funcstacks keep variables for closures
4807 abort = abort || set_ref_in_funcstacks(copyID);
4808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004810 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004811
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004812 // callbacks in buffers
4813 abort = abort || set_ref_in_buffers(copyID);
4814
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004815 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4816 abort = abort || set_ref_in_insexpand_funcs(copyID);
4817
4818 // 'operatorfunc' callback
4819 abort = abort || set_ref_in_opfunc(copyID);
4820
4821 // 'tagfunc' callback
4822 abort = abort || set_ref_in_tagfunc(copyID);
4823
4824 // 'imactivatefunc' and 'imstatusfunc' callbacks
4825 abort = abort || set_ref_in_im_funcs(copyID);
4826
Bram Moolenaar1dced572012-04-05 16:54:08 +02004827#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004828 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004829#endif
4830
Bram Moolenaardb913952012-06-29 12:54:53 +02004831#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004832 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004833#endif
4834
4835#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004836 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004837#endif
4838
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004840 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004841 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004842#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004843#ifdef FEAT_NETBEANS_INTG
4844 abort = abort || set_ref_in_nb_channel(copyID);
4845#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004846
Bram Moolenaare3188e22016-05-31 21:13:04 +02004847#ifdef FEAT_TIMERS
4848 abort = abort || set_ref_in_timer(copyID);
4849#endif
4850
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004851#ifdef FEAT_QUICKFIX
4852 abort = abort || set_ref_in_quickfix(copyID);
4853#endif
4854
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004855#ifdef FEAT_TERMINAL
4856 abort = abort || set_ref_in_term(copyID);
4857#endif
4858
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004859#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004860 abort = abort || set_ref_in_popups(copyID);
4861#endif
4862
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004863 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004864 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004865 /*
4866 * 2. Free lists and dictionaries that are not referenced.
4867 */
4868 did_free = free_unref_items(copyID);
4869
4870 /*
4871 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004873 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004874 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004875 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004876 else if (p_verbose > 0)
4877 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004878 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004879 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004880
4881 return did_free;
4882}
4883
4884/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004885 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004886 */
4887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004888free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004889{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004890 int did_free = FALSE;
4891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 // Let all "free" functions know that we are here. This means no
4893 // dictionaries, lists, channels or jobs are to be freed, because we will
4894 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 in_free_unref_items = TRUE;
4896
4897 /*
4898 * PASS 1: free the contents of the items. We don't free the items
4899 * themselves yet, so that it is possible to decrement refcount counters
4900 */
4901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004903 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004906 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004907
4908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // Go through the list of jobs and free items without the copyID. This
4910 // must happen before doing channels, because jobs refer to channels, but
4911 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004912 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4916#endif
4917
4918 /*
4919 * PASS 2: free the items themselves.
4920 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004921 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004922 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004923
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 // Go through the list of jobs and free items without the copyID. This
4926 // must happen before doing channels, because jobs refer to channels, but
4927 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004928 free_unused_jobs(copyID, COPYID_MASK);
4929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004931 free_unused_channels(copyID, COPYID_MASK);
4932#endif
4933
4934 in_free_unref_items = FALSE;
4935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936 return did_free;
4937}
4938
4939/*
4940 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004941 * "list_stack" is used to add lists to be marked. Can be NULL.
4942 *
4943 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004946set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004947{
4948 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004949 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004950 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004951 hashtab_T *cur_ht;
4952 ht_stack_T *ht_stack = NULL;
4953 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004954
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004955 cur_ht = ht;
4956 for (;;)
4957 {
4958 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 // Mark each item in the hashtab. If the item contains a hashtab
4961 // it is added to ht_stack, if it contains a list it is added to
4962 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004963 todo = (int)cur_ht->ht_used;
4964 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4965 if (!HASHITEM_EMPTY(hi))
4966 {
4967 --todo;
4968 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4969 &ht_stack, list_stack);
4970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004972
4973 if (ht_stack == NULL)
4974 break;
4975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004977 cur_ht = ht_stack->ht;
4978 tempitem = ht_stack;
4979 ht_stack = ht_stack->prev;
4980 free(tempitem);
4981 }
4982
4983 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004984}
4985
Dominique Pelle748b3082022-01-08 12:41:16 +00004986#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4987 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004988/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004989 * Mark a dict and its items with "copyID".
4990 * Returns TRUE if setting references failed somehow.
4991 */
4992 int
4993set_ref_in_dict(dict_T *d, int copyID)
4994{
4995 if (d != NULL && d->dv_copyID != copyID)
4996 {
4997 d->dv_copyID = copyID;
4998 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4999 }
5000 return FALSE;
5001}
Dominique Pelle748b3082022-01-08 12:41:16 +00005002#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005003
5004/*
5005 * Mark a list and its items with "copyID".
5006 * Returns TRUE if setting references failed somehow.
5007 */
5008 int
5009set_ref_in_list(list_T *ll, int copyID)
5010{
5011 if (ll != NULL && ll->lv_copyID != copyID)
5012 {
5013 ll->lv_copyID = copyID;
5014 return set_ref_in_list_items(ll, copyID, NULL);
5015 }
5016 return FALSE;
5017}
5018
5019/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005020 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005021 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5022 *
5023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005025 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005026set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005027{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005028 listitem_T *li;
5029 int abort = FALSE;
5030 list_T *cur_l;
5031 list_stack_T *list_stack = NULL;
5032 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005034 cur_l = l;
5035 for (;;)
5036 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005037 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // Mark each item in the list. If the item contains a hashtab
5039 // it is added to ht_stack, if it contains a list it is added to
5040 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5042 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5043 ht_stack, &list_stack);
5044 if (list_stack == NULL)
5045 break;
5046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005048 cur_l = list_stack->list;
5049 tempitem = list_stack;
5050 list_stack = list_stack->prev;
5051 free(tempitem);
5052 }
5053
5054 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005055}
5056
5057/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005058 * Mark the partial in callback 'cb' with "copyID".
5059 */
5060 int
5061set_ref_in_callback(callback_T *cb, int copyID)
5062{
5063 typval_T tv;
5064
5065 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5066 return FALSE;
5067
5068 tv.v_type = VAR_PARTIAL;
5069 tv.vval.v_partial = cb->cb_partial;
5070 return set_ref_in_item(&tv, copyID, NULL, NULL);
5071}
5072
5073/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005074 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005075 * "list_stack" is used to add lists to be marked. Can be NULL.
5076 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5077 *
5078 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005079 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081set_ref_in_item(
5082 typval_T *tv,
5083 int copyID,
5084 ht_stack_T **ht_stack,
5085 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005086{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005088
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005089 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005090 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005091 dict_T *dd = tv->vval.v_dict;
5092
Bram Moolenaara03f2332016-02-06 18:09:59 +01005093 if (dd != NULL && dd->dv_copyID != copyID)
5094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005096 dd->dv_copyID = copyID;
5097 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005098 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005099 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5100 }
5101 else
5102 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005103 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5104
Bram Moolenaara03f2332016-02-06 18:09:59 +01005105 if (newitem == NULL)
5106 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005107 else
5108 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005109 newitem->ht = &dd->dv_hashtab;
5110 newitem->prev = *ht_stack;
5111 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005112 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005113 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005114 }
5115 }
5116 else if (tv->v_type == VAR_LIST)
5117 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005118 list_T *ll = tv->vval.v_list;
5119
Bram Moolenaara03f2332016-02-06 18:09:59 +01005120 if (ll != NULL && ll->lv_copyID != copyID)
5121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005123 ll->lv_copyID = copyID;
5124 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005125 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005126 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 }
5128 else
5129 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005130 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5131
Bram Moolenaara03f2332016-02-06 18:09:59 +01005132 if (newitem == NULL)
5133 abort = TRUE;
5134 else
5135 {
5136 newitem->list = ll;
5137 newitem->prev = *list_stack;
5138 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005139 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005140 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005141 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005142 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005143 else if (tv->v_type == VAR_FUNC)
5144 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005145 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005146 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005147 else if (tv->v_type == VAR_PARTIAL)
5148 {
5149 partial_T *pt = tv->vval.v_partial;
5150 int i;
5151
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005152 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005153 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005154 // Didn't see this partial yet.
5155 pt->pt_copyID = copyID;
5156
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005157 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005158
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005159 if (pt->pt_dict != NULL)
5160 {
5161 typval_T dtv;
5162
5163 dtv.v_type = VAR_DICT;
5164 dtv.vval.v_dict = pt->pt_dict;
5165 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5166 }
5167
5168 for (i = 0; i < pt->pt_argc; ++i)
5169 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5170 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005171 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172 }
5173 }
5174#ifdef FEAT_JOB_CHANNEL
5175 else if (tv->v_type == VAR_JOB)
5176 {
5177 job_T *job = tv->vval.v_job;
5178 typval_T dtv;
5179
5180 if (job != NULL && job->jv_copyID != copyID)
5181 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005182 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005183 if (job->jv_channel != NULL)
5184 {
5185 dtv.v_type = VAR_CHANNEL;
5186 dtv.vval.v_channel = job->jv_channel;
5187 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5188 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005189 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005190 {
5191 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005192 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005193 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5194 }
5195 }
5196 }
5197 else if (tv->v_type == VAR_CHANNEL)
5198 {
5199 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005200 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005201 typval_T dtv;
5202 jsonq_T *jq;
5203 cbq_T *cq;
5204
5205 if (ch != NULL && ch->ch_copyID != copyID)
5206 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005207 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005208 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005209 {
5210 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5211 jq = jq->jq_next)
5212 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5213 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5214 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005215 if (cq->cq_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 = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005219 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5220 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005221 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005222 {
5223 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005224 dtv.vval.v_partial =
5225 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005226 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5227 }
5228 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005229 if (ch->ch_callback.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_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005233 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5234 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005235 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005236 {
5237 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005238 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005239 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5240 }
5241 }
5242 }
5243#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005244 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005245}
5246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 * Return a string with the string representation of a variable.
5249 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005250 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005251 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005252 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005253 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005254 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005255 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5256 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005257 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005259 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005260echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005261 typval_T *tv,
5262 char_u **tofree,
5263 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005264 int copyID,
5265 int echo_style,
5266 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005267 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005269 static int recurse = 0;
5270 char_u *r = NULL;
5271
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005273 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005274 if (!did_echo_string_emsg)
5275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // Only give this message once for a recursive call to avoid
5277 // flooding the user with errors. And stop iterating over lists
5278 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005279 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005280 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005282 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005283 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005284 }
5285 ++recurse;
5286
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 switch (tv->v_type)
5288 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005289 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005290 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005291 {
5292 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005293 r = tv->vval.v_string;
5294 if (r == NULL)
5295 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005296 }
5297 else
5298 {
5299 *tofree = string_quote(tv->vval.v_string, FALSE);
5300 r = *tofree;
5301 }
5302 break;
5303
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005305 if (echo_style)
5306 {
5307 *tofree = NULL;
5308 r = tv->vval.v_string;
5309 }
5310 else
5311 {
5312 *tofree = string_quote(tv->vval.v_string, TRUE);
5313 r = *tofree;
5314 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005315 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005316
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005317 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005318 {
5319 partial_T *pt = tv->vval.v_partial;
5320 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005321 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005322 garray_T ga;
5323 int i;
5324 char_u *tf;
5325
5326 ga_init2(&ga, 1, 100);
5327 ga_concat(&ga, (char_u *)"function(");
5328 if (fname != NULL)
5329 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005330 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005331 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005332 && vim_isupper(fname[1]))
5333 {
5334 ga_concat(&ga, (char_u *)"'g:");
5335 ga_concat(&ga, fname + 1);
5336 }
5337 else
5338 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005339 vim_free(fname);
5340 }
5341 if (pt != NULL && pt->pt_argc > 0)
5342 {
5343 ga_concat(&ga, (char_u *)", [");
5344 for (i = 0; i < pt->pt_argc; ++i)
5345 {
5346 if (i > 0)
5347 ga_concat(&ga, (char_u *)", ");
5348 ga_concat(&ga,
5349 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5350 vim_free(tf);
5351 }
5352 ga_concat(&ga, (char_u *)"]");
5353 }
5354 if (pt != NULL && pt->pt_dict != NULL)
5355 {
5356 typval_T dtv;
5357
5358 ga_concat(&ga, (char_u *)", ");
5359 dtv.v_type = VAR_DICT;
5360 dtv.vval.v_dict = pt->pt_dict;
5361 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5362 vim_free(tf);
5363 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005364 // terminate with ')' and a NUL
5365 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005366
5367 *tofree = ga.ga_data;
5368 r = *tofree;
5369 break;
5370 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005371
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005372 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005373 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005374 break;
5375
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005377 if (tv->vval.v_list == NULL)
5378 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005379 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005380 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005381 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005382 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005383 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5384 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005385 {
5386 *tofree = NULL;
5387 r = (char_u *)"[...]";
5388 }
5389 else
5390 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005391 int old_copyID = tv->vval.v_list->lv_copyID;
5392
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005393 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005394 *tofree = list2string(tv, copyID, restore_copyID);
5395 if (restore_copyID)
5396 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005397 r = *tofree;
5398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005399 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005400
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005402 if (tv->vval.v_dict == NULL)
5403 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005404 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005405 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005406 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005407 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005408 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5409 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005410 {
5411 *tofree = NULL;
5412 r = (char_u *)"{...}";
5413 }
5414 else
5415 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005416 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005417
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005418 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005419 *tofree = dict2string(tv, copyID, restore_copyID);
5420 if (restore_copyID)
5421 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005422 r = *tofree;
5423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005424 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005426 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005427 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005428 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005429 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005430 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005431 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005432 break;
5433
Bram Moolenaar835dc632016-02-07 14:27:38 +01005434 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005435 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005437 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005438 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5439 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005440 if (composite_val)
5441 {
5442 *tofree = string_quote(r, FALSE);
5443 r = *tofree;
5444 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005445#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005447
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005448 case VAR_INSTR:
5449 *tofree = NULL;
5450 r = (char_u *)"instructions";
5451 break;
5452
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005453 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005454#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005455 *tofree = NULL;
5456 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5457 r = numbuf;
5458 break;
5459#endif
5460
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005461 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005462 case VAR_SPECIAL:
5463 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005464 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005465 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005466 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005467
Bram Moolenaar8502c702014-06-17 12:51:16 +02005468 if (--recurse == 0)
5469 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005470 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005471}
5472
5473/*
5474 * Return a string with the string representation of a variable.
5475 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5476 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005477 * Does not put quotes around strings, as ":echo" displays values.
5478 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5479 * May return NULL.
5480 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005481 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005482echo_string(
5483 typval_T *tv,
5484 char_u **tofree,
5485 char_u *numbuf,
5486 int copyID)
5487{
5488 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5489}
5490
5491/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005492 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5493 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005494 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005495 */
5496 int
5497buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5498{
5499 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005500 char_u *t;
5501 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005502
5503 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5504 return -1;
5505
5506 if (lnum > buf->b_ml.ml_line_count)
5507 lnum = buf->b_ml.ml_line_count;
5508
5509 str = ml_get_buf(buf, lnum, FALSE);
5510 if (str == NULL)
5511 return -1;
5512
5513 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005514 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005515
Bram Moolenaar91458462021-01-13 20:08:38 +01005516 // count the number of characters
5517 t = str;
5518 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5519 t += mb_ptr2len(t);
5520
5521 // In insert mode, when the cursor is at the end of a non-empty line,
5522 // byteidx points to the NUL character immediately past the end of the
5523 // string. In this case, add one to the character count.
5524 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5525 count++;
5526
5527 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005528}
5529
5530/*
5531 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005532 * byte index. Works only for loaded buffers. Returns -1 on failure.
5533 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005534 */
5535 int
5536buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5537{
5538 char_u *str;
5539 char_u *t;
5540
5541 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5542 return -1;
5543
5544 if (lnum > buf->b_ml.ml_line_count)
5545 lnum = buf->b_ml.ml_line_count;
5546
5547 str = ml_get_buf(buf, lnum, FALSE);
5548 if (str == NULL)
5549 return -1;
5550
5551 // Convert the character offset to a byte offset
5552 t = str;
5553 while (*t != NUL && --charidx > 0)
5554 t += mb_ptr2len(t);
5555
Bram Moolenaar91458462021-01-13 20:08:38 +01005556 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005557}
5558
5559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005561 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005564var2fpos(
5565 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005566 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005567 int *fnum, // set to fnum for '0, 'A, etc.
5568 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005570 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005572 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005575 if (varp->v_type == VAR_LIST)
5576 {
5577 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005578 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005579 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005580 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005581
5582 l = varp->vval.v_list;
5583 if (l == NULL)
5584 return NULL;
5585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005587 pos.lnum = list_find_nr(l, 0L, &error);
5588 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005589 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005590 if (charcol)
5591 len = (long)mb_charlen(ml_get(pos.lnum));
5592 else
5593 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005594
Bram Moolenaarec65d772020-08-20 22:29:12 +02005595 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005596 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005597 li = list_find(l, 1L);
5598 if (li != NULL && li->li_tv.v_type == VAR_STRING
5599 && li->li_tv.vval.v_string != NULL
5600 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005601 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005602 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005603 }
5604 else
5605 {
5606 pos.col = list_find_nr(l, 1L, &error);
5607 if (error)
5608 return NULL;
5609 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005612 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005613 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005614 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005616 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005617 pos.coladd = list_find_nr(l, 2L, &error);
5618 if (error)
5619 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005620
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005621 return &pos;
5622 }
5623
Bram Moolenaarc5809432021-03-27 21:23:30 +01005624 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5625 return NULL;
5626
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005627 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005628 if (name == NULL)
5629 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005630 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005631 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005632 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005633 pos = curwin->w_cursor;
5634 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005635 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005636 return &pos;
5637 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005638 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005639 {
5640 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005641 pos = VIsual;
5642 else
5643 pos = curwin->w_cursor;
5644 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005645 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005646 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005647 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005648 if (name[0] == '\'' && (!in_vim9script()
5649 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005651 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005652 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5654 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005655 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005656 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 return pp;
5658 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005659
Bram Moolenaara5525202006-03-02 22:52:09 +00005660 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005661
Bram Moolenaar477933c2007-07-17 14:32:23 +00005662 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005663 {
5664 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005666 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005668 // In silent Ex mode topline is zero, but that's not a valid line
5669 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005670 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005671 return &pos;
5672 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005674 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005675 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005677 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005678 return &pos;
5679 }
5680 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005681 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005683 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
5685 pos.lnum = curbuf->b_ml.ml_line_count;
5686 pos.col = 0;
5687 }
5688 else
5689 {
5690 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005691 if (charcol)
5692 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5693 else
5694 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 return &pos;
5697 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005698 if (in_vim9script())
5699 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return NULL;
5701}
5702
5703/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005704 * Convert list in "arg" into a position and optional file number.
5705 * When "fnump" is NULL there is no file number, only 3 items.
5706 * Note that the column is passed on as-is, the caller may want to decrement
5707 * it to use 1 for the first column.
5708 * Return FAIL when conversion is not possible, doesn't check the position for
5709 * validity.
5710 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712list2fpos(
5713 typval_T *arg,
5714 pos_T *posp,
5715 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005716 colnr_T *curswantp,
5717 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005718{
5719 list_T *l = arg->vval.v_list;
5720 long i = 0;
5721 long n;
5722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005723 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5724 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005725 if (arg->v_type != VAR_LIST
5726 || l == NULL
5727 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005728 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005729 return FAIL;
5730
5731 if (fnump != NULL)
5732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005733 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005734 if (n < 0)
5735 return FAIL;
5736 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005737 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005738 *fnump = n;
5739 }
5740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005741 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005742 if (n < 0)
5743 return FAIL;
5744 posp->lnum = n;
5745
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005746 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005747 if (n < 0)
5748 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005749 // If character position is specified, then convert to byte position
5750 if (charcol)
5751 {
5752 buf_T *buf;
5753
5754 // Get the text for the specified line in a loaded buffer
5755 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5756 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5757 return FAIL;
5758
Bram Moolenaar91458462021-01-13 20:08:38 +01005759 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005760 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005761 posp->col = n;
5762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005763 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005764 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005765 posp->coladd = 0;
5766 else
5767 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005768
Bram Moolenaar493c1782014-05-28 14:34:46 +02005769 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005770 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005771
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005772 return OK;
5773}
5774
5775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 * Get the length of an environment variable name.
5777 * Advance "arg" to the first character after the name.
5778 * Return 0 for error.
5779 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 char_u *p;
5784 int len;
5785
5786 for (p = *arg; vim_isIDc(*p); ++p)
5787 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005788 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 return 0;
5790
5791 len = (int)(p - *arg);
5792 *arg = p;
5793 return len;
5794}
5795
5796/*
5797 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005798 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 * Return 0 if something is wrong.
5800 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005802get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 char_u *p;
5805 int len;
5806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005809 {
5810 if (*p == ':')
5811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005812 // "s:" is start of "s:var", but "n:" is not and can be used in
5813 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005814 len = (int)(p - *arg);
5815 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5816 || len > 1)
5817 break;
5818 }
5819 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 return 0;
5822
5823 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005824 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 return len;
5827}
5828
5829/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005830 * Get the length of the name of a variable or function.
5831 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005833 * Return -1 if curly braces expansion failed.
5834 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * If the name contains 'magic' {}'s, expand them and return the
5836 * expanded name in an allocated string via 'alias' - caller must free.
5837 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005839get_name_len(
5840 char_u **arg,
5841 char_u **alias,
5842 int evaluate,
5843 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 char_u *p;
5847 char_u *expr_start;
5848 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005850 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5853 && (*arg)[2] == (int)KE_SNR)
5854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005855 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 *arg += 3;
5857 return get_id_len(arg) + 3;
5858 }
5859 len = eval_fname_script(*arg);
5860 if (len > 0)
5861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005862 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 *arg += len;
5864 }
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005867 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005869 p = find_name_end(*arg, &expr_start, &expr_end,
5870 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 if (expr_start != NULL)
5872 {
5873 char_u *temp_string;
5874
5875 if (!evaluate)
5876 {
5877 len += (int)(p - *arg);
5878 *arg = skipwhite(p);
5879 return len;
5880 }
5881
5882 /*
5883 * Include any <SID> etc in the expanded string:
5884 * Thus the -len here.
5885 */
5886 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5887 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005888 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *alias = temp_string;
5890 *arg = skipwhite(p);
5891 return (int)STRLEN(temp_string);
5892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005895 // Only give an error when there is something, otherwise it will be
5896 // reported at a higher level.
5897 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005898 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 return len;
5901}
5902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903/*
5904 * Find the end of a variable or function name, taking care of magic braces.
5905 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5906 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005907 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 * Return a pointer to just after the name. Equal to "arg" if there is no
5909 * valid name.
5910 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912find_name_end(
5913 char_u *arg,
5914 char_u **expr_start,
5915 char_u **expr_end,
5916 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005918 int mb_nest = 0;
5919 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005921 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005922 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005924 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005926 *expr_start = NULL;
5927 *expr_end = NULL;
5928 }
5929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005931 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5932 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005933 return arg;
5934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 for (p = arg; *p != NUL
5936 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005937 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005938 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005939 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005940 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005941 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005942 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005943 if (*p == '\'')
5944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005946 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005947 ;
5948 if (*p == NUL)
5949 break;
5950 }
5951 else if (*p == '"')
5952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005954 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005955 if (*p == '\\' && p[1] != NUL)
5956 ++p;
5957 if (*p == NUL)
5958 break;
5959 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005960 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 // "s:" is start of "s:var", but "n:" is not and can be used in
5963 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005964 len = (int)(p - arg);
5965 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005966 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005967 break;
5968 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005970 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005972 if (*p == '[')
5973 ++br_nest;
5974 else if (*p == ']')
5975 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005977
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005978 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005980 if (*p == '{')
5981 {
5982 mb_nest++;
5983 if (expr_start != NULL && *expr_start == NULL)
5984 *expr_start = p;
5985 }
5986 else if (*p == '}')
5987 {
5988 mb_nest--;
5989 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5990 *expr_end = p;
5991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 }
5994
5995 return p;
5996}
5997
5998/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005999 * Expands out the 'magic' {}'s in a variable/function name.
6000 * Note that this can call itself recursively, to deal with
6001 * constructs like foo{bar}{baz}{bam}
6002 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6003 * "in_start" ^
6004 * "expr_start" ^
6005 * "expr_end" ^
6006 * "in_end" ^
6007 *
6008 * Returns a new allocated string, which the caller must free.
6009 * Returns NULL for failure.
6010 */
6011 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012make_expanded_name(
6013 char_u *in_start,
6014 char_u *expr_start,
6015 char_u *expr_end,
6016 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006017{
6018 char_u c1;
6019 char_u *retval = NULL;
6020 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006021
6022 if (expr_end == NULL || in_end == NULL)
6023 return NULL;
6024 *expr_start = NUL;
6025 *expr_end = NUL;
6026 c1 = *in_end;
6027 *in_end = NUL;
6028
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006029 temp_result = eval_to_string(expr_start + 1, FALSE);
6030 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006031 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006032 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6033 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006034 if (retval != NULL)
6035 {
6036 STRCPY(retval, in_start);
6037 STRCAT(retval, temp_result);
6038 STRCAT(retval, expr_end + 1);
6039 }
6040 }
6041 vim_free(temp_result);
6042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006043 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006044 *expr_start = '{';
6045 *expr_end = '}';
6046
6047 if (retval != NULL)
6048 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006049 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006050 if (expr_start != NULL)
6051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006053 temp_result = make_expanded_name(retval, expr_start,
6054 expr_end, temp_result);
6055 vim_free(retval);
6056 retval = temp_result;
6057 }
6058 }
6059
6060 return retval;
6061}
6062
6063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006070 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006071}
6072
6073/*
6074 * Return TRUE if character "c" can be used as the first character in a
6075 * variable or function name (excluding '{' and '}').
6076 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006079{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006080 return ASCII_ISALPHA(c) || c == '_';
6081}
6082
6083/*
6084 * Return TRUE if character "c" can be used as the first character of a
6085 * dictionary key.
6086 */
6087 int
6088eval_isdictc(int c)
6089{
6090 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091}
6092
6093/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006094 * Handle:
6095 * - expr[expr], expr[expr:expr] subscript
6096 * - ".name" lookup
6097 * - function call with Funcref variable: func(expr)
6098 * - method call: var->method()
6099 *
6100 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006101 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006102 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104handle_subscript(
6105 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006106 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006108 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006109 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006110{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006111 int evaluate = evalarg != NULL
6112 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006113 int ret = OK;
6114 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006115 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006116 int getnext;
6117 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006118
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006119 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006120 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006121 // When at the end of the line and ".name" or "->{" or "->X" follows in
6122 // the next line then consume the line break.
6123 p = eval_next_non_blank(*arg, evalarg, &getnext);
6124 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006125 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006126 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6127 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6128 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006129 {
6130 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006131 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006132 check_white = FALSE;
6133 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006134
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006135 if (rettv->v_type == VAR_ANY)
6136 {
6137 char_u *exp_name;
6138 int cc;
6139 int idx;
6140 ufunc_T *ufunc;
6141 type_T *type;
6142
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006143 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006144 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006145 if (**arg != '.')
6146 {
6147 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006148 semsg(_(e_expected_dot_after_name_str),
6149 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006150 ret = FAIL;
6151 break;
6152 }
6153 ++*arg;
6154 if (IS_WHITE_OR_NUL(**arg))
6155 {
6156 if (verbose)
6157 emsg(_(e_no_white_space_allowed_after_dot));
6158 ret = FAIL;
6159 break;
6160 }
6161
6162 // isolate the name
6163 exp_name = *arg;
6164 while (eval_isnamec(**arg))
6165 ++*arg;
6166 cc = **arg;
6167 **arg = NUL;
6168
6169 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006170 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006171 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006172
6173 if (idx < 0 && ufunc == NULL)
6174 {
6175 ret = FAIL;
6176 break;
6177 }
6178 if (idx >= 0)
6179 {
6180 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6181 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6182
6183 copy_tv(sv->sv_tv, rettv);
6184 }
6185 else
6186 {
6187 rettv->v_type = VAR_FUNC;
6188 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6189 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006190 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006191 }
6192
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006193 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6194 || rettv->v_type == VAR_PARTIAL))
6195 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006196 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006197 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6198 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006199
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006200 // Stop the expression evaluation when immediately aborting on
6201 // error, or when an interrupt occurred or an exception was thrown
6202 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006203 if (aborting())
6204 {
6205 if (ret == OK)
6206 clear_tv(rettv);
6207 ret = FAIL;
6208 }
6209 dict_unref(selfdict);
6210 selfdict = NULL;
6211 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006212 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006213 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006214 if (in_vim9script())
6215 *arg = skipwhite(p + 2);
6216 else
6217 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006218 if (ret == OK)
6219 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006220 if (VIM_ISWHITE(**arg))
6221 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006222 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006223 ret = FAIL;
6224 }
6225 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006226 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006227 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006228 else
6229 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006230 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006231 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006232 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006233 // "." is ".name" lookup when we found a dict or when evaluating and
6234 // scriptversion is at least 2, where string concatenation is "..".
6235 else if (**arg == '['
6236 || (**arg == '.' && (rettv->v_type == VAR_DICT
6237 || (!evaluate
6238 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006239 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240 {
6241 dict_unref(selfdict);
6242 if (rettv->v_type == VAR_DICT)
6243 {
6244 selfdict = rettv->vval.v_dict;
6245 if (selfdict != NULL)
6246 ++selfdict->dv_refcount;
6247 }
6248 else
6249 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006250 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006251 {
6252 clear_tv(rettv);
6253 ret = FAIL;
6254 }
6255 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006256 else
6257 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006258 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006260 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6261 // Don't do this when "Func" is already a partial that was bound
6262 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006263 if (selfdict != NULL
6264 && (rettv->v_type == VAR_FUNC
6265 || (rettv->v_type == VAR_PARTIAL
6266 && (rettv->vval.v_partial->pt_auto
6267 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006268 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006270 dict_unref(selfdict);
6271 return ret;
6272}
6273
6274/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 * Make a copy of an item.
6276 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006277 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006278 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6279 * reference to an already copied list/dict can be used.
6280 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006281 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006283item_copy(
6284 typval_T *from,
6285 typval_T *to,
6286 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006287 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289{
6290 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006291 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006295 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006296 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297 }
6298 ++recurse;
6299
6300 switch (from->v_type)
6301 {
6302 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006303 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 case VAR_STRING:
6305 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006306 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006307 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006308 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006309 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006310 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006311 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006312 copy_tv(from, to);
6313 break;
6314 case VAR_LIST:
6315 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006316 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006317 if (from->vval.v_list == NULL)
6318 to->vval.v_list = NULL;
6319 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006321 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006322 to->vval.v_list = from->vval.v_list->lv_copylist;
6323 ++to->vval.v_list->lv_refcount;
6324 }
6325 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006326 to->vval.v_list = list_copy(from->vval.v_list,
6327 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006328 if (to->vval.v_list == NULL)
6329 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006331 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006333 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006334 case VAR_DICT:
6335 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006336 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006337 if (from->vval.v_dict == NULL)
6338 to->vval.v_dict = NULL;
6339 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006342 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6343 ++to->vval.v_dict->dv_refcount;
6344 }
6345 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006346 to->vval.v_dict = dict_copy(from->vval.v_dict,
6347 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006348 if (to->vval.v_dict == NULL)
6349 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006351 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006352 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006354 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006355 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 }
6357 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006359}
6360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 void
6362echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6363{
6364 char_u *tofree;
6365 char_u numbuf[NUMBUFLEN];
6366 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6367
6368 if (*atstart)
6369 {
6370 *atstart = FALSE;
6371 // Call msg_start() after eval1(), evaluating the expression
6372 // may cause a message to appear.
6373 if (with_space)
6374 {
6375 // Mark the saved text as finishing the line, so that what
6376 // follows is displayed on a new line when scrolling back
6377 // at the more prompt.
6378 msg_sb_eol();
6379 msg_start();
6380 }
6381 }
6382 else if (with_space)
6383 msg_puts_attr(" ", echo_attr);
6384
6385 if (p != NULL)
6386 for ( ; *p != NUL && !got_int; ++p)
6387 {
6388 if (*p == '\n' || *p == '\r' || *p == TAB)
6389 {
6390 if (*p != TAB && *needclr)
6391 {
6392 // remove any text still there from the command
6393 msg_clr_eos();
6394 *needclr = FALSE;
6395 }
6396 msg_putchar_attr(*p, echo_attr);
6397 }
6398 else
6399 {
6400 if (has_mbyte)
6401 {
6402 int i = (*mb_ptr2len)(p);
6403
6404 (void)msg_outtrans_len_attr(p, i, echo_attr);
6405 p += i - 1;
6406 }
6407 else
6408 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6409 }
6410 }
6411 vim_free(tofree);
6412}
6413
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 * ":echo expr1 ..." print each argument separated with a space, add a
6416 * newline at the end.
6417 * ":echon expr1 ..." print each argument plain.
6418 */
6419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006424 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 int needclr = TRUE;
6426 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006427 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006428 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006429 evalarg_T evalarg;
6430
Bram Moolenaare707c882020-07-01 13:07:37 +02006431 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 if (eap->skip)
6434 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006435 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 // If eval1() causes an error message the text from the command may
6438 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006439 need_clr_eos = needclr;
6440
Bram Moolenaar68db9962021-05-09 23:19:22 +02006441 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006442 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 {
6444 /*
6445 * Report the invalid expression unless the expression evaluation
6446 * has been cancelled due to an aborting error, an interrupt, or an
6447 * exception.
6448 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006449 if (!aborting() && did_emsg == did_emsg_before
6450 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006451 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006452 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 break;
6454 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006455 need_clr_eos = FALSE;
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006458 {
6459 if (rettv.v_type == VAR_VOID)
6460 {
6461 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6462 break;
6463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006465 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006467 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 arg = skipwhite(arg);
6469 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006470 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006471 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
6473 if (eap->skip)
6474 --emsg_skip;
6475 else
6476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006477 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (needclr)
6479 msg_clr_eos();
6480 if (eap->cmdidx == CMD_echo)
6481 msg_end();
6482 }
6483}
6484
6485/*
6486 * ":echohl {name}".
6487 */
6488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006491 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492}
6493
6494/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006495 * Returns the :echo attribute
6496 */
6497 int
6498get_echo_attr(void)
6499{
6500 return echo_attr;
6501}
6502
6503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * ":execute expr1 ..." execute the result of an expression.
6505 * ":echomsg expr1 ..." Print a message
6506 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006507 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 * Each gets spaces around each argument and a newline at the end for
6509 * echo commands
6510 */
6511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 int ret = OK;
6517 char_u *p;
6518 garray_T ga;
6519 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006520 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 ga_init2(&ga, 1, 80);
6523
6524 if (eap->skip)
6525 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006526 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006528 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006529 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
6532 if (!eap->skip)
6533 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006534 char_u buf[NUMBUFLEN];
6535
6536 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006537 {
6538 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6539 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006540 semsg(_(e_using_invalid_value_as_string_str),
6541 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006542 p = NULL;
6543 }
6544 else
6545 p = tv_get_string_buf(&rettv, buf);
6546 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006547 else
6548 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006549 if (p == NULL)
6550 {
6551 clear_tv(&rettv);
6552 ret = FAIL;
6553 break;
6554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 len = (int)STRLEN(p);
6556 if (ga_grow(&ga, len + 2) == FAIL)
6557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 ret = FAIL;
6560 break;
6561 }
6562 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 ga.ga_len += len;
6566 }
6567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006568 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 arg = skipwhite(arg);
6570 }
6571
6572 if (ret != FAIL && ga.ga_data != NULL)
6573 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006574 // use the first line of continuation lines for messages
6575 SOURCING_LNUM = start_lnum;
6576
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006577 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6578 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 // Mark the already saved text as finishing the line, so that what
6580 // follows is displayed on a new line when scrolling back at the
6581 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006582 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006583 }
6584
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006586 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006587 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006588 out_flush();
6589 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006590 else if (eap->cmdidx == CMD_echoconsole)
6591 {
6592 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6593 ui_write((char_u *)"\r\n", 2, TRUE);
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 else if (eap->cmdidx == CMD_echoerr)
6596 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006597 int save_did_emsg = did_emsg;
6598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006599 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006600 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (!force_abort)
6602 did_emsg = save_did_emsg;
6603 }
6604 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006605 {
6606 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6607
6608 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6609 sticky_cmdmod_flags = cmdmod.cmod_flags
6610 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 do_cmdline((char_u *)ga.ga_data,
6612 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006613 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
6616
6617 ga_clear(&ga);
6618
6619 if (eap->skip)
6620 --emsg_skip;
6621
Bram Moolenaar63b91732021-08-05 20:40:03 +02006622 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625/*
6626 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6627 * "arg" points to the "&" or '+' when called, to "option" when returning.
6628 * Returns NULL when no option name found. Otherwise pointer to the char
6629 * after the option name.
6630 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006631 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006632find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634 char_u *p = *arg;
6635
6636 ++p;
6637 if (*p == 'g' && p[1] == ':')
6638 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006639 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 p += 2;
6641 }
6642 else if (*p == 'l' && p[1] == ':')
6643 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006644 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 p += 2;
6646 }
6647 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006648 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 if (!ASCII_ISALPHA(*p))
6651 return NULL;
6652 *arg = p;
6653
6654 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 else
6657 while (ASCII_ISALPHA(*p))
6658 ++p;
6659 return p;
6660}
6661
6662/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006663 * Display script name where an item was last set.
6664 * Should only be invoked when 'verbose' is non-zero.
6665 */
6666 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006667last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006668{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006669 char_u *p;
6670
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006671 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006672 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006673 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006674 if (p != NULL)
6675 {
6676 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006677 msg_puts(_("\n\tLast set from "));
6678 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006679 if (script_ctx.sc_lnum > 0)
6680 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006681 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006682 msg_outnum((long)script_ctx.sc_lnum);
6683 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006684 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006685 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006686 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006687 }
6688}
6689
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006690#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692/*
6693 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006694 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 * "flags" can be "g" to do a global substitute.
6696 * Returns an allocated string, NULL for error.
6697 */
6698 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006699do_string_sub(
6700 char_u *str,
6701 char_u *pat,
6702 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006703 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 int sublen;
6707 regmatch_T regmatch;
6708 int i;
6709 int do_all;
6710 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006711 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 garray_T ga;
6713 char_u *ret;
6714 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006715 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006717 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006719 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 ga_init2(&ga, 1, 200);
6722
6723 do_all = (flags[0] == 'g');
6724
6725 regmatch.rm_ic = p_ic;
6726 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6727 if (regmatch.regprog != NULL)
6728 {
6729 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006730 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006733 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006734 if (regmatch.startp[0] == regmatch.endp[0])
6735 {
6736 if (zero_width == regmatch.startp[0])
6737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006738 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006739 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006740 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6741 (size_t)i);
6742 ga.ga_len += i;
6743 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006744 continue;
6745 }
6746 zero_width = regmatch.startp[0];
6747 }
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 /*
6750 * Get some space for a temporary buffer to do the substitution
6751 * into. It will contain:
6752 * - The text up to where the match is.
6753 * - The substituted text.
6754 * - The text after the match.
6755 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006756 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006757 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6759 {
6760 ga_clear(&ga);
6761 break;
6762 }
6763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006764 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 i = (int)(regmatch.startp[0] - tail);
6766 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006767 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006768 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 + ga.ga_len + i, TRUE, TRUE, FALSE);
6770 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006771 tail = regmatch.endp[0];
6772 if (*tail == NUL)
6773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (!do_all)
6775 break;
6776 }
6777
6778 if (ga.ga_data != NULL)
6779 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6780
Bram Moolenaar473de612013-06-08 18:19:48 +02006781 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783
6784 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6785 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006786 if (p_cpo == empty_option)
6787 p_cpo = save_cpo;
6788 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006790 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006791 // If it's still empty it was changed and restored, need to restore in
6792 // the complicated way.
6793 if (*p_cpo == NUL)
6794 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006795 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 return ret;
6799}