blob: 61b09fb6b0cdff50a6f492290553d77db898d073 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000114}
115
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000116#if defined(EXITFREE) || defined(PROTO)
117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100118eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200120 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100121 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200122 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200124 // autoloaded script names
125 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000126
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200127 // unreferenced lists and dicts
128 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200129
130 // functions not garbage collected
131 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132}
133#endif
134
Bram Moolenaare6b53242020-07-01 17:28:33 +0200135 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200136fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
137{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100138 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200140 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200141 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200142 evalarg->eval_cstack = eap->cstack;
Yegappan Lakshmanan36a5b682022-03-19 12:56:51 +0000143 if (sourcing_a_script(eap))
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200144 {
145 evalarg->eval_getline = eap->getline;
146 evalarg->eval_cookie = eap->cookie;
147 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 }
149}
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Top level evaluation function, returning a boolean.
153 * Sets "error" to TRUE if there was an error.
154 * Return TRUE or FALSE.
155 */
156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100157eval_to_bool(
158 char_u *arg,
159 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200160 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100161 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaar33570922005-01-25 22:26:29 +0000163 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200164 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200165 evalarg_T evalarg;
166
Bram Moolenaar37c83712020-06-30 21:18:36 +0200167 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 if (skip)
170 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200171 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 else
174 {
175 *error = FALSE;
176 if (!skip)
177 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200179 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200180 else
181 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000182 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
184 }
185 if (skip)
186 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200187 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200189 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192/*
193 * Call eval1() and give an error message if not done at a lower level.
194 */
195 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200196eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100198 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199 int ret;
200 int did_emsg_before = did_emsg;
201 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200204 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
205
206 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 if (ret == FAIL)
208 {
209 // Report the invalid expression unless the expression evaluation has
210 // been cancelled due to an aborting error, an interrupt, or an
211 // exception, or we already gave a more specific error.
212 // Also check called_emsg for when using assert_fails().
213 if (!aborting() && did_emsg == did_emsg_before
214 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200215 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 return ret;
219}
220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200222 * Return whether a typval is a valid expression to pass to eval_expr_typval()
223 * or eval_expr_to_bool(). An empty string returns FALSE;
224 */
225 int
226eval_expr_valid_arg(typval_T *tv)
227{
228 return tv->v_type != VAR_UNKNOWN
229 && (tv->v_type != VAR_STRING
230 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
231}
232
233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 * Evaluate an expression, which can be a function, partial or string.
235 * Pass arguments "argv[argc]".
236 * Return the result in "rettv" and OK or FAIL.
237 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200238 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100239eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
240{
241 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100242 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200243 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100244
245 if (expr->v_type == VAR_FUNC)
246 {
247 s = expr->vval.v_string;
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200250 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000251 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100253 return FAIL;
254 }
255 else if (expr->v_type == VAR_PARTIAL)
256 {
257 partial_T *partial = expr->vval.v_partial;
258
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200259 if (partial == NULL)
260 return FAIL;
261
Bram Moolenaar822ba242020-05-24 23:00:18 +0200262 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200263 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200265 if (call_def_function(partial->pt_func, argc, argv,
266 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 return FAIL;
268 }
269 else
270 {
271 s = partial_name(partial);
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
276 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
278 return FAIL;
279 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100280 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200281 else if (expr->v_type == VAR_INSTR)
282 {
283 return exe_typval_instr(expr, rettv);
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 else
286 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000287 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 if (s == NULL)
289 return FAIL;
290 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200291 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200293 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200295 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200296 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
298 }
299 }
300 return OK;
301}
302
303/*
304 * Like eval_to_bool() but using a typval_T instead of a string.
305 * Works for string, funcref and partial.
306 */
307 int
308eval_expr_to_bool(typval_T *expr, int *error)
309{
310 typval_T rettv;
311 int res;
312
313 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
314 {
315 *error = TRUE;
316 return FALSE;
317 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200318 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 clear_tv(&rettv);
320 return res;
321}
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323/*
324 * Top level evaluation function, returning a string. If "skip" is TRUE,
325 * only parsing to "nextcmd" is done, without reporting errors. Return
326 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
327 */
328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100329eval_to_string_skip(
330 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200331 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100332 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar33570922005-01-25 22:26:29 +0000334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200336 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar37c83712020-06-30 21:18:36 +0200338 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (skip)
340 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 retval = NULL;
343 else
344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100345 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348 if (skip)
349 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352 return retval;
353}
354
355/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000356 * Skip over an expression at "*pp".
357 * Return FAIL for an error, OK otherwise.
358 */
359 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200360skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361{
Bram Moolenaar33570922005-01-25 22:26:29 +0000362 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363
364 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366}
367
368/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200369 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200370 * If in Vim9 script and line breaks are encountered, the lines are
371 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200372 * "arg" is advanced to just after the expression.
373 * "start" is set to the start of the expression, "end" to just after the end.
374 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Return FAIL for an error, OK otherwise.
376 */
377 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200378skip_expr_concatenate(
379 char_u **arg,
380 char_u **start,
381 char_u **end,
382 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200383{
384 typval_T rettv;
385 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200386 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200387 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200388 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200389 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200390 int evaluate = evalarg == NULL
391 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200393 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200400 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403
404 // Don't evaluate the expression.
405 if (evalarg != NULL)
406 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *arg = skipwhite(*arg);
408 res = eval1(arg, &rettv, evalarg);
409 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 if (evalarg != NULL)
411 evalarg->eval_flags = save_flags;
412
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200414 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200416 if (evalarg->eval_ga.ga_len == 1)
417 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200418 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 ga_clear(gap);
420 gap->ga_itemsize = 0;
421 }
422 else
423 {
424 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200425 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // Line breaks encountered, concatenate all the lines.
428 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200430
431 // free the lines only when using getsourceline()
432 if (evalarg->eval_cookie != NULL)
433 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 // Do not free the last line, "arg" points into it, free it
437 // later.
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree =
440 ((char_u **)gap->ga_data)[gap->ga_len - 1];
441 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 ga_clear_strings(gap);
443 }
444 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200445 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447
448 // free lines that were explicitly marked for freeing
449 ga_clear_strings(freegap);
450 }
451
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100467 * Convert "tv" to a string.
468 * When "convert" is TRUE convert a List into a sequence of lines and convert
469 * a Float to a String.
470 * Returns an allocated string (NULL when out of memory).
471 */
472 char_u *
473typval2string(typval_T *tv, int convert)
474{
475 garray_T ga;
476 char_u *retval;
477#ifdef FEAT_FLOAT
478 char_u numbuf[NUMBUFLEN];
479#endif
480
481 if (convert && tv->v_type == VAR_LIST)
482 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000483 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100484 if (tv->vval.v_list != NULL)
485 {
486 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
487 if (tv->vval.v_list->lv_len > 0)
488 ga_append(&ga, NL);
489 }
490 ga_append(&ga, NUL);
491 retval = (char_u *)ga.ga_data;
492 }
493#ifdef FEAT_FLOAT
494 else if (convert && tv->v_type == VAR_FLOAT)
495 {
496 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
497 retval = vim_strsave(numbuf);
498 }
499#endif
500 else
501 retval = vim_strsave(tv_get_string(tv));
502 return retval;
503}
504
505/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 * Top level evaluation function, returning a string. Does not handle line
507 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000508 * When "convert" is TRUE convert a List into a sequence of lines and convert
509 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Return pointer to allocated memory, or NULL for failure.
511 */
512 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100513eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100514 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100515 int convert,
516 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
Bram Moolenaar33570922005-01-25 22:26:29 +0000518 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
523 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 retval = NULL;
525 else
526 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000528 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100530 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 return retval;
533}
534
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 char_u *
536eval_to_string(
537 char_u *arg,
538 int convert)
539{
540 return eval_to_string_eap(arg, convert, NULL);
541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000544 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200545 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200546 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100549eval_to_string_safe(
550 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000551 int use_sandbox,
552 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200555 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200556 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200557 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar9530b582022-01-22 13:39:08 +0000559 if (!keep_script_version)
560 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 if (use_sandbox)
563 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200564 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200565 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000624 * "*arg" points to what can be a function name in the form of "import.Name" or
625 * "Funcref". Return the name of the function. Set "tofree" to something that
626 * was allocated.
627 * If "verbose" is FALSE no errors are given.
628 * Return NULL for any failure.
629 */
630 static char_u *
631deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000632 char_u **arg,
633 char_u **tofree,
634 evalarg_T *evalarg,
635 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000636{
637 typval_T ref;
638 char_u *name = *arg;
639
640 ref.v_type = VAR_UNKNOWN;
641 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100642 {
643 dictitem_T *v;
644
645 // If <SID>VarName was used it would not be found, try another way.
646 v = find_var_also_in_script(name, NULL, FALSE);
647 if (v == NULL)
648 return NULL;
649 copy_tv(&v->di_tv, &ref);
650 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000651 if (*skipwhite(*arg) != NUL)
652 {
653 if (verbose)
654 semsg(_(e_trailing_characters_str), *arg);
655 name = NULL;
656 }
657 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
658 {
659 name = ref.vval.v_string;
660 ref.vval.v_string = NULL;
661 *tofree = name;
662 }
663 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
664 {
665 if (ref.vval.v_partial->pt_argc > 0
666 || ref.vval.v_partial->pt_dict != NULL)
667 {
668 if (verbose)
669 emsg(_(e_cannot_use_partial_here));
670 name = NULL;
671 }
672 else
673 {
674 name = vim_strsave(partial_name(ref.vval.v_partial));
675 *tofree = name;
676 }
677 }
678 else
679 {
680 if (verbose)
681 semsg(_(e_not_callable_type_str), name);
682 name = NULL;
683 }
684 clear_tv(&ref);
685 return name;
686}
687
688/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100689 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200690 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
691 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000692 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100695call_vim_function(
696 char_u *func,
697 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200698 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200699 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200702 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000703 char_u *arg;
704 char_u *name;
705 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000706 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100708 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200709 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000710 funcexe.fe_firstline = curwin->w_cursor.lnum;
711 funcexe.fe_lastline = curwin->w_cursor.lnum;
712 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000713
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000714 // The name might be "import.Func" or "Funcref". We don't know, we need to
715 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000716 // autoload script has errors. Guess that when there is a dot in the name
717 // showing errors is the right choice.
718 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000719 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000720 if (ignore_errors)
721 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000722 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000723 if (ignore_errors)
724 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000725 if (name == NULL)
726 name = func;
727
728 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
729
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000730 if (ret == FAIL)
731 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000732 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733
734 return ret;
735}
736
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100737/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100738 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000739 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
740 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000741 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000742 */
743 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744call_func_retstr(
745 char_u *func,
746 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200747 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000748{
749 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000750 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000751
Bram Moolenaarded27a12018-08-01 19:06:03 +0200752 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000753 return NULL;
754
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100755 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return retval;
758}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000760/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100761 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000762 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000763 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 */
765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100766call_func_retlist(
767 char_u *func,
768 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200769 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000770{
771 typval_T rettv;
772
Bram Moolenaarded27a12018-08-01 19:06:03 +0200773 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000774 return NULL;
775
776 if (rettv.v_type != VAR_LIST)
777 {
778 clear_tv(&rettv);
779 return NULL;
780 }
781
782 return rettv.vval.v_list;
783}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaare70dd112022-01-21 16:31:11 +0000785#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100787 * Evaluate "arg", which is 'foldexpr'.
788 * Note: caller must set "curwin" to match "arg".
789 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
790 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
792 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000793eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000795 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000799 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000800 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
801 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaare70dd112022-01-21 16:31:11 +0000803 arg = wp->w_p_fde;
804 current_sctx = wp->w_p_script_ctx[WV_FDE];
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000807 if (use_sandbox)
808 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200809 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200811 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
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 number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000816 if (tv.v_type == VAR_NUMBER)
817 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000818 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 retval = 0;
820 else
821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100822 // If the result is a string, check if there is a non-digit before
823 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (!VIM_ISDIGIT(*s) && *s != '-')
826 *cp = *s++;
827 retval = atol((char *)s);
828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000832 if (use_sandbox)
833 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200834 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200835 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000836 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200838 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840#endif
841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 * Get an lval: variable, Dict item or List item that can be assigned a value
844 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
845 * "name.key", "name.key[expr]" etc.
846 * Indexing only works if "name" is an existing List or Dictionary.
847 * "name" points to the start of the name.
848 * If "rettv" is not NULL it points to the value to be assigned.
849 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
850 * wrong; must end in space or cmd separator.
851 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100852 * flags:
853 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100854 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100855 * GLV_NO_AUTOLOAD: do not use script autoloading
856 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862get_lval(
863 char_u *name,
864 typval_T *rettv,
865 lval_T *lp,
866 int unlet,
867 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 int flags, // GLV_ values
869 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 char_u *expr_start, *expr_end;
873 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 dictitem_T *v;
875 typval_T var1;
876 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100880 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100881 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100882 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000883 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200886 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100888 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100890 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200892 lp->ll_name_end = find_name_end(name, NULL, NULL,
893 FNE_INCL_BR | fne_flags);
894 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 }
896
Bram Moolenaara749a422022-02-12 19:52:25 +0000897 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000898 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000899 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
900 {
901 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
902 return NULL;
903 }
904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000906 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (expr_start != NULL)
909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100911 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 && *p != '[' && *p != '.')
913 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000914 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
916 }
917
918 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
919 if (lp->ll_exp_name == NULL)
920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 // Report an invalid expression in braces, unless the
922 // expression evaluation has been cancelled due to an
923 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!aborting() && !quiet)
925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000926 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000927 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 return NULL;
929 }
930 }
931 lp->ll_name = lp->ll_exp_name;
932 }
933 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 lp->ll_name = name;
936
Bram Moolenaar4525a572022-02-13 11:57:33 +0000937 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200939 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000940 // However, "g:[key]" is indexing a dictionary.
941 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200942 {
943 --p;
944 lp->ll_name_end = p;
945 }
946 if (*p == ':')
947 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000948 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200949
950 if (tp == p + 1 && !quiet)
951 {
952 semsg(_(e_white_space_required_after_str_str), ":", p);
953 return NULL;
954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000956 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000957 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000958 semsg(_(e_using_type_not_in_script_context_str), p);
959 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000960 }
961
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200962 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000963 lp->ll_type = parse_type(&tp,
964 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
965 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100966 if (lp->ll_type == NULL && !quiet)
967 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200968 lp->ll_name_end = tp;
969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 }
971 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000972 if (lp->ll_name == NULL)
973 return p;
974
Bram Moolenaar62aec932022-01-29 21:45:34 +0000975 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000976 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000977 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000978
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000979 if (import != NULL)
980 {
981 ufunc_T *ufunc;
982 type_T *type;
983
984 lp->ll_sid = import->imp_sid;
985 lp->ll_name = skipwhite(p + 1);
986 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
987 lp->ll_name_end = p;
988
989 // check the item is exported
990 cc = *p;
991 *p = NUL;
992 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000993 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000994 {
995 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000996 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000997 }
998 *p = cc;
999 }
1000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001003 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return p;
1005
Bram Moolenaar4525a572022-02-13 11:57:33 +00001006 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001007 {
1008 // using local variable
1009 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001010 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001011 }
1012 else
1013 {
1014 cc = *p;
1015 *p = NUL;
1016 // When we would write to the variable pass &ht and prevent autoload.
1017 writing = !(flags & GLV_READ_ONLY);
1018 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001019 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001020 if (v == NULL && !quiet)
1021 semsg(_(e_undefined_variable_str), lp->ll_name);
1022 *p = cc;
1023 if (v == NULL)
1024 return NULL;
1025 lp->ll_tv = &v->di_tv;
1026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027
Bram Moolenaar4525a572022-02-13 11:57:33 +00001028 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001029 {
1030 if (!quiet)
1031 semsg(_(e_variable_already_declared), lp->ll_name);
1032 return NULL;
1033 }
1034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 /*
1036 * Loop until no more [idx] or .key is following.
1037 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 var1.v_type = VAR_UNKNOWN;
1039 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001040 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001041 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001042 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1043 {
1044 if (!quiet)
1045 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1046 return NULL;
1047 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001048 if (lp->ll_tv->v_type != VAR_LIST
1049 && lp->ll_tv->v_type != VAR_DICT
1050 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001053 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001055 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001056
1057 // a NULL list/blob works like an empty list/blob, allocate one now.
1058 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1059 rettv_list_alloc(lp->ll_tv);
1060 else if (lp->ll_tv->v_type == VAR_BLOB
1061 && lp->ll_tv->vval.v_blob == NULL)
1062 rettv_blob_alloc(lp->ll_tv);
1063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001065 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001067 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001070
Bram Moolenaar4525a572022-02-13 11:57:33 +00001071 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001072 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001073 && lp->ll_tv == &v->di_tv
1074 && ht != NULL && ht == get_script_local_ht())
1075 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001076 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001077
1078 // Vim9 script local variable: get the type
1079 if (sv != NULL)
1080 lp->ll_valtype = sv->sv_type;
1081 }
1082
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 len = -1;
1084 if (*p == '.')
1085 {
1086 key = p + 1;
1087 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1088 ;
1089 if (len == 0)
1090 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001092 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
1095 p = key + len;
1096 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001097 else
1098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001099 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001100 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 if (*p == ':')
1102 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001103 else
1104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001106 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001108 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001111 clear_tv(&var1);
1112 return NULL;
1113 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001114 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 }
1116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001118 if (*p == ':')
1119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001123 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001124 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001126 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001127 if (rettv != NULL
1128 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001129 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001134 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138 p = skipwhite(p + 1);
1139 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 else
1142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001144 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001145 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001150 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001153 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001154 clear_tv(&var2);
1155 return NULL;
1156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001162
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001166 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001167 clear_tv(&var1);
1168 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 }
1171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 ++p;
1174 }
1175
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
1178 if (len == -1)
1179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001180 // "[key]": get key from "var1"
1181 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001182 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 }
1187 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001188 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001189
1190 // a NULL dict is equivalent with an empty dict
1191 if (lp->ll_tv->vval.v_dict == NULL)
1192 {
1193 lp->ll_tv->vval.v_dict = dict_alloc();
1194 if (lp->ll_tv->vval.v_dict == NULL)
1195 {
1196 clear_tv(&var1);
1197 return NULL;
1198 }
1199 ++lp->ll_tv->vval.v_dict->dv_refcount;
1200 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001202
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001203 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001205 // When assigning to a scope dictionary check that a function and
1206 // variable name is valid (only variable name unless it is l: or
1207 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001208 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001209 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001210 int prevval;
1211 int wrong;
1212
1213 if (len != -1)
1214 {
1215 prevval = key[len];
1216 key[len] = NUL;
1217 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001218 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001219 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001220 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1221 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001222 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001223 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001224 if (len != -1)
1225 key[len] = prevval;
1226 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001227 {
1228 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001229 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001230 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001239 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001240 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001241 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001242 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001243 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001244 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001245 return NULL;
1246 }
1247
Bram Moolenaar31b81602019-02-10 22:14:27 +01001248 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001252 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 p = NULL;
1263 break;
1264 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001265 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001266 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001267 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1268 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001269 {
1270 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001271 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001272 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001273
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001274 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 else if (lp->ll_tv->v_type == VAR_BLOB)
1278 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001279 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1280
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 /*
1282 * Get the number and item for the only or first index of the List.
1283 */
1284 if (empty1)
1285 lp->ll_n1 = 0;
1286 else
1287 // is number or string
1288 lp->ll_n1 = (long)tv_get_number(&var1);
1289 clear_tv(&var1);
1290
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001291 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 return NULL;
1295 }
1296 if (lp->ll_range && !lp->ll_empty2)
1297 {
1298 lp->ll_n2 = (long)tv_get_number(&var2);
1299 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001300 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1301 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 }
1304 lp->ll_blob = lp->ll_tv->vval.v_blob;
1305 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001306 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001308 else
1309 {
1310 /*
1311 * Get the number and item for the only or first index of the List.
1312 */
1313 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001317 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
1319
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001322 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1323 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001324 if (lp->ll_li == NULL)
1325 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001326 clear_tv(&var2);
1327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329
Bram Moolenaar10c65862020-10-08 21:16:42 +02001330 if (lp->ll_valtype != NULL)
1331 // use the type of the member
1332 lp->ll_valtype = lp->ll_valtype->tt_member;
1333
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 /*
1335 * May need to find the item or absolute index for the second
1336 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 * When no index given: "lp->ll_empty2" is TRUE.
1338 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001342 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001343 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001345 if (check_range_index_two(lp->ll_list,
1346 &lp->ll_n1, lp->ll_li,
1347 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001349 }
1350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 }
1354
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001355 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001357 return p;
1358}
1359
1360/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001364clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365{
1366 vim_free(lp->ll_exp_name);
1367 vim_free(lp->ll_newkey);
1368}
1369
1370/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1374 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001377set_var_lval(
1378 lval_T *lp,
1379 char_u *endp,
1380 typval_T *rettv,
1381 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001382 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1383 char_u *op,
1384 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385{
1386 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388
1389 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 cc = *endp;
1392 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001393 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1394 return;
1395
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001396 if (lp->ll_blob != NULL)
1397 {
1398 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001400 if (op != NULL && *op != '=')
1401 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001402 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001403 return;
1404 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001405 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001406 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001407
1408 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1409 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001410 if (lp->ll_empty2)
1411 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1412
Bram Moolenaar68452172021-04-12 21:21:02 +02001413 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1414 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 }
1417 else
1418 {
1419 val = (int)tv_get_number_chk(rettv, &error);
1420 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001421 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001422 }
1423 }
1424 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001426 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001428 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1429 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001431 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001432 *endp = cc;
1433 return;
1434 }
1435
Bram Moolenaarff697e62019-02-12 22:28:33 +01001436 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001437 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001438 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001439 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001440 {
1441 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001442 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1443 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001444 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001445 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001446 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001447 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001451 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001452 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1453 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001454 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001455 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001456 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001457 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001458 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001460 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001461 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001462 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001463 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 else if (lp->ll_range)
1465 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001466 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1467 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001469 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001470 return;
1471 }
1472
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001473 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1474 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 }
1476 else
1477 {
1478 /*
1479 * Assign to a List or Dictionary item.
1480 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001481 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1482 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001483 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001484 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001485 return;
1486 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001487
1488 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001489 && check_typval_arg_type(lp->ll_valtype, rettv,
1490 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001491 return;
1492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 if (lp->ll_newkey != NULL)
1494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 if (op != NULL && *op != '=')
1496 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001497 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 return;
1499 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001500 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1501 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001502 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001505 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 if (di == NULL)
1507 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001508 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1509 {
1510 vim_free(di);
1511 return;
1512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 else if (op != NULL && *op != '=')
1516 {
1517 tv_op(lp->ll_tv, rettv, op);
1518 return;
1519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001520 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001521 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 /*
1524 * Assign the value to the variable or list item.
1525 */
1526 if (copy)
1527 copy_tv(rettv, lp->ll_tv);
1528 else
1529 {
1530 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001531 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 }
1534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535}
1536
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1539 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 * Returns OK or FAIL.
1541 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001545 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 char_u numbuf[NUMBUFLEN];
1547 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001548 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001550 // Can't do anything with a Funcref or Dict on the right.
1551 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001552 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001553 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1554 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 {
1556 switch (tv1->v_type)
1557 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001558 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001559 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 case VAR_DICT:
1562 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001564 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001565 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001566 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001567 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001568 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 break;
1570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 case VAR_BLOB:
1572 if (*op != '+' || tv2->v_type != VAR_BLOB)
1573 break;
1574 // BLOB += BLOB
1575 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1576 {
1577 blob_T *b1 = tv1->vval.v_blob;
1578 blob_T *b2 = tv2->vval.v_blob;
1579 int i, len = blob_len(b2);
1580 for (i = 0; i < len; i++)
1581 ga_append(&b1->bv_ga, blob_get(b2, i));
1582 }
1583 return OK;
1584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585 case VAR_LIST:
1586 if (*op != '+' || tv2->v_type != VAR_LIST)
1587 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001589 if (tv2->vval.v_list != NULL)
1590 {
1591 if (tv1->vval.v_list == NULL)
1592 {
1593 tv1->vval.v_list = tv2->vval.v_list;
1594 ++tv1->vval.v_list->lv_refcount;
1595 }
1596 else
1597 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 return OK;
1600
1601 case VAR_NUMBER:
1602 case VAR_STRING:
1603 if (tv2->v_type == VAR_LIST)
1604 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609#ifdef FEAT_FLOAT
1610 if (tv2->v_type == VAR_FLOAT)
1611 {
1612 float_T f = n;
1613
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 if (*op == '%')
1615 break;
1616 switch (*op)
1617 {
1618 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 clear_tv(tv1);
1624 tv1->v_type = VAR_FLOAT;
1625 tv1->vval.v_float = f;
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628#endif
1629 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 switch (*op)
1631 {
1632 case '+': n += tv_get_number(tv2); break;
1633 case '-': n -= tv_get_number(tv2); break;
1634 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001635 case '/': n = num_divide(n, tv_get_number(tv2),
1636 &failed); break;
1637 case '%': n = num_modulus(n, tv_get_number(tv2),
1638 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 clear_tv(tv1);
1641 tv1->v_type = VAR_NUMBER;
1642 tv1->vval.v_number = n;
1643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 }
1645 else
1646 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647 if (tv2->v_type == VAR_FLOAT)
1648 break;
1649
Bram Moolenaarff697e62019-02-12 22:28:33 +01001650 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001651 s = tv_get_string(tv1);
1652 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 clear_tv(tv1);
1654 tv1->v_type = VAR_STRING;
1655 tv1->vval.v_string = s;
1656 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001657 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661 {
1662 float_T f;
1663
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 if (*op == '%' || *op == '.'
1665 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 && tv2->v_type != VAR_NUMBER
1667 && tv2->v_type != VAR_STRING))
1668 break;
1669 if (tv2->v_type == VAR_FLOAT)
1670 f = tv2->vval.v_float;
1671 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001672 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 switch (*op)
1674 {
1675 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001682 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 }
1684 }
1685
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001686 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 return FAIL;
1688}
1689
1690/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 * Evaluate the expression used in a ":for var in expr" command.
1692 * "arg" points to "var".
1693 * Set "*errp" to TRUE for an error, FALSE otherwise;
1694 * Return a pointer that holds the info. Null when there is an error.
1695 */
1696 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697eval_for_line(
1698 char_u *arg,
1699 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001700 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702{
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001704 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T tv;
1707 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001708 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001710 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001712 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 if (fi == NULL)
1714 return NULL;
1715
Bram Moolenaar404557e2021-07-05 21:41:48 +02001716 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1717 &fi->fi_semicolon, FALSE);
1718 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return fi;
1720
Bram Moolenaar404557e2021-07-05 21:41:48 +02001721 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 if (expr[0] != 'i' || expr[1] != 'n'
1723 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001725 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1726 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1727 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001728 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 return fi;
1730 }
1731
1732 if (skip)
1733 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001734 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1735 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 {
1737 *errp = FALSE;
1738 if (!skip)
1739 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001741 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 l = tv.vval.v_list;
1743 if (l == NULL)
1744 {
1745 // a null list is like an empty list: do nothing
1746 clear_tv(&tv);
1747 }
1748 else
1749 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001751 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 // No need to increment the refcount, it's already set for
1754 // the list being used in "tv".
1755 fi->fi_list = l;
1756 list_add_watch(l, &fi->fi_lw);
1757 fi->fi_lw.lw_item = l->lv_first;
1758 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001759 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001760 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001761 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001762 fi->fi_bi = 0;
1763 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001765 typval_T btv;
1766
1767 // Make a copy, so that the iteration still works when the
1768 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001770 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001771 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001772 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001773 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001774 else if (tv.v_type == VAR_STRING)
1775 {
1776 fi->fi_byte_idx = 0;
1777 fi->fi_string = tv.vval.v_string;
1778 tv.vval.v_string = NULL;
1779 if (fi->fi_string == NULL)
1780 fi->fi_string = vim_strsave((char_u *)"");
1781 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 else
1783 {
Sean Dewar80d73952021-08-04 19:25:54 +02001784 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001785 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 }
1787 }
1788 }
1789 if (skip)
1790 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001791 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792
1793 return fi;
1794}
1795
1796/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001797 * Used when looping over a :for line, skip the "in expr" part.
1798 */
1799 void
1800skip_for_lines(void *fi_void, evalarg_T *evalarg)
1801{
1802 forinfo_T *fi = (forinfo_T *)fi_void;
1803 int i;
1804
1805 for (i = 0; i < fi->fi_break_count; ++i)
1806 eval_next_line(evalarg);
1807}
1808
1809/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 * Use the first item in a ":for" list. Advance to the next.
1811 * Assign the values to the variable (list). "arg" points to the first one.
1812 * Return TRUE when a valid item was found, FALSE when at end of list or
1813 * something wrong.
1814 */
1815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001818 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001820 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001821 ? (ASSIGN_FINAL
1822 // first round: error if variable exists
1823 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1824 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001825 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001827 int skip_assign = in_vim9script() && arg[0] == '_'
1828 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 if (fi->fi_blob != NULL)
1831 {
1832 typval_T tv;
1833
1834 if (fi->fi_bi >= blob_len(fi->fi_blob))
1835 return FALSE;
1836 tv.v_type = VAR_NUMBER;
1837 tv.v_lock = VAR_FIXED;
1838 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1839 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001840 if (skip_assign)
1841 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001842 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001843 fi->fi_varcount, flag, NULL) == OK;
1844 }
1845
1846 if (fi->fi_string != NULL)
1847 {
1848 typval_T tv;
1849 int len;
1850
1851 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1852 if (len == 0)
1853 return FALSE;
1854 tv.v_type = VAR_STRING;
1855 tv.v_lock = VAR_FIXED;
1856 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1857 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001858 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001859 if (skip_assign)
1860 result = TRUE;
1861 else
1862 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001864 vim_free(tv.vval.v_string);
1865 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001866 }
1867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 item = fi->fi_lw.lw_item;
1869 if (item == NULL)
1870 result = FALSE;
1871 else
1872 {
1873 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001874 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001875 if (skip_assign)
1876 result = TRUE;
1877 else
1878 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001879 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 }
1881 return result;
1882}
1883
1884/*
1885 * Free the structure used to store info used by ":for".
1886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001892 if (fi == NULL)
1893 return;
1894 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001895 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001897 list_unref(fi->fi_list);
1898 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001899 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001900 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001901 else
1902 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 vim_free(fi);
1904}
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001907set_context_for_expression(
1908 expand_T *xp,
1909 char_u *arg,
1910 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001912 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001916 if (cmdidx == CMD_let || cmdidx == CMD_var
1917 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 {
1919 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 {
1925 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001926 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001927 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 break;
1929 }
1930 return;
1931 }
1932 }
1933 else
1934 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1935 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 while ((xp->xp_pattern = vim_strpbrk(arg,
1937 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1938 {
1939 c = *xp->xp_pattern;
1940 if (c == '&')
1941 {
1942 c = xp->xp_pattern[1];
1943 if (c == '&')
1944 {
1945 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001946 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001951 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1952 xp->xp_pattern += 2;
1953
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 else if (c == '$')
1957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 xp->xp_context = EXPAND_ENV_VARS;
1960 }
1961 else if (c == '=')
1962 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001963 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 xp->xp_context = EXPAND_EXPRESSION;
1965 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001966 else if (c == '#'
1967 && xp->xp_context == EXPAND_EXPRESSION)
1968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001970 break;
1971 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001972 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 && xp->xp_context == EXPAND_FUNCTIONS
1974 && vim_strchr(xp->xp_pattern, '(') == NULL)
1975 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 break;
1978 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001979 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1984 if (c == '\\' && xp->xp_pattern[1] != NUL)
1985 ++xp->xp_pattern;
1986 xp->xp_context = EXPAND_NOTHING;
1987 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1992 /* skip */ ;
1993 xp->xp_context = EXPAND_NOTHING;
1994 }
1995 else if (c == '|')
1996 {
1997 if (xp->xp_pattern[1] == '|')
1998 {
1999 ++xp->xp_pattern;
2000 xp->xp_context = EXPAND_EXPRESSION;
2001 }
2002 else
2003 xp->xp_context = EXPAND_COMMANDS;
2004 }
2005 else
2006 xp->xp_context = EXPAND_EXPRESSION;
2007 }
2008 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // Doesn't look like something valid, expand as an expression
2010 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 arg = xp->xp_pattern;
2013 if (*arg != NUL)
2014 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2015 /* skip */ ;
2016 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002017
2018 // ":exe one two" completes "two"
2019 if ((cmdidx == CMD_execute
2020 || cmdidx == CMD_echo
2021 || cmdidx == CMD_echon
2022 || cmdidx == CMD_echomsg)
2023 && xp->xp_context == EXPAND_EXPRESSION)
2024 {
2025 for (;;)
2026 {
2027 char_u *n = skiptowhite(arg);
2028
2029 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2030 break;
2031 arg = skipwhite(n);
2032 }
2033 }
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 xp->xp_pattern = arg;
2036}
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002039 * Return TRUE if "pat" matches "text".
2040 * Does not use 'cpo' and always uses 'magic'.
2041 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002042 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002043pattern_match(char_u *pat, char_u *text, int ic)
2044{
2045 int matches = FALSE;
2046 char_u *save_cpo;
2047 regmatch_T regmatch;
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002050 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002051 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2053 if (regmatch.regprog != NULL)
2054 {
2055 regmatch.rm_ic = ic;
2056 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2057 vim_regfree(regmatch.regprog);
2058 }
2059 p_cpo = save_cpo;
2060 return matches;
2061}
2062
2063/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 * Handle a name followed by "(". Both for just "name(arg)" and for
2065 * "expr->name(arg)".
2066 * Returns OK or FAIL.
2067 */
2068 static int
2069eval_func(
2070 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002071 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 char_u *name,
2073 int name_len,
2074 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076 typval_T *basetv) // "expr" for "expr->name(arg)"
2077{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002078 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 char_u *s = name;
2080 int len = name_len;
2081 partial_T *partial;
2082 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002083 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002084 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085
2086 if (!evaluate)
2087 check_vars(s, len);
2088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002089 // If "s" is the name of a variable of type VAR_FUNC
2090 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002091 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002092 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 // Need to make a copy, in case evaluating the arguments makes
2095 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002096 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002097 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002098 ret = FAIL;
2099 else
2100 {
2101 funcexe_T funcexe;
2102
2103 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002104 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002105 funcexe.fe_firstline = curwin->w_cursor.lnum;
2106 funcexe.fe_lastline = curwin->w_cursor.lnum;
2107 funcexe.fe_evaluate = evaluate;
2108 funcexe.fe_partial = partial;
2109 funcexe.fe_basetv = basetv;
2110 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002111 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002112 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002113 }
2114 vim_free(s);
2115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // If evaluate is FALSE rettv->v_type was not set in
2117 // get_func_tv, but it's needed in handle_subscript() to parse
2118 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002119 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2120 {
2121 rettv->vval.v_string = NULL;
2122 rettv->v_type = VAR_FUNC;
2123 }
2124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Stop the expression evaluation when immediately
2126 // aborting on error, or when an interrupt occurred or
2127 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002128 if (evaluate && aborting())
2129 {
2130 if (ret == OK)
2131 clear_tv(rettv);
2132 ret = FAIL;
2133 }
2134 return ret;
2135}
2136
2137/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002138 * Get the next line source line without advancing. But do skip over comment
2139 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002140 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002141 */
2142 static char_u *
2143getline_peek_skip_comments(evalarg_T *evalarg)
2144{
2145 for (;;)
2146 {
2147 char_u *next = getline_peek(evalarg->eval_getline,
2148 evalarg->eval_cookie);
2149 char_u *p;
2150
2151 if (next == NULL)
2152 break;
2153 p = skipwhite(next);
2154 if (*p != NUL && !vim9_comment_start(p))
2155 return next;
Bram Moolenaar43216612022-03-25 11:16:28 +00002156 if (eval_next_line(evalarg) == NULL)
2157 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002158 }
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 Moolenaar43216612022-03-25 11:16:28 +00002212 if (line == NULL)
2213 return NULL;
2214
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002215 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002216 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2217 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002218 char_u *p = skipwhite(line);
2219
2220 // Going to concatenate the lines after parsing. For an empty or
2221 // comment line use an empty string.
2222 if (*p == NUL || vim9_comment_start(p))
2223 {
2224 vim_free(line);
2225 line = vim_strsave((char_u *)"");
2226 }
2227
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002228 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2229 ++gap->ga_len;
2230 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002231 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002232 {
2233 vim_free(evalarg->eval_tofree);
2234 evalarg->eval_tofree = line;
2235 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002236
2237 // Advanced to the next line, "arg" no longer points into the previous
2238 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002239 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002240 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002241}
2242
Bram Moolenaare6b53242020-07-01 17:28:33 +02002243/*
2244 * Call eval_next_non_blank() and get the next line if needed.
2245 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002246 char_u *
2247skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2248{
2249 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002250 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002251
Bram Moolenaar962d7212020-07-04 14:15:00 +02002252 if (evalarg == NULL)
2253 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002254 eval_next_non_blank(p, evalarg, &getnext);
2255 if (getnext)
2256 return eval_next_line(evalarg);
2257 return p;
2258}
2259
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002260/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002261 * Initialize "evalarg" for use.
2262 */
2263 void
2264init_evalarg(evalarg_T *evalarg)
2265{
2266 CLEAR_POINTER(evalarg);
2267 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2268}
2269
2270/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002271 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002272 */
2273 void
2274clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2275{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002276 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002277 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002278 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002279 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002280 if (eap != NULL)
2281 {
2282 // We may need to keep the original command line, e.g. for
2283 // ":let" it has the variable names. But we may also need the
2284 // new one, "nextcmd" points into it. Keep both.
2285 vim_free(eap->cmdline_tofree);
2286 eap->cmdline_tofree = *eap->cmdlinep;
2287 *eap->cmdlinep = evalarg->eval_tofree;
2288 }
2289 else
2290 vim_free(evalarg->eval_tofree);
2291 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002292 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002293
Bram Moolenaar844fb642021-10-23 13:32:30 +01002294 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002295 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002296 }
2297}
2298
2299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2303 */
2304
2305/*
2306 * Handle zero level expression.
2307 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002309 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 * Return OK or FAIL.
2312 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002314eval0(
2315 char_u *arg,
2316 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002317 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002320 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2321}
2322
2323/*
2324 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2325 * expression and don't check what comes after the expression.
2326 */
2327 int
2328eval0_retarg(
2329 char_u *arg,
2330 typval_T *rettv,
2331 exarg_T *eap,
2332 evalarg_T *evalarg,
2333 char_u **retarg)
2334{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 int ret;
2336 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002337 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002338 int did_emsg_before = did_emsg;
2339 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002340 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002341 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002342 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002345 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002346 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002347 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002348
Bram Moolenaar02929a32021-12-17 14:46:12 +00002349 // In Vim9 script a command block is not split at NL characters for
2350 // commands using an expression argument. Skip over a '#' comment to check
2351 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002352 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002353 while (*p == '#')
2354 {
2355 char_u *nl = vim_strchr(p, NL);
2356
2357 if (nl == NULL)
2358 break;
2359 p = skipwhite(nl + 1);
2360 if (eap != NULL && *p != NUL)
2361 eap->nextcmd = p;
2362 check_for_end = FALSE;
2363 }
2364
2365 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002366 end_error = !ends_excmd2(arg, p);
2367 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
2369 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /*
2372 * Report the invalid expression unless the expression evaluation has
2373 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002374 * exception, or we already gave a more specific error.
2375 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002377 if (!aborting()
2378 && did_emsg == did_emsg_before
2379 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002380 && (flags & EVAL_CONSTANT) == 0
2381 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002382 {
2383 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002384 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002385 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002386 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002387 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002388
2389 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002390 // a next command to avoid more errors, unless "|" is following, which
2391 // could only be a command separator.
2392 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2393 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002396
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002397 if (retarg != NULL)
2398 *retarg = p;
2399 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002400 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 return ret;
2403}
2404
2405/*
2406 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002407 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002408 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 *
2410 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002411 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002413 * Note: "rettv.v_lock" is not set.
2414 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 * Return OK or FAIL.
2416 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002417 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002420 char_u *p;
2421 int getnext;
2422
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002423 CLEAR_POINTER(rettv);
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /*
2426 * Get the first variable.
2427 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FAIL;
2430
Bram Moolenaar793648f2020-06-26 21:28:25 +02002431 p = eval_next_non_blank(*arg, evalarg, &getnext);
2432 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002434 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435 int result;
2436 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 evalarg_T *evalarg_used = evalarg;
2438 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002440 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002441 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442
2443 if (evalarg == NULL)
2444 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002445 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 orig_flags = evalarg_used->eval_flags;
2449 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 if (getnext)
2452 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002453 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002454 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002455 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002456 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002457 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002458 clear_tv(rettv);
2459 return FAIL;
2460 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002461 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 int error = FALSE;
2468
Bram Moolenaar13106602020-10-04 16:06:05 +02002469 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002470 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002471 else if (vim9script)
2472 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002473 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002475 if (error || !op_falsy || !result)
2476 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 if (error)
2478 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
2481 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002482 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002484 if (op_falsy)
2485 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002486 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002487 {
mityu4ac198c2021-05-28 17:52:40 +02002488 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002489 clear_tv(rettv);
2490 return FAIL;
2491 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002493 evalarg_used->eval_flags = (op_falsy ? !result : result)
2494 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2495 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002496 {
2497 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002499 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002500 if (!op_falsy || !result)
2501 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002503 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002505 /*
2506 * Check for the ":".
2507 */
2508 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2509 if (*p != ':')
2510 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002511 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002512 if (evaluate && result)
2513 clear_tv(rettv);
2514 evalarg_used->eval_flags = orig_flags;
2515 return FAIL;
2516 }
2517 if (getnext)
2518 *arg = eval_next_line(evalarg_used);
2519 else
2520 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002521 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002522 {
2523 error_white_both(p, 1);
2524 clear_tv(rettv);
2525 evalarg_used->eval_flags = orig_flags;
2526 return FAIL;
2527 }
2528 *arg = p;
2529 }
2530
2531 /*
2532 * Get the third variable. Recursive!
2533 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002534 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002535 {
mityu4ac198c2021-05-28 17:52:40 +02002536 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002537 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002538 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002539 return FAIL;
2540 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002541 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2542 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002544 if (eval1(arg, &var2, evalarg_used) == FAIL)
2545 {
2546 if (evaluate && result)
2547 clear_tv(rettv);
2548 evalarg_used->eval_flags = orig_flags;
2549 return FAIL;
2550 }
2551 if (evaluate && !result)
2552 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554
2555 if (evalarg == NULL)
2556 clear_evalarg(&local_evalarg, NULL);
2557 else
2558 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561 return OK;
2562}
2563
2564/*
2565 * Handle first level expression:
2566 * expr2 || expr2 || expr2 logical OR
2567 *
2568 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002569 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 *
2571 * Return OK or FAIL.
2572 */
2573 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576 char_u *p;
2577 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579 /*
2580 * Get the first variable.
2581 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002582 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 return FAIL;
2584
2585 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002588 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 evalarg_T *evalarg_used = evalarg;
2592 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 int evaluate;
2594 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595 long result = FALSE;
2596 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002597 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002598 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002599
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 if (evalarg == NULL)
2601 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002602 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002603 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002604 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 orig_flags = evalarg_used->eval_flags;
2606 evaluate = orig_flags & EVAL_EVALUATE;
2607 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002608 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002609 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002610 result = tv_get_bool_chk(rettv, &error);
2611 else if (tv_get_number_chk(rettv, &error) != 0)
2612 result = TRUE;
2613 clear_tv(rettv);
2614 if (error)
2615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
2618 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002619 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 while (p[0] == '|' && p[1] == '|')
2622 {
2623 if (getnext)
2624 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002625 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002626 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002627 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002628 {
2629 error_white_both(p, 2);
2630 clear_tv(rettv);
2631 return FAIL;
2632 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002633 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002634 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635
2636 /*
2637 * Get the second variable.
2638 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002639 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002640 {
mityu4ac198c2021-05-28 17:52:40 +02002641 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002642 clear_tv(rettv);
2643 return FAIL;
2644 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2646 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650
2651 /*
2652 * Compute the result.
2653 */
2654 if (evaluate && !result)
2655 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002656 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002657 result = tv_get_bool_chk(&var2, &error);
2658 else if (tv_get_number_chk(&var2, &error) != 0)
2659 result = TRUE;
2660 clear_tv(&var2);
2661 if (error)
2662 return FAIL;
2663 }
2664 if (evaluate)
2665 {
2666 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002668 rettv->v_type = VAR_BOOL;
2669 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 }
2671 else
2672 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002673 rettv->v_type = VAR_NUMBER;
2674 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002675 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677
2678 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002680
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 if (evalarg == NULL)
2682 clear_evalarg(&local_evalarg, NULL);
2683 else
2684 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686
2687 return OK;
2688}
2689
2690/*
2691 * Handle second level expression:
2692 * expr3 && expr3 && expr3 logical AND
2693 *
2694 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002695 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 *
2697 * Return OK or FAIL.
2698 */
2699 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002700eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002702 char_u *p;
2703 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
2706 * Get the first variable.
2707 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return FAIL;
2710
2711 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002714 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002715 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 evalarg_T *evalarg_used = evalarg;
2718 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 int orig_flags;
2720 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 long result = TRUE;
2722 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002723 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002724 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002725
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002726 if (evalarg == NULL)
2727 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002728 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002729 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 orig_flags = evalarg_used->eval_flags;
2732 evaluate = orig_flags & EVAL_EVALUATE;
2733 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002734 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002735 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002736 result = tv_get_bool_chk(rettv, &error);
2737 else if (tv_get_number_chk(rettv, &error) == 0)
2738 result = FALSE;
2739 clear_tv(rettv);
2740 if (error)
2741 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743
2744 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002747 while (p[0] == '&' && p[1] == '&')
2748 {
2749 if (getnext)
2750 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002751 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002752 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002753 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002754 {
2755 error_white_both(p, 2);
2756 clear_tv(rettv);
2757 return FAIL;
2758 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002759 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002760 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761
2762 /*
2763 * Get the second variable.
2764 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002765 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002766 {
mityu4ac198c2021-05-28 17:52:40 +02002767 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002768 clear_tv(rettv);
2769 return FAIL;
2770 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002771 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2772 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002773 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002774 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002775 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002777
2778 /*
2779 * Compute the result.
2780 */
2781 if (evaluate && result)
2782 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002783 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002784 result = tv_get_bool_chk(&var2, &error);
2785 else if (tv_get_number_chk(&var2, &error) == 0)
2786 result = FALSE;
2787 clear_tv(&var2);
2788 if (error)
2789 return FAIL;
2790 }
2791 if (evaluate)
2792 {
2793 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002794 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002795 rettv->v_type = VAR_BOOL;
2796 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002797 }
2798 else
2799 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002800 rettv->v_type = VAR_NUMBER;
2801 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002802 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002803 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002804
2805 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002807
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002808 if (evalarg == NULL)
2809 clear_evalarg(&local_evalarg, NULL);
2810 else
2811 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813
2814 return OK;
2815}
2816
2817/*
2818 * Handle third level expression:
2819 * var1 == var2
2820 * var1 =~ var2
2821 * var1 != var2
2822 * var1 !~ var2
2823 * var1 > var2
2824 * var1 >= var2
2825 * var1 < var2
2826 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002827 * var1 is var2
2828 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 *
2830 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002831 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 *
2833 * Return OK or FAIL.
2834 */
2835 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002839 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002840 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002842 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 /*
2845 * Get the first variable.
2846 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 return FAIL;
2849
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002850 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002851 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
2853 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002854 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002856 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002858 typval_T var2;
2859 int ic;
2860 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002861 int evaluate = evalarg == NULL
2862 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002863 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002864
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002865 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002866 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002867 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002868 p = *arg;
2869 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002870 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2871 {
mityu4ac198c2021-05-28 17:52:40 +02002872 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002873 clear_tv(rettv);
2874 return FAIL;
2875 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002876
Bram Moolenaar696ba232020-07-29 21:20:41 +02002877 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2878 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002879 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002880 clear_tv(rettv);
2881 return FAIL;
2882 }
2883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (p[len] == '?')
2886 {
2887 ic = TRUE;
2888 ++len;
2889 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else if (p[len] == '#')
2892 {
2893 ic = FALSE;
2894 ++len;
2895 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002896 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002898 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 /*
2901 * Get the second variable.
2902 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002903 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2904 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002905 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002906 clear_tv(rettv);
2907 return FAIL;
2908 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002909 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 return FAIL;
2914 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002915 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002916 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002917 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002918
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002919 // use the line of the comparison for messages
2920 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002921 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002922 {
2923 ret = FAIL;
2924 clear_tv(rettv);
2925 }
2926 else
2927 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002928 clear_tv(&var2);
2929 return ret;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932
2933 return OK;
2934}
2935
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002936/*
2937 * Make a copy of blob "tv1" and append blob "tv2".
2938 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 void
2940eval_addblob(typval_T *tv1, typval_T *tv2)
2941{
2942 blob_T *b1 = tv1->vval.v_blob;
2943 blob_T *b2 = tv2->vval.v_blob;
2944 blob_T *b = blob_alloc();
2945 int i;
2946
2947 if (b != NULL)
2948 {
2949 for (i = 0; i < blob_len(b1); i++)
2950 ga_append(&b->bv_ga, blob_get(b1, i));
2951 for (i = 0; i < blob_len(b2); i++)
2952 ga_append(&b->bv_ga, blob_get(b2, i));
2953
2954 clear_tv(tv1);
2955 rettv_blob_set(tv1, b);
2956 }
2957}
2958
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002959/*
2960 * Make a copy of list "tv1" and append list "tv2".
2961 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 int
2963eval_addlist(typval_T *tv1, typval_T *tv2)
2964{
2965 typval_T var3;
2966
2967 // concatenate Lists
2968 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2969 {
2970 clear_tv(tv1);
2971 clear_tv(tv2);
2972 return FAIL;
2973 }
2974 clear_tv(tv1);
2975 *tv1 = var3;
2976 return OK;
2977}
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
2980 * Handle fourth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00002981 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002983 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002984 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 *
2986 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002987 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 *
2989 * Return OK or FAIL.
2990 */
2991 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 /*
2995 * Get the first variable.
2996 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 return FAIL;
2999
3000 /*
3001 * Repeat computing, until no '+', '-' or '.' is following.
3002 */
3003 for (;;)
3004 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003005 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003006 int getnext;
3007 char_u *p;
3008 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003009 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003010 int concat;
3011 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003012 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003013
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003014 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003015 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003016 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 p = eval_next_non_blank(*arg, evalarg, &getnext);
3018 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003019 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003020 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3021 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003023 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3024 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003025
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003026 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003027 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003029 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003030 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003031 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003032 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003033 {
mityu4ac198c2021-05-28 17:52:40 +02003034 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003035 clear_tv(rettv);
3036 return FAIL;
3037 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003038 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003039 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003040 if ((op != '+' || (rettv->v_type != VAR_LIST
3041 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#ifdef FEAT_FLOAT
3043 && (op == '.' || rettv->v_type != VAR_FLOAT)
3044#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003045 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003047 int error = FALSE;
3048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003049 // For "list + ...", an illegal use of the first operand as
3050 // a number cannot be determined before evaluating the 2nd
3051 // operand: if this is also a list, all is ok.
3052 // For "something . ...", "something - ..." or "non-list + ...",
3053 // we know that the first operand needs to be a string or number
3054 // without evaluating the 2nd operand. So check before to avoid
3055 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003056 if (op != '.')
3057 tv_get_number_chk(rettv, &error);
3058 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003059 {
3060 clear_tv(rettv);
3061 return FAIL;
3062 }
3063 }
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 /*
3066 * Get the second variable.
3067 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003068 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003069 {
mityu89dcb4d2021-05-28 13:50:17 +02003070 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003071 clear_tv(rettv);
3072 return FAIL;
3073 }
3074 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003075 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 return FAIL;
3079 }
3080
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003081 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 /*
3084 * Compute the result.
3085 */
3086 if (op == '.')
3087 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3089 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003090 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003091
Bram Moolenaar2e086612020-08-25 22:37:48 +02003092 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003093 || var2.v_type == VAR_CHANNEL
3094 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003095 semsg(_(e_using_invalid_value_as_string_str),
3096 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003097#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003098 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003099 {
3100 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3101 var2.vval.v_float);
3102 s2 = buf2;
3103 }
3104#endif
3105 else
3106 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003108 {
3109 clear_tv(rettv);
3110 clear_tv(&var2);
3111 return FAIL;
3112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003114 clear_tv(rettv);
3115 rettv->v_type = VAR_STRING;
3116 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003118 else if (op == '+' && rettv->v_type == VAR_BLOB
3119 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003121 else if (op == '+' && rettv->v_type == VAR_LIST
3122 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003123 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003125 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 else
3128 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 int error = FALSE;
3130 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003132 float_T f1 = 0, f2 = 0;
3133
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003135 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 f1 = rettv->vval.v_float;
3137 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003138 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139 else
3140#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003142 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (error)
3144 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003145 // This can only happen for "list + non-list" or
3146 // "blob + non-blob". For "non-list + ..." or
3147 // "something - ...", we returned before evaluating the
3148 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003150 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151 return FAIL;
3152 }
3153#ifdef FEAT_FLOAT
3154 if (var2.v_type == VAR_FLOAT)
3155 f1 = n1;
3156#endif
3157 }
3158#ifdef FEAT_FLOAT
3159 if (var2.v_type == VAR_FLOAT)
3160 {
3161 f2 = var2.vval.v_float;
3162 n2 = 0;
3163 }
3164 else
3165#endif
3166 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003167 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 if (error)
3169 {
3170 clear_tv(rettv);
3171 clear_tv(&var2);
3172 return FAIL;
3173 }
3174#ifdef FEAT_FLOAT
3175 if (rettv->v_type == VAR_FLOAT)
3176 f2 = n2;
3177#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180
3181#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3184 {
3185 if (op == '+')
3186 f1 = f1 + f2;
3187 else
3188 f1 = f1 - f2;
3189 rettv->v_type = VAR_FLOAT;
3190 rettv->vval.v_float = f1;
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193#endif
3194 {
3195 if (op == '+')
3196 n1 = n1 + n2;
3197 else
3198 n1 = n1 - n2;
3199 rettv->v_type = VAR_NUMBER;
3200 rettv->vval.v_number = n1;
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003203 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 }
3206 return OK;
3207}
3208
3209/*
3210 * Handle fifth level expression:
3211 * * number multiplication
3212 * / number division
3213 * % number modulo
3214 *
3215 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003216 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 *
3218 * Return OK or FAIL.
3219 */
3220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221eval6(
3222 char_u **arg,
3223 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003225 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003227#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003228 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /*
3232 * Get the first variable.
3233 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003234 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return FAIL;
3236
3237 /*
3238 * Repeat computing, until no '*', '/' or '%' is following.
3239 */
3240 for (;;)
3241 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003242 int evaluate;
3243 int getnext;
3244 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003245 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003246 int op;
3247 varnumber_T n1, n2;
3248#ifdef FEAT_FLOAT
3249 float_T f1, f2;
3250#endif
3251 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003252
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003253 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003254 p = eval_next_non_blank(*arg, evalarg, &getnext);
3255 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003256 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003258
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003259 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003260 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003261 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003262 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003263 {
3264 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3265 {
mityu4ac198c2021-05-28 17:52:40 +02003266 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003267 clear_tv(rettv);
3268 return FAIL;
3269 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003270 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003273#ifdef FEAT_FLOAT
3274 f1 = 0;
3275 f2 = 0;
3276#endif
3277 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003278 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003280#ifdef FEAT_FLOAT
3281 if (rettv->v_type == VAR_FLOAT)
3282 {
3283 f1 = rettv->vval.v_float;
3284 use_float = TRUE;
3285 n1 = 0;
3286 }
3287 else
3288#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003289 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003290 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003291 if (error)
3292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 else
3295 n1 = 0;
3296
3297 /*
3298 * Get the second variable.
3299 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003300 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3301 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003302 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003303 clear_tv(rettv);
3304 return FAIL;
3305 }
3306 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003307 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003310 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312#ifdef FEAT_FLOAT
3313 if (var2.v_type == VAR_FLOAT)
3314 {
3315 if (!use_float)
3316 {
3317 f1 = n1;
3318 use_float = TRUE;
3319 }
3320 f2 = var2.vval.v_float;
3321 n2 = 0;
3322 }
3323 else
3324#endif
3325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003326 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327 clear_tv(&var2);
3328 if (error)
3329 return FAIL;
3330#ifdef FEAT_FLOAT
3331 if (use_float)
3332 f2 = n2;
3333#endif
3334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /*
3337 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003338 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340#ifdef FEAT_FLOAT
3341 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 if (op == '*')
3344 f1 = f1 * f2;
3345 else if (op == '/')
3346 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003347# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003349 if (f2 == 0.0)
3350 {
3351 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003353 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003354 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003355 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003356 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003357 }
3358 else
3359 f1 = f1 / f2;
3360# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 // We rely on the floating point library to handle divide
3362 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003364# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003367 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003368 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003369 return FAIL;
3370 }
3371 rettv->v_type = VAR_FLOAT;
3372 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003377 int failed = FALSE;
3378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379 if (op == '*')
3380 n1 = n1 * n2;
3381 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003382 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003384 n1 = num_modulus(n1, n2, &failed);
3385 if (failed)
3386 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003387
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003388 rettv->v_type = VAR_NUMBER;
3389 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 }
3393
3394 return OK;
3395}
3396
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003397/*
3398 * Handle a type cast before a base level expression.
3399 * "arg" must point to the first non-white of the expression.
3400 * "arg" is advanced to just after the recognized expression.
3401 * Return OK or FAIL.
3402 */
3403 static int
3404eval7t(
3405 char_u **arg,
3406 typval_T *rettv,
3407 evalarg_T *evalarg,
3408 int want_string) // after "." operator
3409{
3410 type_T *want_type = NULL;
3411 garray_T type_list; // list of pointers to allocated types
3412 int res;
3413 int evaluate = evalarg == NULL ? 0
3414 : (evalarg->eval_flags & EVAL_EVALUATE);
3415
3416 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003417 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3418 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003419 {
3420 ++*arg;
3421 ga_init2(&type_list, sizeof(type_T *), 10);
3422 want_type = parse_type(arg, &type_list, TRUE);
3423 if (want_type == NULL && (evaluate || **arg != '>'))
3424 {
3425 clear_type_list(&type_list);
3426 return FAIL;
3427 }
3428
3429 if (**arg != '>')
3430 {
3431 if (*skipwhite(*arg) == '>')
3432 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3433 else
3434 emsg(_(e_missing_gt));
3435 clear_type_list(&type_list);
3436 return FAIL;
3437 }
3438 ++*arg;
3439 *arg = skipwhite_and_linebreak(*arg, evalarg);
3440 }
3441
3442 res = eval7(arg, rettv, evalarg, want_string);
3443
3444 if (want_type != NULL && evaluate)
3445 {
3446 if (res == OK)
3447 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003448 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3449 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003450
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003451 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003452 {
3453 if (want_type == &t_bool && actual != &t_bool
3454 && (actual->tt_flags & TTFLAG_BOOL_OK))
3455 {
3456 int n = tv2bool(rettv);
3457
3458 // can use "0" and "1" for boolean in some places
3459 clear_tv(rettv);
3460 rettv->v_type = VAR_BOOL;
3461 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3462 }
3463 else
3464 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003465 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003466
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003467 where.wt_variable = TRUE;
3468 res = check_type(want_type, actual, TRUE, where);
3469 }
3470 }
3471 }
3472 clear_type_list(&type_list);
3473 }
3474
3475 return res;
3476}
3477
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003478 int
3479eval_leader(char_u **arg, int vim9)
3480{
3481 char_u *s = *arg;
3482 char_u *p = *arg;
3483
3484 while (*p == '!' || *p == '-' || *p == '+')
3485 {
3486 char_u *n = skipwhite(p + 1);
3487
3488 // ++, --, -+ and +- are not accepted in Vim9 script
3489 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3490 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003491 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003492 return FAIL;
3493 }
3494 p = n;
3495 }
3496 *arg = p;
3497 return OK;
3498}
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003501 * Check for a predefined value "true", "false" and "null.*".
3502 * Return OK when recognized.
3503 */
3504 int
3505handle_predefined(char_u *s, int len, typval_T *rettv)
3506{
3507 switch (len)
3508 {
3509 case 4: if (STRNCMP(s, "true", 4) == 0)
3510 {
3511 rettv->v_type = VAR_BOOL;
3512 rettv->vval.v_number = VVAL_TRUE;
3513 return OK;
3514 }
3515 if (STRNCMP(s, "null", 4) == 0)
3516 {
3517 rettv->v_type = VAR_SPECIAL;
3518 rettv->vval.v_number = VVAL_NULL;
3519 return OK;
3520 }
3521 break;
3522 case 5: if (STRNCMP(s, "false", 5) == 0)
3523 {
3524 rettv->v_type = VAR_BOOL;
3525 rettv->vval.v_number = VVAL_FALSE;
3526 return OK;
3527 }
3528 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003529 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3530 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003531#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003532 rettv->v_type = VAR_JOB;
3533 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003534#else
3535 rettv->v_type = VAR_SPECIAL;
3536 rettv->vval.v_number = VVAL_NULL;
3537#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003538 return OK;
3539 }
3540 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003541 case 9:
3542 if (STRNCMP(s, "null_", 5) != 0)
3543 break;
3544 if (STRNCMP(s + 5, "list", 4) == 0)
3545 {
3546 rettv->v_type = VAR_LIST;
3547 rettv->vval.v_list = NULL;
3548 return OK;
3549 }
3550 if (STRNCMP(s + 5, "dict", 4) == 0)
3551 {
3552 rettv->v_type = VAR_DICT;
3553 rettv->vval.v_dict = NULL;
3554 return OK;
3555 }
3556 if (STRNCMP(s + 5, "blob", 4) == 0)
3557 {
3558 rettv->v_type = VAR_BLOB;
3559 rettv->vval.v_blob = NULL;
3560 return OK;
3561 }
3562 break;
3563 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3564 {
3565 rettv->v_type = VAR_STRING;
3566 rettv->vval.v_string = NULL;
3567 return OK;
3568 }
3569 break;
3570 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003571 if (STRNCMP(s, "null_channel", 12) == 0)
3572 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003573#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003574 rettv->v_type = VAR_CHANNEL;
3575 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003576#else
3577 rettv->v_type = VAR_SPECIAL;
3578 rettv->vval.v_number = VVAL_NULL;
3579#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003580 return OK;
3581 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003582 if (STRNCMP(s, "null_partial", 12) == 0)
3583 {
3584 rettv->v_type = VAR_PARTIAL;
3585 rettv->vval.v_partial = NULL;
3586 return OK;
3587 }
3588 break;
3589 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3590 {
3591 rettv->v_type = VAR_FUNC;
3592 rettv->vval.v_string = NULL;
3593 return OK;
3594 }
3595 break;
3596 }
3597 return FAIL;
3598}
3599
3600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 * Handle sixth level expression:
3602 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003603 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003604 * "string" string constant
3605 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 * &option-name option value
3607 * @r register contents
3608 * identifier variable value
3609 * function() function call
3610 * $VAR environment variable
3611 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003614 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003615 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 *
3617 * Also handle:
3618 * ! in front logical NOT
3619 * - in front unary minus
3620 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 * trailing [] subscript in String or List
3622 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003623 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 *
3625 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003626 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *
3628 * Return OK or FAIL.
3629 */
3630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631eval7(
3632 char_u **arg,
3633 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003634 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003637 int evaluate = evalarg != NULL
3638 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int len;
3640 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003641 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 char_u *start_leader, *end_leader;
3643 int ret = OK;
3644 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003645 static int recurse = 0;
3646 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003650 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003652 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003655 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 */
3657 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003658 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 end_leader = *arg;
3661
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003662 if (**arg == '.' && (!isdigit(*(*arg + 1))
3663#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003664 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003665#endif
3666 ))
3667 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003668 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003669 ++*arg;
3670 return FAIL;
3671 }
3672
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003673 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003674 // and crash. With MSVC the stack is smaller.
3675 if (recurse ==
3676#ifdef _MSC_VER
3677 300
3678#else
3679 1000
3680#endif
3681 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003682 {
3683 semsg(_(e_expression_too_recursive_str), *arg);
3684 return FAIL;
3685 }
3686 ++recurse;
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 switch (**arg)
3689 {
3690 /*
3691 * Number constant.
3692 */
3693 case '0':
3694 case '1':
3695 case '2':
3696 case '3':
3697 case '4':
3698 case '5':
3699 case '6':
3700 case '7':
3701 case '8':
3702 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003703 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003704
3705 // Apply prefixed "-" and "+" now. Matters especially when
3706 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003707 if (ret == OK && evaluate && end_leader > start_leader
3708 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003709 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 break;
3711
3712 /*
3713 * String constant: "string".
3714 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003715 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 break;
3717
3718 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003721 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003722 break;
3723
3724 /*
3725 * List: [expr, expr]
3726 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003727 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729
3730 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003731 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003732 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003733 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003734 {
3735 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3736 }
3737 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003738 {
3739 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003740 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003741 }
3742 else
3743 ret = NOTDONE;
3744 break;
3745
3746 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003747 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003748 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003749 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003750 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003751 ret = NOTDONE;
3752 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003753 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003754 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003755 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003756 break;
3757
3758 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003759 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003761 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 break;
3763
3764 /*
3765 * Environment variable: $VAR.
3766 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003767 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 break;
3769
3770 /*
3771 * Register contents: @r.
3772 */
3773 case '@': ++*arg;
3774 if (evaluate)
3775 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003776 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003777 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003778 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003779 emsg_invreg(**arg);
3780 else
3781 {
3782 rettv->v_type = VAR_STRING;
3783 rettv->vval.v_string = get_reg_contents(**arg,
3784 GREG_EXPR_SRC);
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 if (**arg != NUL)
3788 ++*arg;
3789 break;
3790
3791 /*
3792 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003793 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003795 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003796 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003797 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003798 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003799 if (ret == OK && evaluate)
3800 {
3801 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3802
Bram Moolenaara9931532021-06-12 15:58:16 +02003803 // Compile it here to get the return type. The return
3804 // type is optional, when it's missing use t_unknown.
3805 // This is recognized in compile_return().
3806 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3807 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003808 if (compile_def_function(ufunc, FALSE,
3809 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003810 {
3811 clear_tv(rettv);
3812 ret = FAIL;
3813 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003814 }
3815 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003816 if (ret == NOTDONE)
3817 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003818 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003819 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003820
Bram Moolenaar9215f012020-06-27 21:18:00 +02003821 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003822 if (**arg == ')')
3823 ++*arg;
3824 else if (ret == OK)
3825 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003826 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003827 clear_tv(rettv);
3828 ret = FAIL;
3829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 break;
3832
Bram Moolenaar8c711452005-01-14 21:53:12 +00003833 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 break;
3835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836
3837 if (ret == NOTDONE)
3838 {
3839 /*
3840 * Must be a variable or function name.
3841 * Can also be a curly-braces kind of name: {expr}.
3842 */
3843 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003844 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845 if (alias != NULL)
3846 s = alias;
3847
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003848 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003849 ret = FAIL;
3850 else
3851 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003852 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3853
Bram Moolenaar4525a572022-02-13 11:57:33 +00003854 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003855 {
3856 emsg(_(e_cannot_use_underscore_here));
3857 ret = FAIL;
3858 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003859 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003860 && s[0] == 's' && s[1] == ':')
3861 {
3862 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3863 ret = FAIL;
3864 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003865 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003866 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003867 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003868 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003869 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003870 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003871 else if (flags & EVAL_CONSTANT)
3872 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003873 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003874 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003875 // get the value of "true", "false", etc. or a variable
3876 ret = FAIL;
3877 if (vim9script)
3878 ret = handle_predefined(s, len, rettv);
3879 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003880 {
3881 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003882 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003883 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003884 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003885 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003886 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003887 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003888 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003889 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003890 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003892 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003893 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003894 }
3895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003896 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3897 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003898 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003899 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 /*
3902 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3903 */
3904 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003905 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003906
3907 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003908 return ret;
3909}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003910
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003911/*
3912 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003913 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003914 * Adjusts "end_leaderp" until it is at "start_leader".
3915 */
3916 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003917eval7_leader(
3918 typval_T *rettv,
3919 int numeric_only,
3920 char_u *start_leader,
3921 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003922{
3923 char_u *end_leader = *end_leaderp;
3924 int ret = OK;
3925 int error = FALSE;
3926 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003927 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003928 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003929#ifdef FEAT_FLOAT
3930 float_T f = 0.0;
3931
3932 if (rettv->v_type == VAR_FLOAT)
3933 f = rettv->vval.v_float;
3934 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003935#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003936 {
3937 while (VIM_ISWHITE(end_leader[-1]))
3938 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003939 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003940 val = tv2bool(rettv);
3941 else
3942 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003943 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003944 if (error)
3945 {
3946 clear_tv(rettv);
3947 ret = FAIL;
3948 }
3949 else
3950 {
3951 while (end_leader > start_leader)
3952 {
3953 --end_leader;
3954 if (*end_leader == '!')
3955 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003956 if (numeric_only)
3957 {
3958 ++end_leader;
3959 break;
3960 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003961#ifdef FEAT_FLOAT
3962 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003963 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003964 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003965 {
3966 rettv->v_type = VAR_BOOL;
3967 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3968 }
3969 else
3970 f = !f;
3971 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003972 else
3973#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003974 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003975 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003976 type = VAR_BOOL;
3977 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003978 }
3979 else if (*end_leader == '-')
3980 {
3981#ifdef FEAT_FLOAT
3982 if (rettv->v_type == VAR_FLOAT)
3983 f = -f;
3984 else
3985#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003986 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003987 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003988 type = VAR_NUMBER;
3989 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003990 }
3991 }
3992#ifdef FEAT_FLOAT
3993 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003996 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003999#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004000 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004001 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004002 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004003 rettv->v_type = type;
4004 else
4005 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004006 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004009 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return ret;
4011}
4012
4013/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004014 * Call the function referred to in "rettv".
4015 */
4016 static int
4017call_func_rettv(
4018 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004019 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004020 typval_T *rettv,
4021 int evaluate,
4022 dict_T *selfdict,
4023 typval_T *basetv)
4024{
4025 partial_T *pt = NULL;
4026 funcexe_T funcexe;
4027 typval_T functv;
4028 char_u *s;
4029 int ret;
4030
4031 // need to copy the funcref so that we can clear rettv
4032 if (evaluate)
4033 {
4034 functv = *rettv;
4035 rettv->v_type = VAR_UNKNOWN;
4036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004038 if (functv.v_type == VAR_PARTIAL)
4039 {
4040 pt = functv.vval.v_partial;
4041 s = partial_name(pt);
4042 }
4043 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004044 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004045 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004046 if (s == NULL || *s == NUL)
4047 {
4048 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004049 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004050 goto theend;
4051 }
4052 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004053 }
4054 else
4055 s = (char_u *)"";
4056
Bram Moolenaara80faa82020-04-12 19:37:17 +02004057 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004058 funcexe.fe_firstline = curwin->w_cursor.lnum;
4059 funcexe.fe_lastline = curwin->w_cursor.lnum;
4060 funcexe.fe_evaluate = evaluate;
4061 funcexe.fe_partial = pt;
4062 funcexe.fe_selfdict = selfdict;
4063 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004064 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004065
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004066theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004067 // Clear the funcref afterwards, so that deleting it while
4068 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004069 if (evaluate)
4070 clear_tv(&functv);
4071
4072 return ret;
4073}
4074
4075/*
4076 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004077 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004078 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4079 */
4080 static int
4081eval_lambda(
4082 char_u **arg,
4083 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004084 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004085 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004086{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004087 int evaluate = evalarg != NULL
4088 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004089 typval_T base = *rettv;
4090 int ret;
4091
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004092 rettv->v_type = VAR_UNKNOWN;
4093
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004094 if (**arg == '{')
4095 {
4096 // ->{lambda}()
4097 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4098 }
4099 else
4100 {
4101 // ->(lambda)()
4102 ++*arg;
4103 ret = eval1(arg, rettv, evalarg);
4104 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004105 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004106 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004107 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004108 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004109 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004110 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4111 && rettv->v_type != VAR_PARTIAL)
4112 {
4113 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4114 return FAIL;
4115 }
4116 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004117 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004118 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004119 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004120
4121 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004122 {
4123 if (verbose)
4124 {
4125 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004126 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004127 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004128 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004129 }
4130 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004131 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004132 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004133 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004134 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004135
4136 // Clear the funcref afterwards, so that deleting it while
4137 // evaluating the arguments is possible (see test55).
4138 if (evaluate)
4139 clear_tv(&base);
4140
4141 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004142}
4143
4144/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004145 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004146 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004147 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4148 */
4149 static int
4150eval_method(
4151 char_u **arg,
4152 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004153 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004155{
4156 char_u *name;
4157 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004158 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004159 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004160 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004161 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004162 int evaluate = evalarg != NULL
4163 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004164
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004165 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004166
Bram Moolenaarac92e252019-08-03 21:58:38 +02004167 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004168 len = get_name_len(arg, &alias, evaluate, TRUE);
4169 if (alias != NULL)
4170 name = alias;
4171
4172 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004173 {
4174 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004175 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004176 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004177 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004178 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004179 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004180 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004181
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004182 // If there is no "(" immediately following, but there is further on,
4183 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4184 // Does not handle anything where "(" is part of the expression.
4185 *arg = skipwhite(*arg);
4186
4187 if (**arg != '(' && alias == NULL
4188 && (paren = vim_strchr(*arg, '(')) != NULL)
4189 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004190 char_u *deref;
4191
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004192 *arg = name;
4193 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004194 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4195 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004196 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004197 *arg = name + len;
4198 ret = FAIL;
4199 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004200 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004201 {
4202 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004203 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004204 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004205 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004206 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004207
4208 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004209 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004210 *arg = skipwhite(*arg);
4211
4212 if (**arg != '(')
4213 {
4214 if (verbose)
4215 semsg(_(e_missing_parenthesis_str), name);
4216 ret = FAIL;
4217 }
4218 else if (VIM_ISWHITE((*arg)[-1]))
4219 {
4220 if (verbose)
4221 emsg(_(e_no_white_space_allowed_before_parenthesis));
4222 ret = FAIL;
4223 }
4224 else
4225 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004226 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004227 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004228 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004229
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004230 // Clear the funcref afterwards, so that deleting it while
4231 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004232 if (evaluate)
4233 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004234 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004235
Bram Moolenaarac92e252019-08-03 21:58:38 +02004236 return ret;
4237}
4238
4239/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004240 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4241 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004242 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4243 */
4244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245eval_index(
4246 char_u **arg,
4247 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004248 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004250{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004251 int evaluate = evalarg != NULL
4252 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004253 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004254 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004255 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004257 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004258 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4261 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004262
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004263 init_tv(&var1);
4264 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004265 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004267 /*
4268 * dict.name
4269 */
4270 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004271 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004272 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004273 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004275 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004276 }
4277 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004279 /*
4280 * something[idx]
4281 *
4282 * Get the (first) variable from inside the [].
4283 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004284 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004285 if (**arg == ':')
4286 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004287 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004288 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004289 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004290 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004291 semsg(_(e_white_space_required_before_and_after_str_at_str),
4292 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004293 clear_tv(&var1);
4294 return FAIL;
4295 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004296 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004298 int error = FALSE;
4299
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004300#ifdef FEAT_FLOAT
4301 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004302 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004303 && var1.v_type == VAR_FLOAT)
4304 {
4305 var1.vval.v_string = typval_tostring(&var1, TRUE);
4306 var1.v_type = VAR_STRING;
4307 }
4308#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004309 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004310 tv_get_number_chk(&var1, &error);
4311 else
4312 error = tv_get_string_chk(&var1) == NULL;
4313 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004314 {
4315 // not a number or string
4316 clear_tv(&var1);
4317 return FAIL;
4318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004320
4321 /*
4322 * Get the second variable from inside the [:].
4323 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004324 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004325 if (**arg == ':')
4326 {
4327 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004328 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004329 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004330 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004331 semsg(_(e_white_space_required_before_and_after_str_at_str),
4332 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004333 if (!empty1)
4334 clear_tv(&var1);
4335 return FAIL;
4336 }
4337 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004338 if (**arg == ']')
4339 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004340 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (!empty1)
4343 clear_tv(&var1);
4344 return FAIL;
4345 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004346 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004348 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (!empty1)
4350 clear_tv(&var1);
4351 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004352 return FAIL;
4353 }
4354 }
4355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004356 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004357 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 if (**arg != ']')
4359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004360 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004361 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004362 clear_tv(&var1);
4363 if (range)
4364 clear_tv(&var2);
4365 return FAIL;
4366 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004367 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004368 }
4369
4370 if (evaluate)
4371 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004372 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004373 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004374 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004375
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004376 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004378 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004379 clear_tv(&var2);
4380 return res;
4381 }
4382 return OK;
4383}
4384
4385/*
4386 * Check if "rettv" can have an [index] or [sli:ce]
4387 */
4388 int
4389check_can_index(typval_T *rettv, int evaluate, int verbose)
4390{
4391 switch (rettv->v_type)
4392 {
4393 case VAR_FUNC:
4394 case VAR_PARTIAL:
4395 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004396 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004397 return FAIL;
4398 case VAR_FLOAT:
4399#ifdef FEAT_FLOAT
4400 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004401 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004402 return FAIL;
4403#endif
4404 case VAR_BOOL:
4405 case VAR_SPECIAL:
4406 case VAR_JOB:
4407 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004408 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004409 if (verbose)
4410 emsg(_(e_cannot_index_special_variable));
4411 return FAIL;
4412 case VAR_UNKNOWN:
4413 case VAR_ANY:
4414 case VAR_VOID:
4415 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004416 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004417 emsg(_(e_cannot_index_special_variable));
4418 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004420 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004421
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004422 case VAR_STRING:
4423 case VAR_LIST:
4424 case VAR_DICT:
4425 case VAR_BLOB:
4426 break;
4427 case VAR_NUMBER:
4428 if (in_vim9script())
4429 emsg(_(e_cannot_index_number));
4430 break;
4431 }
4432 return OK;
4433}
4434
4435/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004436 * slice() function
4437 */
4438 void
4439f_slice(typval_T *argvars, typval_T *rettv)
4440{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004441 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004442 && ((argvars[0].v_type != VAR_STRING
4443 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004444 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004445 && check_for_list_arg(argvars, 0) == FAIL)
4446 || check_for_number_arg(argvars, 1) == FAIL
4447 || check_for_opt_number_arg(argvars, 2) == FAIL))
4448 return;
4449
Bram Moolenaar6601b622021-01-13 21:47:15 +01004450 if (check_can_index(argvars, TRUE, FALSE) == OK)
4451 {
4452 copy_tv(argvars, rettv);
4453 eval_index_inner(rettv, TRUE, argvars + 1,
4454 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4455 TRUE, NULL, 0, FALSE);
4456 }
4457}
4458
4459/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004460 * Apply index or range to "rettv".
4461 * "var1" is the first index, NULL for [:expr].
4462 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004463 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4464 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004465 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4466 */
4467 int
4468eval_index_inner(
4469 typval_T *rettv,
4470 int is_range,
4471 typval_T *var1,
4472 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004473 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004474 char_u *key,
4475 int keylen,
4476 int verbose)
4477{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004478 varnumber_T n1, n2 = 0;
4479 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004480
4481 n1 = 0;
4482 if (var1 != NULL && rettv->v_type != VAR_DICT)
4483 n1 = tv_get_number(var1);
4484
4485 if (is_range)
4486 {
4487 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004488 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004489 if (verbose)
4490 emsg(_(e_cannot_slice_dictionary));
4491 return FAIL;
4492 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004493 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004494 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004495 else
4496 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004497 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004498
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004499 switch (rettv->v_type)
4500 {
4501 case VAR_UNKNOWN:
4502 case VAR_ANY:
4503 case VAR_VOID:
4504 case VAR_FUNC:
4505 case VAR_PARTIAL:
4506 case VAR_FLOAT:
4507 case VAR_BOOL:
4508 case VAR_SPECIAL:
4509 case VAR_JOB:
4510 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004511 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004512 break; // not evaluating, skipping over subscript
4513
4514 case VAR_NUMBER:
4515 case VAR_STRING:
4516 {
4517 char_u *s = tv_get_string(rettv);
4518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004519 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004520 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004521 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004522 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004523 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004524 else
4525 s = char_from_string(s, n1);
4526 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004527 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 // The resulting variable is a substring. If the indexes
4530 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 if (n1 < 0)
4532 {
4533 n1 = len + n1;
4534 if (n1 < 0)
4535 n1 = 0;
4536 }
4537 if (n2 < 0)
4538 n2 = len + n2;
4539 else if (n2 >= len)
4540 n2 = len;
4541 if (n1 >= len || n2 < 0 || n1 > n2)
4542 s = NULL;
4543 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004544 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 }
4546 else
4547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // The resulting variable is a string of a single
4549 // character. If the index is too big or negative the
4550 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 if (n1 >= len || n1 < 0)
4552 s = NULL;
4553 else
4554 s = vim_strnsave(s + n1, 1);
4555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004556 clear_tv(rettv);
4557 rettv->v_type = VAR_STRING;
4558 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004559 }
4560 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004562 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004563 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4564 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004565 break;
4566
4567 case VAR_LIST:
4568 if (var1 == NULL)
4569 n1 = 0;
4570 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004571 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004573 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 return FAIL;
4575 break;
4576
4577 case VAR_DICT:
4578 {
4579 dictitem_T *item;
4580 typval_T tmp;
4581
4582 if (key == NULL)
4583 {
4584 key = tv_get_string_chk(var1);
4585 if (key == NULL)
4586 return FAIL;
4587 }
4588
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004589 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004590
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004591 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004592 {
4593 if (verbose)
4594 {
4595 if (keylen > 0)
4596 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004597 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004598 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004599 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004600 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004601
4602 copy_tv(&item->di_tv, &tmp);
4603 clear_tv(rettv);
4604 *rettv = tmp;
4605 }
4606 break;
4607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 return OK;
4609}
4610
4611/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004612 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004613 */
4614 char_u *
4615partial_name(partial_T *pt)
4616{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004617 if (pt != NULL)
4618 {
4619 if (pt->pt_name != NULL)
4620 return pt->pt_name;
4621 if (pt->pt_func != NULL)
4622 return pt->pt_func->uf_name;
4623 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004624 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004625}
4626
Bram Moolenaarddecc252016-04-06 22:59:37 +02004627 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004628partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004629{
4630 int i;
4631
4632 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004633 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004634 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004635 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004636 if (pt->pt_name != NULL)
4637 {
4638 func_unref(pt->pt_name);
4639 vim_free(pt->pt_name);
4640 }
4641 else
4642 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004643
Bram Moolenaar54656012021-06-09 20:50:46 +02004644 // "out_up" is no longer used, decrement refcount on partial that owns it.
4645 partial_unref(pt->pt_outer.out_up_partial);
4646
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004647 // Using pt_outer from another partial.
4648 partial_unref(pt->pt_outer_partial);
4649
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004650 // Decrease the reference count for the context of a closure. If down
4651 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004652 if (pt->pt_funcstack != NULL)
4653 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004654 --pt->pt_funcstack->fs_refcount;
4655 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004656 }
4657
Bram Moolenaarddecc252016-04-06 22:59:37 +02004658 vim_free(pt);
4659}
4660
4661/*
4662 * Unreference a closure: decrement the reference count and free it when it
4663 * becomes zero.
4664 */
4665 void
4666partial_unref(partial_T *pt)
4667{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004668 if (pt != NULL)
4669 {
4670 if (--pt->pt_refcount <= 0)
4671 partial_free(pt);
4672
4673 // If the reference count goes down to one, the funcstack may be the
4674 // only reference and can be freed if no other partials reference it.
4675 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4676 funcstack_check_refcount(pt->pt_funcstack);
4677 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004678}
4679
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004680/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004681 * Return the next (unique) copy ID.
4682 * Used for serializing nested structures.
4683 */
4684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004686{
4687 current_copyID += COPYID_INC;
4688 return current_copyID;
4689}
4690
4691/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004692 * Garbage collection for lists and dictionaries.
4693 *
4694 * We use reference counts to be able to free most items right away when they
4695 * are no longer used. But for composite items it's possible that it becomes
4696 * unused while the reference count is > 0: When there is a recursive
4697 * reference. Example:
4698 * :let l = [1, 2, 3]
4699 * :let d = {9: l}
4700 * :let l[1] = d
4701 *
4702 * Since this is quite unusual we handle this with garbage collection: every
4703 * once in a while find out which lists and dicts are not referenced from any
4704 * variable.
4705 *
4706 * Here is a good reference text about garbage collection (refers to Python
4707 * but it applies to all reference-counting mechanisms):
4708 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004709 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004710
4711/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004712 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004713 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004714 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004715 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004717garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004718{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004719 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004720 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004721 buf_T *buf;
4722 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004723 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004724 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004725
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004726 if (!testing)
4727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004729 want_garbage_collect = FALSE;
4730 may_garbage_collect = FALSE;
4731 garbage_collect_at_exit = FALSE;
4732 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004733
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004734 // The execution stack can grow big, limit the size.
4735 if (exestack.ga_maxlen - exestack.ga_len > 500)
4736 {
4737 size_t new_len;
4738 char_u *pp;
4739 int n;
4740
4741 // Keep 150% of the current size, with a minimum of the growth size.
4742 n = exestack.ga_len / 2;
4743 if (n < exestack.ga_growsize)
4744 n = exestack.ga_growsize;
4745
4746 // Don't make it bigger though.
4747 if (exestack.ga_len + n < exestack.ga_maxlen)
4748 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004749 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004750 pp = vim_realloc(exestack.ga_data, new_len);
4751 if (pp == NULL)
4752 return FAIL;
4753 exestack.ga_maxlen = exestack.ga_len + n;
4754 exestack.ga_data = pp;
4755 }
4756 }
4757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 // We advance by two because we add one for items referenced through
4759 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004760 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004761
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004762 /*
4763 * 1. Go through all accessible variables and mark all lists and dicts
4764 * with copyID.
4765 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // Don't free variables in the previous_funccal list unless they are only
4768 // referenced through previous_funccal. This must be first, because if
4769 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004770 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004772 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004773 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004776 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004777 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4778 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004782 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4783 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004784 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004785 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4786 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004787#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004788 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004789 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4790 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004791 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004792 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004793 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4794 NULL, NULL);
4795#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004798 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004799 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4800 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004802 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004804 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004805 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004808 abort = abort || set_ref_in_functions(copyID);
4809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004812
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004813 // funcstacks keep variables for closures
4814 abort = abort || set_ref_in_funcstacks(copyID);
4815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004817 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004818
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004819 // callbacks in buffers
4820 abort = abort || set_ref_in_buffers(copyID);
4821
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004822 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4823 abort = abort || set_ref_in_insexpand_funcs(copyID);
4824
4825 // 'operatorfunc' callback
4826 abort = abort || set_ref_in_opfunc(copyID);
4827
4828 // 'tagfunc' callback
4829 abort = abort || set_ref_in_tagfunc(copyID);
4830
4831 // 'imactivatefunc' and 'imstatusfunc' callbacks
4832 abort = abort || set_ref_in_im_funcs(copyID);
4833
Bram Moolenaar1dced572012-04-05 16:54:08 +02004834#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004835 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004836#endif
4837
Bram Moolenaardb913952012-06-29 12:54:53 +02004838#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004839 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004840#endif
4841
4842#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004843 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004844#endif
4845
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004846#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004847 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004848 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004849#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004850#ifdef FEAT_NETBEANS_INTG
4851 abort = abort || set_ref_in_nb_channel(copyID);
4852#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004853
Bram Moolenaare3188e22016-05-31 21:13:04 +02004854#ifdef FEAT_TIMERS
4855 abort = abort || set_ref_in_timer(copyID);
4856#endif
4857
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004858#ifdef FEAT_QUICKFIX
4859 abort = abort || set_ref_in_quickfix(copyID);
4860#endif
4861
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004862#ifdef FEAT_TERMINAL
4863 abort = abort || set_ref_in_term(copyID);
4864#endif
4865
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004866#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004867 abort = abort || set_ref_in_popups(copyID);
4868#endif
4869
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004870 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004871 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004872 /*
4873 * 2. Free lists and dictionaries that are not referenced.
4874 */
4875 did_free = free_unref_items(copyID);
4876
4877 /*
4878 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004879 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004880 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004882 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004883 else if (p_verbose > 0)
4884 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004885 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004886 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004887
4888 return did_free;
4889}
4890
4891/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004892 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004893 */
4894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004896{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004897 int did_free = FALSE;
4898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // Let all "free" functions know that we are here. This means no
4900 // dictionaries, lists, channels or jobs are to be freed, because we will
4901 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004902 in_free_unref_items = TRUE;
4903
4904 /*
4905 * PASS 1: free the contents of the items. We don't free the items
4906 * themselves yet, so that it is possible to decrement refcount counters
4907 */
4908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004910 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004913 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004914
4915#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 // Go through the list of jobs and free items without the copyID. This
4917 // must happen before doing channels, because jobs refer to channels, but
4918 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004919 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004922 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4923#endif
4924
4925 /*
4926 * PASS 2: free the items themselves.
4927 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004928 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004929 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004930
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004931#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 // Go through the list of jobs and free items without the copyID. This
4933 // must happen before doing channels, because jobs refer to channels, but
4934 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004935 free_unused_jobs(copyID, COPYID_MASK);
4936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004938 free_unused_channels(copyID, COPYID_MASK);
4939#endif
4940
4941 in_free_unref_items = FALSE;
4942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004943 return did_free;
4944}
4945
4946/*
4947 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004948 * "list_stack" is used to add lists to be marked. Can be NULL.
4949 *
4950 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004951 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004954{
4955 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004956 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004957 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004958 hashtab_T *cur_ht;
4959 ht_stack_T *ht_stack = NULL;
4960 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004962 cur_ht = ht;
4963 for (;;)
4964 {
4965 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 // Mark each item in the hashtab. If the item contains a hashtab
4968 // it is added to ht_stack, if it contains a list it is added to
4969 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004970 todo = (int)cur_ht->ht_used;
4971 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4972 if (!HASHITEM_EMPTY(hi))
4973 {
4974 --todo;
4975 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4976 &ht_stack, list_stack);
4977 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004978 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004979
4980 if (ht_stack == NULL)
4981 break;
4982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004984 cur_ht = ht_stack->ht;
4985 tempitem = ht_stack;
4986 ht_stack = ht_stack->prev;
4987 free(tempitem);
4988 }
4989
4990 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004991}
4992
Dominique Pelle748b3082022-01-08 12:41:16 +00004993#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4994 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004995/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004996 * Mark a dict and its items with "copyID".
4997 * Returns TRUE if setting references failed somehow.
4998 */
4999 int
5000set_ref_in_dict(dict_T *d, int copyID)
5001{
5002 if (d != NULL && d->dv_copyID != copyID)
5003 {
5004 d->dv_copyID = copyID;
5005 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5006 }
5007 return FALSE;
5008}
Dominique Pelle748b3082022-01-08 12:41:16 +00005009#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005010
5011/*
5012 * Mark a list and its items with "copyID".
5013 * Returns TRUE if setting references failed somehow.
5014 */
5015 int
5016set_ref_in_list(list_T *ll, int copyID)
5017{
5018 if (ll != NULL && ll->lv_copyID != copyID)
5019 {
5020 ll->lv_copyID = copyID;
5021 return set_ref_in_list_items(ll, copyID, NULL);
5022 }
5023 return FALSE;
5024}
5025
5026/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005027 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005028 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5029 *
5030 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005031 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005032 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005033set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005034{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005035 listitem_T *li;
5036 int abort = FALSE;
5037 list_T *cur_l;
5038 list_stack_T *list_stack = NULL;
5039 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005040
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 cur_l = l;
5042 for (;;)
5043 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005044 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // Mark each item in the list. If the item contains a hashtab
5046 // it is added to ht_stack, if it contains a list it is added to
5047 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005048 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5049 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5050 ht_stack, &list_stack);
5051 if (list_stack == NULL)
5052 break;
5053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005055 cur_l = list_stack->list;
5056 tempitem = list_stack;
5057 list_stack = list_stack->prev;
5058 free(tempitem);
5059 }
5060
5061 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005062}
5063
5064/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005065 * Mark the partial in callback 'cb' with "copyID".
5066 */
5067 int
5068set_ref_in_callback(callback_T *cb, int copyID)
5069{
5070 typval_T tv;
5071
5072 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5073 return FALSE;
5074
5075 tv.v_type = VAR_PARTIAL;
5076 tv.vval.v_partial = cb->cb_partial;
5077 return set_ref_in_item(&tv, copyID, NULL, NULL);
5078}
5079
5080/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005081 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 * "list_stack" is used to add lists to be marked. Can be NULL.
5083 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5084 *
5085 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005086 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005088set_ref_in_item(
5089 typval_T *tv,
5090 int copyID,
5091 ht_stack_T **ht_stack,
5092 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005093{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005094 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005095
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005097 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098 dict_T *dd = tv->vval.v_dict;
5099
Bram Moolenaara03f2332016-02-06 18:09:59 +01005100 if (dd != NULL && dd->dv_copyID != copyID)
5101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005103 dd->dv_copyID = copyID;
5104 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005105 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005106 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5107 }
5108 else
5109 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005110 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5111
Bram Moolenaara03f2332016-02-06 18:09:59 +01005112 if (newitem == NULL)
5113 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005114 else
5115 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005116 newitem->ht = &dd->dv_hashtab;
5117 newitem->prev = *ht_stack;
5118 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005119 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005120 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005121 }
5122 }
5123 else if (tv->v_type == VAR_LIST)
5124 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125 list_T *ll = tv->vval.v_list;
5126
Bram Moolenaara03f2332016-02-06 18:09:59 +01005127 if (ll != NULL && ll->lv_copyID != copyID)
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005130 ll->lv_copyID = copyID;
5131 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005132 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005133 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005134 }
5135 else
5136 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005137 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5138
Bram Moolenaara03f2332016-02-06 18:09:59 +01005139 if (newitem == NULL)
5140 abort = TRUE;
5141 else
5142 {
5143 newitem->list = ll;
5144 newitem->prev = *list_stack;
5145 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005147 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005149 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005150 else if (tv->v_type == VAR_FUNC)
5151 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005152 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005153 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005154 else if (tv->v_type == VAR_PARTIAL)
5155 {
5156 partial_T *pt = tv->vval.v_partial;
5157 int i;
5158
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005159 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005160 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005161 // Didn't see this partial yet.
5162 pt->pt_copyID = copyID;
5163
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005164 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005165
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166 if (pt->pt_dict != NULL)
5167 {
5168 typval_T dtv;
5169
5170 dtv.v_type = VAR_DICT;
5171 dtv.vval.v_dict = pt->pt_dict;
5172 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5173 }
5174
5175 for (i = 0; i < pt->pt_argc; ++i)
5176 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5177 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005178 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005179 }
5180 }
5181#ifdef FEAT_JOB_CHANNEL
5182 else if (tv->v_type == VAR_JOB)
5183 {
5184 job_T *job = tv->vval.v_job;
5185 typval_T dtv;
5186
5187 if (job != NULL && job->jv_copyID != copyID)
5188 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005189 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005190 if (job->jv_channel != NULL)
5191 {
5192 dtv.v_type = VAR_CHANNEL;
5193 dtv.vval.v_channel = job->jv_channel;
5194 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5195 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005196 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005197 {
5198 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005199 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005200 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5201 }
5202 }
5203 }
5204 else if (tv->v_type == VAR_CHANNEL)
5205 {
5206 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005207 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005208 typval_T dtv;
5209 jsonq_T *jq;
5210 cbq_T *cq;
5211
5212 if (ch != NULL && ch->ch_copyID != copyID)
5213 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005214 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005215 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005216 {
5217 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5218 jq = jq->jq_next)
5219 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5220 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5221 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005222 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005223 {
5224 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005225 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005226 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5227 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005228 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005229 {
5230 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005231 dtv.vval.v_partial =
5232 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005233 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5234 }
5235 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005236 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005237 {
5238 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005239 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005240 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5241 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005242 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005243 {
5244 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005245 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005246 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5247 }
5248 }
5249 }
5250#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005251 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005252}
5253
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 * Return a string with the string representation of a variable.
5256 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005257 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005258 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005259 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005260 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005261 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005262 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5263 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005264 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005266 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005267echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005268 typval_T *tv,
5269 char_u **tofree,
5270 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005271 int copyID,
5272 int echo_style,
5273 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005274 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005276 static int recurse = 0;
5277 char_u *r = NULL;
5278
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005280 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005281 if (!did_echo_string_emsg)
5282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 // Only give this message once for a recursive call to avoid
5284 // flooding the user with errors. And stop iterating over lists
5285 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005286 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005287 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005289 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005290 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005291 }
5292 ++recurse;
5293
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 switch (tv->v_type)
5295 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005296 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005297 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005298 {
5299 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005300 r = tv->vval.v_string;
5301 if (r == NULL)
5302 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005303 }
5304 else
5305 {
5306 *tofree = string_quote(tv->vval.v_string, FALSE);
5307 r = *tofree;
5308 }
5309 break;
5310
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005312 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005313 char_u buf[MAX_FUNC_NAME_LEN];
5314
5315 if (echo_style)
5316 {
5317 r = make_ufunc_name_readable(tv->vval.v_string,
5318 buf, MAX_FUNC_NAME_LEN);
5319 if (r == buf)
5320 {
5321 r = vim_strsave(buf);
5322 *tofree = r;
5323 }
5324 else
5325 *tofree = NULL;
5326 }
5327 else
5328 {
5329 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5330 : make_ufunc_name_readable(
5331 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5332 TRUE);
5333 r = *tofree;
5334 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005336 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005337
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005338 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005339 {
5340 partial_T *pt = tv->vval.v_partial;
5341 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005342 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005343 garray_T ga;
5344 int i;
5345 char_u *tf;
5346
5347 ga_init2(&ga, 1, 100);
5348 ga_concat(&ga, (char_u *)"function(");
5349 if (fname != NULL)
5350 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005351 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005352 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005353 && vim_isupper(fname[1]))
5354 {
5355 ga_concat(&ga, (char_u *)"'g:");
5356 ga_concat(&ga, fname + 1);
5357 }
5358 else
5359 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005360 vim_free(fname);
5361 }
5362 if (pt != NULL && pt->pt_argc > 0)
5363 {
5364 ga_concat(&ga, (char_u *)", [");
5365 for (i = 0; i < pt->pt_argc; ++i)
5366 {
5367 if (i > 0)
5368 ga_concat(&ga, (char_u *)", ");
5369 ga_concat(&ga,
5370 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5371 vim_free(tf);
5372 }
5373 ga_concat(&ga, (char_u *)"]");
5374 }
5375 if (pt != NULL && pt->pt_dict != NULL)
5376 {
5377 typval_T dtv;
5378
5379 ga_concat(&ga, (char_u *)", ");
5380 dtv.v_type = VAR_DICT;
5381 dtv.vval.v_dict = pt->pt_dict;
5382 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5383 vim_free(tf);
5384 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005385 // terminate with ')' and a NUL
5386 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005387
5388 *tofree = ga.ga_data;
5389 r = *tofree;
5390 break;
5391 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005392
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005393 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005394 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005395 break;
5396
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005398 if (tv->vval.v_list == NULL)
5399 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005400 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005401 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005402 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005403 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005404 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5405 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005406 {
5407 *tofree = NULL;
5408 r = (char_u *)"[...]";
5409 }
5410 else
5411 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005412 int old_copyID = tv->vval.v_list->lv_copyID;
5413
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005414 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005415 *tofree = list2string(tv, copyID, restore_copyID);
5416 if (restore_copyID)
5417 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005418 r = *tofree;
5419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005420 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005421
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005423 if (tv->vval.v_dict == NULL)
5424 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005425 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005426 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005427 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005428 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005429 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5430 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005431 {
5432 *tofree = NULL;
5433 r = (char_u *)"{...}";
5434 }
5435 else
5436 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005437 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005438
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005439 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005440 *tofree = dict2string(tv, copyID, restore_copyID);
5441 if (restore_copyID)
5442 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005443 r = *tofree;
5444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005445 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005448 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005449 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005451 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005452 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005453 break;
5454
Bram Moolenaar835dc632016-02-07 14:27:38 +01005455 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005456 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005458 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005459 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5460 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005461 if (composite_val)
5462 {
5463 *tofree = string_quote(r, FALSE);
5464 r = *tofree;
5465 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005466#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005468
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005469 case VAR_INSTR:
5470 *tofree = NULL;
5471 r = (char_u *)"instructions";
5472 break;
5473
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005474 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005476 *tofree = NULL;
5477 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5478 r = numbuf;
5479 break;
5480#endif
5481
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005482 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005483 case VAR_SPECIAL:
5484 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005485 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005486 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005488
Bram Moolenaar8502c702014-06-17 12:51:16 +02005489 if (--recurse == 0)
5490 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005491 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005492}
5493
5494/*
5495 * Return a string with the string representation of a variable.
5496 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5497 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005498 * Does not put quotes around strings, as ":echo" displays values.
5499 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5500 * May return NULL.
5501 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005502 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005503echo_string(
5504 typval_T *tv,
5505 char_u **tofree,
5506 char_u *numbuf,
5507 int copyID)
5508{
5509 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5510}
5511
5512/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005513 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5514 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005515 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005516 */
5517 int
5518buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5519{
5520 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005521 char_u *t;
5522 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005523
5524 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5525 return -1;
5526
5527 if (lnum > buf->b_ml.ml_line_count)
5528 lnum = buf->b_ml.ml_line_count;
5529
5530 str = ml_get_buf(buf, lnum, FALSE);
5531 if (str == NULL)
5532 return -1;
5533
5534 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005535 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005536
Bram Moolenaar91458462021-01-13 20:08:38 +01005537 // count the number of characters
5538 t = str;
5539 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5540 t += mb_ptr2len(t);
5541
5542 // In insert mode, when the cursor is at the end of a non-empty line,
5543 // byteidx points to the NUL character immediately past the end of the
5544 // string. In this case, add one to the character count.
5545 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5546 count++;
5547
5548 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005549}
5550
5551/*
5552 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005553 * byte index. Works only for loaded buffers. Returns -1 on failure.
5554 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005555 */
5556 int
5557buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5558{
5559 char_u *str;
5560 char_u *t;
5561
5562 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5563 return -1;
5564
5565 if (lnum > buf->b_ml.ml_line_count)
5566 lnum = buf->b_ml.ml_line_count;
5567
5568 str = ml_get_buf(buf, lnum, FALSE);
5569 if (str == NULL)
5570 return -1;
5571
5572 // Convert the character offset to a byte offset
5573 t = str;
5574 while (*t != NUL && --charidx > 0)
5575 t += mb_ptr2len(t);
5576
Bram Moolenaar91458462021-01-13 20:08:38 +01005577 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005578}
5579
5580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005582 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005585var2fpos(
5586 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005587 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005588 int *fnum, // set to fnum for '0, 'A, etc.
5589 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005591 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005593 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005596 if (varp->v_type == VAR_LIST)
5597 {
5598 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005599 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005600 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005601 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005602
5603 l = varp->vval.v_list;
5604 if (l == NULL)
5605 return NULL;
5606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005607 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005608 pos.lnum = list_find_nr(l, 0L, &error);
5609 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005610 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005611 if (charcol)
5612 len = (long)mb_charlen(ml_get(pos.lnum));
5613 else
5614 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005615
Bram Moolenaarec65d772020-08-20 22:29:12 +02005616 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005617 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005618 li = list_find(l, 1L);
5619 if (li != NULL && li->li_tv.v_type == VAR_STRING
5620 && li->li_tv.vval.v_string != NULL
5621 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005622 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005623 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005624 }
5625 else
5626 {
5627 pos.col = list_find_nr(l, 1L, &error);
5628 if (error)
5629 return NULL;
5630 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005632 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005633 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005634 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005635 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005638 pos.coladd = list_find_nr(l, 2L, &error);
5639 if (error)
5640 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005641
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005642 return &pos;
5643 }
5644
Bram Moolenaarc5809432021-03-27 21:23:30 +01005645 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5646 return NULL;
5647
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005648 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005649 if (name == NULL)
5650 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005651
5652 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005653 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005654 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005655 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005656 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005657 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005658 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005659 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005660 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005661 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005662 pos = VIsual;
5663 else
5664 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005665 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005666 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005667 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005669 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005670 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5672 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005673 pos = *pp;
5674 }
5675 if (pos.lnum != 0)
5676 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005677 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005678 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5679 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005681
Bram Moolenaara5525202006-03-02 22:52:09 +00005682 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005683
Bram Moolenaar477933c2007-07-17 14:32:23 +00005684 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005685 {
5686 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005687 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005688 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005689 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005690 // In silent Ex mode topline is zero, but that's not a valid line
5691 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005692 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005693 return &pos;
5694 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005695 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005696 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005697 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005698 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005699 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005700 return &pos;
5701 }
5702 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005703 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005705 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 pos.lnum = curbuf->b_ml.ml_line_count;
5708 pos.col = 0;
5709 }
5710 else
5711 {
5712 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005713 if (charcol)
5714 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5715 else
5716 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 return &pos;
5719 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005720 if (in_vim9script())
5721 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 return NULL;
5723}
5724
5725/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005726 * Convert list in "arg" into a position and optional file number.
5727 * When "fnump" is NULL there is no file number, only 3 items.
5728 * Note that the column is passed on as-is, the caller may want to decrement
5729 * it to use 1 for the first column.
5730 * Return FAIL when conversion is not possible, doesn't check the position for
5731 * validity.
5732 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734list2fpos(
5735 typval_T *arg,
5736 pos_T *posp,
5737 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005738 colnr_T *curswantp,
5739 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005740{
5741 list_T *l = arg->vval.v_list;
5742 long i = 0;
5743 long n;
5744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005745 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5746 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005747 if (arg->v_type != VAR_LIST
5748 || l == NULL
5749 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005750 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005751 return FAIL;
5752
5753 if (fnump != NULL)
5754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005756 if (n < 0)
5757 return FAIL;
5758 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005759 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005760 *fnump = n;
5761 }
5762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005763 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005764 if (n < 0)
5765 return FAIL;
5766 posp->lnum = n;
5767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005768 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005769 if (n < 0)
5770 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005771 // If character position is specified, then convert to byte position
5772 if (charcol)
5773 {
5774 buf_T *buf;
5775
5776 // Get the text for the specified line in a loaded buffer
5777 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5778 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5779 return FAIL;
5780
Bram Moolenaar91458462021-01-13 20:08:38 +01005781 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005782 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005783 posp->col = n;
5784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005786 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005787 posp->coladd = 0;
5788 else
5789 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005790
Bram Moolenaar493c1782014-05-28 14:34:46 +02005791 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005793
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005794 return OK;
5795}
5796
5797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 * Get the length of an environment variable name.
5799 * Advance "arg" to the first character after the name.
5800 * Return 0 for error.
5801 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005803get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 char_u *p;
5806 int len;
5807
5808 for (p = *arg; vim_isIDc(*p); ++p)
5809 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005810 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 return 0;
5812
5813 len = (int)(p - *arg);
5814 *arg = p;
5815 return len;
5816}
5817
5818/*
5819 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005820 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Return 0 if something is wrong.
5822 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005824get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 char_u *p;
5827 int len;
5828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005831 {
5832 if (*p == ':')
5833 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005834 // "s:" is start of "s:var", but "n:" is not and can be used in
5835 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005836 len = (int)(p - *arg);
5837 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5838 || len > 1)
5839 break;
5840 }
5841 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 return 0;
5844
5845 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005846 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 return len;
5849}
5850
5851/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005852 * Get the length of the name of a variable or function.
5853 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005855 * Return -1 if curly braces expansion failed.
5856 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 * If the name contains 'magic' {}'s, expand them and return the
5858 * expanded name in an allocated string via 'alias' - caller must free.
5859 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005861get_name_len(
5862 char_u **arg,
5863 char_u **alias,
5864 int evaluate,
5865 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 char_u *p;
5869 char_u *expr_start;
5870 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5875 && (*arg)[2] == (int)KE_SNR)
5876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005877 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 *arg += 3;
5879 return get_id_len(arg) + 3;
5880 }
5881 len = eval_fname_script(*arg);
5882 if (len > 0)
5883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005884 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 *arg += len;
5886 }
5887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005889 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005891 p = find_name_end(*arg, &expr_start, &expr_end,
5892 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (expr_start != NULL)
5894 {
5895 char_u *temp_string;
5896
5897 if (!evaluate)
5898 {
5899 len += (int)(p - *arg);
5900 *arg = skipwhite(p);
5901 return len;
5902 }
5903
5904 /*
5905 * Include any <SID> etc in the expanded string:
5906 * Thus the -len here.
5907 */
5908 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5909 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 *alias = temp_string;
5912 *arg = skipwhite(p);
5913 return (int)STRLEN(temp_string);
5914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005917 // Only give an error when there is something, otherwise it will be
5918 // reported at a higher level.
5919 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005920 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 return len;
5923}
5924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005925/*
5926 * Find the end of a variable or function name, taking care of magic braces.
5927 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5928 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005929 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005930 * Return a pointer to just after the name. Equal to "arg" if there is no
5931 * valid name.
5932 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005933 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934find_name_end(
5935 char_u *arg,
5936 char_u **expr_start,
5937 char_u **expr_end,
5938 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005940 int mb_nest = 0;
5941 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005943 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005944 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005948 *expr_start = NULL;
5949 *expr_end = NULL;
5950 }
5951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005952 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005953 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5954 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005955 return arg;
5956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005957 for (p = arg; *p != NUL
5958 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005959 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005960 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005961 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005963 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005965 if (*p == '\'')
5966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005967 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005968 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005969 ;
5970 if (*p == NUL)
5971 break;
5972 }
5973 else if (*p == '"')
5974 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005976 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005977 if (*p == '\\' && p[1] != NUL)
5978 ++p;
5979 if (*p == NUL)
5980 break;
5981 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005982 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005984 // "s:" is start of "s:var", but "n:" is not and can be used in
5985 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005986 len = (int)(p - arg);
5987 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005988 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005989 break;
5990 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005991
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005992 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005994 if (*p == '[')
5995 ++br_nest;
5996 else if (*p == ']')
5997 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005999
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006000 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006002 if (*p == '{')
6003 {
6004 mb_nest++;
6005 if (expr_start != NULL && *expr_start == NULL)
6006 *expr_start = p;
6007 }
6008 else if (*p == '}')
6009 {
6010 mb_nest--;
6011 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6012 *expr_end = p;
6013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 }
6016
6017 return p;
6018}
6019
6020/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006021 * Expands out the 'magic' {}'s in a variable/function name.
6022 * Note that this can call itself recursively, to deal with
6023 * constructs like foo{bar}{baz}{bam}
6024 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6025 * "in_start" ^
6026 * "expr_start" ^
6027 * "expr_end" ^
6028 * "in_end" ^
6029 *
6030 * Returns a new allocated string, which the caller must free.
6031 * Returns NULL for failure.
6032 */
6033 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034make_expanded_name(
6035 char_u *in_start,
6036 char_u *expr_start,
6037 char_u *expr_end,
6038 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006039{
6040 char_u c1;
6041 char_u *retval = NULL;
6042 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006043
6044 if (expr_end == NULL || in_end == NULL)
6045 return NULL;
6046 *expr_start = NUL;
6047 *expr_end = NUL;
6048 c1 = *in_end;
6049 *in_end = NUL;
6050
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006051 temp_result = eval_to_string(expr_start + 1, FALSE);
6052 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006053 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006054 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6055 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006056 if (retval != NULL)
6057 {
6058 STRCPY(retval, in_start);
6059 STRCAT(retval, temp_result);
6060 STRCAT(retval, expr_end + 1);
6061 }
6062 }
6063 vim_free(temp_result);
6064
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006065 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006066 *expr_start = '{';
6067 *expr_end = '}';
6068
6069 if (retval != NULL)
6070 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006071 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006072 if (expr_start != NULL)
6073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006075 temp_result = make_expanded_name(retval, expr_start,
6076 expr_end, temp_result);
6077 vim_free(retval);
6078 retval = temp_result;
6079 }
6080 }
6081
6082 return retval;
6083}
6084
6085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006087 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006089 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006092 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006093}
6094
6095/*
6096 * Return TRUE if character "c" can be used as the first character in a
6097 * variable or function name (excluding '{' and '}').
6098 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006101{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006102 return ASCII_ISALPHA(c) || c == '_';
6103}
6104
6105/*
6106 * Return TRUE if character "c" can be used as the first character of a
6107 * dictionary key.
6108 */
6109 int
6110eval_isdictc(int c)
6111{
6112 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}
6114
6115/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006116 * Handle:
6117 * - expr[expr], expr[expr:expr] subscript
6118 * - ".name" lookup
6119 * - function call with Funcref variable: func(expr)
6120 * - method call: var->method()
6121 *
6122 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006123 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006124 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126handle_subscript(
6127 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006128 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006130 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006131 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006132{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006133 int evaluate = evalarg != NULL
6134 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006135 int ret = OK;
6136 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006137 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006138 int getnext;
6139 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006140
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006141 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006142 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006143 // When at the end of the line and ".name" or "->{" or "->X" follows in
6144 // the next line then consume the line break.
6145 p = eval_next_non_blank(*arg, evalarg, &getnext);
6146 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006147 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006148 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6149 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6150 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006151 {
6152 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006153 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006154 check_white = FALSE;
6155 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006156
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006157 if (rettv->v_type == VAR_ANY)
6158 {
6159 char_u *exp_name;
6160 int cc;
6161 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006162 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006163 type_T *type;
6164
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006165 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006166 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006167 if (**arg != '.')
6168 {
6169 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006170 semsg(_(e_expected_dot_after_name_str),
6171 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006172 ret = FAIL;
6173 break;
6174 }
6175 ++*arg;
6176 if (IS_WHITE_OR_NUL(**arg))
6177 {
6178 if (verbose)
6179 emsg(_(e_no_white_space_allowed_after_dot));
6180 ret = FAIL;
6181 break;
6182 }
6183
6184 // isolate the name
6185 exp_name = *arg;
6186 while (eval_isnamec(**arg))
6187 ++*arg;
6188 cc = **arg;
6189 **arg = NUL;
6190
6191 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006192 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006193 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006194
6195 if (idx < 0 && ufunc == NULL)
6196 {
6197 ret = FAIL;
6198 break;
6199 }
6200 if (idx >= 0)
6201 {
6202 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6203 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6204
6205 copy_tv(sv->sv_tv, rettv);
6206 }
6207 else
6208 {
6209 rettv->v_type = VAR_FUNC;
6210 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6211 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006212 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006213 }
6214
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006215 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6216 || rettv->v_type == VAR_PARTIAL))
6217 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006218 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006219 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6220 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006221
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006222 // Stop the expression evaluation when immediately aborting on
6223 // error, or when an interrupt occurred or an exception was thrown
6224 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006225 if (aborting())
6226 {
6227 if (ret == OK)
6228 clear_tv(rettv);
6229 ret = FAIL;
6230 }
6231 dict_unref(selfdict);
6232 selfdict = NULL;
6233 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006234 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006235 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006236 if (in_vim9script())
6237 *arg = skipwhite(p + 2);
6238 else
6239 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006240 if (ret == OK)
6241 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006242 if (VIM_ISWHITE(**arg))
6243 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006244 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006245 ret = FAIL;
6246 }
6247 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006248 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006249 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006250 else
6251 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006252 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006253 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006254 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006255 // "." is ".name" lookup when we found a dict or when evaluating and
6256 // scriptversion is at least 2, where string concatenation is "..".
6257 else if (**arg == '['
6258 || (**arg == '.' && (rettv->v_type == VAR_DICT
6259 || (!evaluate
6260 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006261 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006262 {
6263 dict_unref(selfdict);
6264 if (rettv->v_type == VAR_DICT)
6265 {
6266 selfdict = rettv->vval.v_dict;
6267 if (selfdict != NULL)
6268 ++selfdict->dv_refcount;
6269 }
6270 else
6271 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006272 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006273 {
6274 clear_tv(rettv);
6275 ret = FAIL;
6276 }
6277 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006278 else
6279 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006280 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006282 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6283 // Don't do this when "Func" is already a partial that was bound
6284 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006285 if (selfdict != NULL
6286 && (rettv->v_type == VAR_FUNC
6287 || (rettv->v_type == VAR_PARTIAL
6288 && (rettv->vval.v_partial->pt_auto
6289 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006291
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006292 dict_unref(selfdict);
6293 return ret;
6294}
6295
6296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297 * Make a copy of an item.
6298 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006299 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006300 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6301 * reference to an already copied list/dict can be used.
6302 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006305item_copy(
6306 typval_T *from,
6307 typval_T *to,
6308 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006309 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006311{
6312 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006313 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006314
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006316 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006317 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006319 }
6320 ++recurse;
6321
6322 switch (from->v_type)
6323 {
6324 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006325 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326 case VAR_STRING:
6327 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006328 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006329 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006330 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006331 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006332 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006333 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006334 copy_tv(from, to);
6335 break;
6336 case VAR_LIST:
6337 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006338 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006339 if (from->vval.v_list == NULL)
6340 to->vval.v_list = NULL;
6341 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006343 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006344 to->vval.v_list = from->vval.v_list->lv_copylist;
6345 ++to->vval.v_list->lv_refcount;
6346 }
6347 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006348 to->vval.v_list = list_copy(from->vval.v_list,
6349 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 if (to->vval.v_list == NULL)
6351 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006353 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006355 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 case VAR_DICT:
6357 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006358 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006359 if (from->vval.v_dict == NULL)
6360 to->vval.v_dict = NULL;
6361 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006363 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006364 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6365 ++to->vval.v_dict->dv_refcount;
6366 }
6367 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006368 to->vval.v_dict = dict_copy(from->vval.v_dict,
6369 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370 if (to->vval.v_dict == NULL)
6371 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006373 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006374 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006376 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006378 }
6379 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381}
6382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 void
6384echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6385{
6386 char_u *tofree;
6387 char_u numbuf[NUMBUFLEN];
6388 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6389
6390 if (*atstart)
6391 {
6392 *atstart = FALSE;
6393 // Call msg_start() after eval1(), evaluating the expression
6394 // may cause a message to appear.
6395 if (with_space)
6396 {
6397 // Mark the saved text as finishing the line, so that what
6398 // follows is displayed on a new line when scrolling back
6399 // at the more prompt.
6400 msg_sb_eol();
6401 msg_start();
6402 }
6403 }
6404 else if (with_space)
6405 msg_puts_attr(" ", echo_attr);
6406
6407 if (p != NULL)
6408 for ( ; *p != NUL && !got_int; ++p)
6409 {
6410 if (*p == '\n' || *p == '\r' || *p == TAB)
6411 {
6412 if (*p != TAB && *needclr)
6413 {
6414 // remove any text still there from the command
6415 msg_clr_eos();
6416 *needclr = FALSE;
6417 }
6418 msg_putchar_attr(*p, echo_attr);
6419 }
6420 else
6421 {
6422 if (has_mbyte)
6423 {
6424 int i = (*mb_ptr2len)(p);
6425
6426 (void)msg_outtrans_len_attr(p, i, echo_attr);
6427 p += i - 1;
6428 }
6429 else
6430 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6431 }
6432 }
6433 vim_free(tofree);
6434}
6435
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 * ":echo expr1 ..." print each argument separated with a space, add a
6438 * newline at the end.
6439 * ":echon expr1 ..." print each argument plain.
6440 */
6441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006446 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 int needclr = TRUE;
6448 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006449 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006450 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006451 evalarg_T evalarg;
6452
Bram Moolenaare707c882020-07-01 13:07:37 +02006453 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 if (eap->skip)
6456 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006457 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006459 // If eval1() causes an error message the text from the command may
6460 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006461 need_clr_eos = needclr;
6462
Bram Moolenaar68db9962021-05-09 23:19:22 +02006463 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006464 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
6466 /*
6467 * Report the invalid expression unless the expression evaluation
6468 * has been cancelled due to an aborting error, an interrupt, or an
6469 * exception.
6470 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006471 if (!aborting() && did_emsg == did_emsg_before
6472 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006473 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006474 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 break;
6476 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006477 need_clr_eos = FALSE;
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006480 {
6481 if (rettv.v_type == VAR_VOID)
6482 {
6483 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6484 break;
6485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006486 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006487 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 arg = skipwhite(arg);
6491 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006492 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006493 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 if (eap->skip)
6496 --emsg_skip;
6497 else
6498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006499 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 if (needclr)
6501 msg_clr_eos();
6502 if (eap->cmdidx == CMD_echo)
6503 msg_end();
6504 }
6505}
6506
6507/*
6508 * ":echohl {name}".
6509 */
6510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006513 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514}
6515
6516/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006517 * Returns the :echo attribute
6518 */
6519 int
6520get_echo_attr(void)
6521{
6522 return echo_attr;
6523}
6524
6525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 * ":execute expr1 ..." execute the result of an expression.
6527 * ":echomsg expr1 ..." Print a message
6528 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006529 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 * Each gets spaces around each argument and a newline at the end for
6531 * echo commands
6532 */
6533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 int ret = OK;
6539 char_u *p;
6540 garray_T ga;
6541 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006542 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544 ga_init2(&ga, 1, 80);
6545
6546 if (eap->skip)
6547 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006548 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006550 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006551 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
6554 if (!eap->skip)
6555 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006556 char_u buf[NUMBUFLEN];
6557
6558 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006559 {
6560 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6561 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006562 semsg(_(e_using_invalid_value_as_string_str),
6563 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006564 p = NULL;
6565 }
6566 else
6567 p = tv_get_string_buf(&rettv, buf);
6568 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006569 else
6570 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006571 if (p == NULL)
6572 {
6573 clear_tv(&rettv);
6574 ret = FAIL;
6575 break;
6576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 len = (int)STRLEN(p);
6578 if (ga_grow(&ga, len + 2) == FAIL)
6579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 ret = FAIL;
6582 break;
6583 }
6584 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 ga.ga_len += len;
6588 }
6589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006590 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 arg = skipwhite(arg);
6592 }
6593
6594 if (ret != FAIL && ga.ga_data != NULL)
6595 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006596 // use the first line of continuation lines for messages
6597 SOURCING_LNUM = start_lnum;
6598
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006599 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6600 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006601 // Mark the already saved text as finishing the line, so that what
6602 // follows is displayed on a new line when scrolling back at the
6603 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006604 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006605 }
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006608 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006609 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006610 out_flush();
6611 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006612 else if (eap->cmdidx == CMD_echoconsole)
6613 {
6614 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6615 ui_write((char_u *)"\r\n", 2, TRUE);
6616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 else if (eap->cmdidx == CMD_echoerr)
6618 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006619 int save_did_emsg = did_emsg;
6620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006621 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006622 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if (!force_abort)
6624 did_emsg = save_did_emsg;
6625 }
6626 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006627 {
6628 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6629
6630 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6631 sticky_cmdmod_flags = cmdmod.cmod_flags
6632 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 do_cmdline((char_u *)ga.ga_data,
6634 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006635 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
6638
6639 ga_clear(&ga);
6640
6641 if (eap->skip)
6642 --emsg_skip;
6643
Bram Moolenaar63b91732021-08-05 20:40:03 +02006644 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
6647/*
6648 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6649 * "arg" points to the "&" or '+' when called, to "option" when returning.
6650 * Returns NULL when no option name found. Otherwise pointer to the char
6651 * after the option name.
6652 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006653 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006654find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 char_u *p = *arg;
6657
6658 ++p;
6659 if (*p == 'g' && p[1] == ':')
6660 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006661 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 p += 2;
6663 }
6664 else if (*p == 'l' && p[1] == ':')
6665 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006666 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 p += 2;
6668 }
6669 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006670 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 if (!ASCII_ISALPHA(*p))
6673 return NULL;
6674 *arg = p;
6675
6676 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006677 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 else
6679 while (ASCII_ISALPHA(*p))
6680 ++p;
6681 return p;
6682}
6683
6684/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006685 * Display script name where an item was last set.
6686 * Should only be invoked when 'verbose' is non-zero.
6687 */
6688 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006689last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006690{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006691 char_u *p;
6692
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006693 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006694 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006695 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006696 if (p != NULL)
6697 {
6698 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006699 msg_puts(_("\n\tLast set from "));
6700 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006701 if (script_ctx.sc_lnum > 0)
6702 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006703 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006704 msg_outnum((long)script_ctx.sc_lnum);
6705 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006706 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006707 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006708 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006709 }
6710}
6711
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006712#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714/*
6715 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006716 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 * "flags" can be "g" to do a global substitute.
6718 * Returns an allocated string, NULL for error.
6719 */
6720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006721do_string_sub(
6722 char_u *str,
6723 char_u *pat,
6724 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006725 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 int sublen;
6729 regmatch_T regmatch;
6730 int i;
6731 int do_all;
6732 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006733 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 garray_T ga;
6735 char_u *ret;
6736 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006737 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006739 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006741 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 ga_init2(&ga, 1, 200);
6744
6745 do_all = (flags[0] == 'g');
6746
6747 regmatch.rm_ic = p_ic;
6748 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6749 if (regmatch.regprog != NULL)
6750 {
6751 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006752 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006755 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006756 if (regmatch.startp[0] == regmatch.endp[0])
6757 {
6758 if (zero_width == regmatch.startp[0])
6759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006760 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006761 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006762 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6763 (size_t)i);
6764 ga.ga_len += i;
6765 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006766 continue;
6767 }
6768 zero_width = regmatch.startp[0];
6769 }
6770
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 /*
6772 * Get some space for a temporary buffer to do the substitution
6773 * into. It will contain:
6774 * - The text up to where the match is.
6775 * - The substituted text.
6776 * - The text after the match.
6777 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006778 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006779 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6781 {
6782 ga_clear(&ga);
6783 break;
6784 }
6785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006786 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 i = (int)(regmatch.startp[0] - tail);
6788 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006789 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006790 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 + ga.ga_len + i, TRUE, TRUE, FALSE);
6792 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006793 tail = regmatch.endp[0];
6794 if (*tail == NUL)
6795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (!do_all)
6797 break;
6798 }
6799
6800 if (ga.ga_data != NULL)
6801 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6802
Bram Moolenaar473de612013-06-08 18:19:48 +02006803 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 }
6805
6806 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6807 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006808 if (p_cpo == empty_option)
6809 p_cpo = save_cpo;
6810 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006812 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006813 // If it's still empty it was changed and restored, need to restore in
6814 // the complicated way.
6815 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006816 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006817 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 return ret;
6821}