blob: 5f9c3423d10cdc3103a809b454f684916116f525 [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);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010052static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020053static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010054static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
56static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static 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 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000115}
116
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117#if defined(EXITFREE) || defined(PROTO)
118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100119eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000120{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200121 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100122 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200123 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200125 // autoloaded script names
126 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000127
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200128 // unreferenced lists and dicts
129 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200130
131 // functions not garbage collected
132 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000133}
134#endif
135
Bram Moolenaare6b53242020-07-01 17:28:33 +0200136 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
138{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100139 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200141 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200142 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200143 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100144 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200145 {
146 evalarg->eval_getline = eap->getline;
147 evalarg->eval_cookie = eap->cookie;
148 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 }
150}
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Top level evaluation function, returning a boolean.
154 * Sets "error" to TRUE if there was an error.
155 * Return TRUE or FALSE.
156 */
157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100158eval_to_bool(
159 char_u *arg,
160 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200161 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100162 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaar33570922005-01-25 22:26:29 +0000164 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200165 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200166 evalarg_T evalarg;
167
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200179 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200180 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200181 else
182 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
185 }
186 if (skip)
187 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200197eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200205 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
206
207 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 if (ret == FAIL)
209 {
210 // Report the invalid expression unless the expression evaluation has
211 // been cancelled due to an aborting error, an interrupt, or an
212 // exception, or we already gave a more specific error.
213 // Also check called_emsg for when using assert_fails().
214 if (!aborting() && did_emsg == did_emsg_before
215 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200216 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100219 return ret;
220}
221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200223 * Return whether a typval is a valid expression to pass to eval_expr_typval()
224 * or eval_expr_to_bool(). An empty string returns FALSE;
225 */
226 int
227eval_expr_valid_arg(typval_T *tv)
228{
229 return tv->v_type != VAR_UNKNOWN
230 && (tv->v_type != VAR_STRING
231 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
232}
233
234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 * Evaluate an expression, which can be a function, partial or string.
236 * Pass arguments "argv[argc]".
237 * Return the result in "rettv" and OK or FAIL.
238 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200239 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100240eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
241{
242 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100243 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100245
246 if (expr->v_type == VAR_FUNC)
247 {
248 s = expr->vval.v_string;
249 if (s == NULL || *s == NUL)
250 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200251 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000252 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else if (expr->v_type == VAR_PARTIAL)
257 {
258 partial_T *partial = expr->vval.v_partial;
259
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200260 if (partial == NULL)
261 return FAIL;
262
Bram Moolenaar822ba242020-05-24 23:00:18 +0200263 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200266 if (call_def_function(partial->pt_func, argc, argv,
267 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 return FAIL;
269 }
270 else
271 {
272 s = partial_name(partial);
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
277 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
279 return FAIL;
280 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 else if (expr->v_type == VAR_INSTR)
283 {
284 return exe_typval_instr(expr, rettv);
285 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 else
287 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000288 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 if (s == NULL)
290 return FAIL;
291 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200292 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200294 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200296 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200297 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 return FAIL;
299 }
300 }
301 return OK;
302}
303
304/*
305 * Like eval_to_bool() but using a typval_T instead of a string.
306 * Works for string, funcref and partial.
307 */
308 int
309eval_expr_to_bool(typval_T *expr, int *error)
310{
311 typval_T rettv;
312 int res;
313
314 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
315 {
316 *error = TRUE;
317 return FALSE;
318 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200319 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 clear_tv(&rettv);
321 return res;
322}
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/*
325 * Top level evaluation function, returning a string. If "skip" is TRUE,
326 * only parsing to "nextcmd" is done, without reporting errors. Return
327 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
328 */
329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_to_string_skip(
331 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200332 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100333 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200337 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar37c83712020-06-30 21:18:36 +0200339 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (skip)
341 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200342 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 retval = NULL;
344 else
345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100346 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349 if (skip)
350 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 return retval;
354}
355
356/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000357 * Skip over an expression at "*pp".
358 * Return FAIL for an error, OK otherwise.
359 */
360 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200361skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000362{
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000364
365 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200366 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367}
368
369/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200370 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200371 * If in Vim9 script and line breaks are encountered, the lines are
372 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200373 * "arg" is advanced to just after the expression.
374 * "start" is set to the start of the expression, "end" to just after the end.
375 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200376 * Return FAIL for an error, OK otherwise.
377 */
378 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379skip_expr_concatenate(
380 char_u **arg,
381 char_u **start,
382 char_u **end,
383 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200384{
385 typval_T rettv;
386 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200387 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200388 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200389 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200391 int evaluate = evalarg == NULL
392 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200394 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200395 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 {
397 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200398 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200401 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200403 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404
405 // Don't evaluate the expression.
406 if (evalarg != NULL)
407 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200408 *arg = skipwhite(*arg);
409 res = eval1(arg, &rettv, evalarg);
410 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200411 if (evalarg != NULL)
412 evalarg->eval_flags = save_flags;
413
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200414 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200415 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200416 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 if (evalarg->eval_ga.ga_len == 1)
418 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200419 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 ga_clear(gap);
421 gap->ga_itemsize = 0;
422 }
423 else
424 {
425 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200426 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200428 // Line breaks encountered, concatenate all the lines.
429 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200430 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200431
432 // free the lines only when using getsourceline()
433 if (evalarg->eval_cookie != NULL)
434 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200435 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200437 // Do not free the last line, "arg" points into it, free it
438 // later.
439 vim_free(evalarg->eval_tofree);
440 evalarg->eval_tofree =
441 ((char_u **)gap->ga_data)[gap->ga_len - 1];
442 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 ga_clear_strings(gap);
444 }
445 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200446 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200448
449 // free lines that were explicitly marked for freeing
450 ga_clear_strings(freegap);
451 }
452
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 gap->ga_itemsize = 0;
454 if (p == NULL)
455 return FAIL;
456 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200457 vim_free(evalarg->eval_tofree_lambda);
458 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 // Compute "end" relative to the end.
460 *end = *start + STRLEN(*start) - endoff;
461 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 }
463
464 return res;
465}
466
467/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100468 * Convert "tv" to a string.
469 * When "convert" is TRUE convert a List into a sequence of lines and convert
470 * a Float to a String.
471 * Returns an allocated string (NULL when out of memory).
472 */
473 char_u *
474typval2string(typval_T *tv, int convert)
475{
476 garray_T ga;
477 char_u *retval;
478#ifdef FEAT_FLOAT
479 char_u numbuf[NUMBUFLEN];
480#endif
481
482 if (convert && tv->v_type == VAR_LIST)
483 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000484 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100485 if (tv->vval.v_list != NULL)
486 {
487 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
488 if (tv->vval.v_list->lv_len > 0)
489 ga_append(&ga, NL);
490 }
491 ga_append(&ga, NUL);
492 retval = (char_u *)ga.ga_data;
493 }
494#ifdef FEAT_FLOAT
495 else if (convert && tv->v_type == VAR_FLOAT)
496 {
497 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
498 retval = vim_strsave(numbuf);
499 }
500#endif
501 else
502 retval = vim_strsave(tv_get_string(tv));
503 return retval;
504}
505
506/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200507 * Top level evaluation function, returning a string. Does not handle line
508 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000509 * When "convert" is TRUE convert a List into a sequence of lines and convert
510 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 * Return pointer to allocated memory, or NULL for failure.
512 */
513 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100514eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100515 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100516 int convert,
517 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
Bram Moolenaar33570922005-01-25 22:26:29 +0000519 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100521 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
524 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 retval = NULL;
526 else
527 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100528 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000529 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
533 return retval;
534}
535
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 char_u *
537eval_to_string(
538 char_u *arg,
539 int convert)
540{
541 return eval_to_string_eap(arg, convert, NULL);
542}
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000545 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100546 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200547 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100550eval_to_string_safe(
551 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000552 int use_sandbox,
553 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200556 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200557 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200558 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar9530b582022-01-22 13:39:08 +0000560 if (!keep_script_version)
561 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000563 if (use_sandbox)
564 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100565 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200566 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200567 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100570 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200571 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return retval;
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/*
578 * Top level evaluation function, returning a number.
579 * Evaluates "expr" silently.
580 * Returns -1 for an error.
581 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200582 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000587 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 ++emsg_off;
590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200591 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 retval = -1;
593 else
594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100595 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598 --emsg_off;
599
600 return retval;
601}
602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000604 * Top level evaluation function.
605 * Returns an allocated typval_T with the result.
606 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607 */
608 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200609eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000610{
611 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200612 evalarg_T evalarg;
613
614 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200616 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200617 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100618 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaar37c83712020-06-30 21:18:36 +0200620 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000621 return tv;
622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000625 * "*arg" points to what can be a function name in the form of "import.Name" or
626 * "Funcref". Return the name of the function. Set "tofree" to something that
627 * was allocated.
628 * If "verbose" is FALSE no errors are given.
629 * Return NULL for any failure.
630 */
631 static char_u *
632deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000633 char_u **arg,
634 char_u **tofree,
635 evalarg_T *evalarg,
636 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000637{
638 typval_T ref;
639 char_u *name = *arg;
640
641 ref.v_type = VAR_UNKNOWN;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100642 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100643 {
644 dictitem_T *v;
645
646 // If <SID>VarName was used it would not be found, try another way.
647 v = find_var_also_in_script(name, NULL, FALSE);
648 if (v == NULL)
649 return NULL;
650 copy_tv(&v->di_tv, &ref);
651 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000652 if (*skipwhite(*arg) != NUL)
653 {
654 if (verbose)
655 semsg(_(e_trailing_characters_str), *arg);
656 name = NULL;
657 }
658 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
659 {
660 name = ref.vval.v_string;
661 ref.vval.v_string = NULL;
662 *tofree = name;
663 }
664 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
665 {
666 if (ref.vval.v_partial->pt_argc > 0
667 || ref.vval.v_partial->pt_dict != NULL)
668 {
669 if (verbose)
670 emsg(_(e_cannot_use_partial_here));
671 name = NULL;
672 }
673 else
674 {
675 name = vim_strsave(partial_name(ref.vval.v_partial));
676 *tofree = name;
677 }
678 }
679 else
680 {
681 if (verbose)
682 semsg(_(e_not_callable_type_str), name);
683 name = NULL;
684 }
685 clear_tv(&ref);
686 return name;
687}
688
689/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100690 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200691 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
692 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100696call_vim_function(
697 char_u *func,
698 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200699 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200700 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200703 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000704 char_u *arg;
705 char_u *name;
706 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000707 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100709 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200710 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000711 funcexe.fe_firstline = curwin->w_cursor.lnum;
712 funcexe.fe_lastline = curwin->w_cursor.lnum;
713 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000714
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000715 // The name might be "import.Func" or "Funcref". We don't know, we need to
716 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000717 // autoload script has errors. Guess that when there is a dot in the name
718 // showing errors is the right choice.
719 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000720 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000721 if (ignore_errors)
722 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000723 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000724 if (ignore_errors)
725 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000726 if (name == NULL)
727 name = func;
728
729 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
730
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 if (ret == FAIL)
732 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000733 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000734
735 return ret;
736}
737
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100738/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100739 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000740 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
741 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000742 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743 */
744 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745call_func_retstr(
746 char_u *func,
747 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200748 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749{
750 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000751 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000752
Bram Moolenaarded27a12018-08-01 19:06:03 +0200753 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 return NULL;
755
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100756 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 return retval;
759}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000760
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000761/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100762 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000763 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000764 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000765 */
766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767call_func_retlist(
768 char_u *func,
769 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200770 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000771{
772 typval_T rettv;
773
Bram Moolenaarded27a12018-08-01 19:06:03 +0200774 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000775 return NULL;
776
777 if (rettv.v_type != VAR_LIST)
778 {
779 clear_tv(&rettv);
780 return NULL;
781 }
782
783 return rettv.vval.v_list;
784}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaare70dd112022-01-21 16:31:11 +0000786#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100788 * Evaluate "arg", which is 'foldexpr'.
789 * Note: caller must set "curwin" to match "arg".
790 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
791 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 */
793 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000794eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000796 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200798 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000800 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000801 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
802 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaare70dd112022-01-21 16:31:11 +0000804 arg = wp->w_p_fde;
805 current_sctx = wp->w_p_script_ctx[WV_FDE];
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000808 if (use_sandbox)
809 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100810 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200812 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 retval = 0;
814 else
815 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 if (tv.v_type == VAR_NUMBER)
818 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000819 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 retval = 0;
821 else
822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100823 // If the result is a string, check if there is a non-digit before
824 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (!VIM_ISDIGIT(*s) && *s != '-')
827 *cp = *s++;
828 retval = atol((char *)s);
829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000833 if (use_sandbox)
834 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100835 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200836 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000837 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200839 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841#endif
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 * Get an lval: variable, Dict item or List item that can be assigned a value
845 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
846 * "name.key", "name.key[expr]" etc.
847 * Indexing only works if "name" is an existing List or Dictionary.
848 * "name" points to the start of the name.
849 * If "rettv" is not NULL it points to the value to be assigned.
850 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
851 * wrong; must end in space or cmd separator.
852 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100853 * flags:
854 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100855 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100856 * GLV_NO_AUTOLOAD: do not use script autoloading
857 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000859 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100863get_lval(
864 char_u *name,
865 typval_T *rettv,
866 lval_T *lp,
867 int unlet,
868 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100869 int flags, // GLV_ values
870 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 char_u *expr_start, *expr_end;
874 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 dictitem_T *v;
876 typval_T var1;
877 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000880 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100881 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100882 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100883 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000884 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200887 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100889 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100891 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200893 lp->ll_name_end = find_name_end(name, NULL, NULL,
894 FNE_INCL_BR | fne_flags);
895 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 }
897
Bram Moolenaara749a422022-02-12 19:52:25 +0000898 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000899 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000900 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
901 {
902 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
903 return NULL;
904 }
905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000907 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (expr_start != NULL)
910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100912 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 && *p != '[' && *p != '.')
914 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000915 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 return NULL;
917 }
918
919 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
920 if (lp->ll_exp_name == NULL)
921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100922 // Report an invalid expression in braces, unless the
923 // expression evaluation has been cancelled due to an
924 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 if (!aborting() && !quiet)
926 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000927 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000928 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 return NULL;
930 }
931 }
932 lp->ll_name = lp->ll_exp_name;
933 }
934 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 lp->ll_name = name;
937
Bram Moolenaar4525a572022-02-13 11:57:33 +0000938 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200940 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000941 // However, "g:[key]" is indexing a dictionary.
942 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200943 {
944 --p;
945 lp->ll_name_end = p;
946 }
947 if (*p == ':')
948 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000949 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200950
951 if (tp == p + 1 && !quiet)
952 {
953 semsg(_(e_white_space_required_after_str_str), ":", p);
954 return NULL;
955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000957 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000958 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000959 semsg(_(e_using_type_not_in_script_context_str), p);
960 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000961 }
962
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200963 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +0000964 lp->ll_type = parse_type(&tp,
965 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
966 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100967 if (lp->ll_type == NULL && !quiet)
968 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200969 lp->ll_name_end = tp;
970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 }
972 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000973 if (lp->ll_name == NULL)
974 return p;
975
Bram Moolenaar62aec932022-01-29 21:45:34 +0000976 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000977 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000978 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000979
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000980 if (import != NULL)
981 {
982 ufunc_T *ufunc;
983 type_T *type;
984
985 lp->ll_sid = import->imp_sid;
986 lp->ll_name = skipwhite(p + 1);
987 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
988 lp->ll_name_end = p;
989
990 // check the item is exported
991 cc = *p;
992 *p = NUL;
993 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000994 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000995 {
996 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000997 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000998 }
999 *p = cc;
1000 }
1001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001004 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return p;
1006
Bram Moolenaar4525a572022-02-13 11:57:33 +00001007 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001008 {
1009 // using local variable
1010 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001011 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001012 }
1013 else
1014 {
1015 cc = *p;
1016 *p = NUL;
1017 // When we would write to the variable pass &ht and prevent autoload.
1018 writing = !(flags & GLV_READ_ONLY);
1019 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001020 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001021 if (v == NULL && !quiet)
1022 semsg(_(e_undefined_variable_str), lp->ll_name);
1023 *p = cc;
1024 if (v == NULL)
1025 return NULL;
1026 lp->ll_tv = &v->di_tv;
1027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001028
Bram Moolenaar4525a572022-02-13 11:57:33 +00001029 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001030 {
1031 if (!quiet)
1032 semsg(_(e_variable_already_declared), lp->ll_name);
1033 return NULL;
1034 }
1035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 /*
1037 * Loop until no more [idx] or .key is following.
1038 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 var1.v_type = VAR_UNKNOWN;
1040 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001041 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001043 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1044 {
1045 if (!quiet)
1046 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1047 return NULL;
1048 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001049 if (lp->ll_tv->v_type != VAR_LIST
1050 && lp->ll_tv->v_type != VAR_DICT
1051 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001054 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001056 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001057
1058 // a NULL list/blob works like an empty list/blob, allocate one now.
1059 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1060 rettv_list_alloc(lp->ll_tv);
1061 else if (lp->ll_tv->v_type == VAR_BLOB
1062 && lp->ll_tv->vval.v_blob == NULL)
1063 rettv_blob_alloc(lp->ll_tv);
1064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001068 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001070 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001071
Bram Moolenaar4525a572022-02-13 11:57:33 +00001072 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001073 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001074 && lp->ll_tv == &v->di_tv
1075 && ht != NULL && ht == get_script_local_ht())
1076 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001077 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001078
1079 // Vim9 script local variable: get the type
1080 if (sv != NULL)
1081 lp->ll_valtype = sv->sv_type;
1082 }
1083
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 len = -1;
1085 if (*p == '.')
1086 {
1087 key = p + 1;
1088 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1089 ;
1090 if (len == 0)
1091 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001092 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001093 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 }
1096 p = key + len;
1097 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001098 else
1099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001100 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001101 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 if (*p == ':')
1103 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001104 else
1105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001107 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001109 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001112 clear_tv(&var1);
1113 return NULL;
1114 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001115 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 }
1117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001118 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 if (*p == ':')
1120 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001124 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001127 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001128 if (rettv != NULL
1129 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001130 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001132 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001135 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001136 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 }
1139 p = skipwhite(p + 1);
1140 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 else
1143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001145 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001146 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001148 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001151 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001154 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001155 clear_tv(&var2);
1156 return NULL;
1157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001163
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001167 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001168 clear_tv(&var1);
1169 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 }
1172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001173 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 ++p;
1175 }
1176
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 {
1179 if (len == -1)
1180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001181 // "[key]": get key from "var1"
1182 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001183 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 }
1188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001189 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001190
1191 // a NULL dict is equivalent with an empty dict
1192 if (lp->ll_tv->vval.v_dict == NULL)
1193 {
1194 lp->ll_tv->vval.v_dict = dict_alloc();
1195 if (lp->ll_tv->vval.v_dict == NULL)
1196 {
1197 clear_tv(&var1);
1198 return NULL;
1199 }
1200 ++lp->ll_tv->vval.v_dict->dv_refcount;
1201 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001202 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001203
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001204 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001206 // When assigning to a scope dictionary check that a function and
1207 // variable name is valid (only variable name unless it is l: or
1208 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001209 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001210 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001211 int prevval;
1212 int wrong;
1213
1214 if (len != -1)
1215 {
1216 prevval = key[len];
1217 key[len] = NUL;
1218 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001219 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001220 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001221 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1222 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001223 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001224 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001225 if (len != -1)
1226 key[len] = prevval;
1227 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001228 {
1229 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001230 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001231 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001232 }
1233
Bram Moolenaar10c65862020-10-08 21:16:42 +02001234 if (lp->ll_valtype != NULL)
1235 // use the type of the member
1236 lp->ll_valtype = lp->ll_valtype->tt_member;
1237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001240 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001241 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001242 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001243 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001244 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001245 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001246 return NULL;
1247 }
1248
Bram Moolenaar31b81602019-02-10 22:14:27 +01001249 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001253 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001254 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 }
1257 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001261 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 p = NULL;
1264 break;
1265 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001267 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001268 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1269 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001270 {
1271 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001272 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001273 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001274
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001275 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 else if (lp->ll_tv->v_type == VAR_BLOB)
1279 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001280 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1281
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 /*
1283 * Get the number and item for the only or first index of the List.
1284 */
1285 if (empty1)
1286 lp->ll_n1 = 0;
1287 else
1288 // is number or string
1289 lp->ll_n1 = (long)tv_get_number(&var1);
1290 clear_tv(&var1);
1291
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001292 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 return NULL;
1296 }
1297 if (lp->ll_range && !lp->ll_empty2)
1298 {
1299 lp->ll_n2 = (long)tv_get_number(&var2);
1300 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001301 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1302 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 lp->ll_blob = lp->ll_tv->vval.v_blob;
1306 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001307 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 else
1310 {
1311 /*
1312 * Get the number and item for the only or first index of the List.
1313 */
1314 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001316 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001319 clear_tv(&var1);
1320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001323 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1324 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001325 if (lp->ll_li == NULL)
1326 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001327 clear_tv(&var2);
1328 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 }
1330
Bram Moolenaar10c65862020-10-08 21:16:42 +02001331 if (lp->ll_valtype != NULL)
1332 // use the type of the member
1333 lp->ll_valtype = lp->ll_valtype->tt_member;
1334
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 /*
1336 * May need to find the item or absolute index for the second
1337 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 * When no index given: "lp->ll_empty2" is TRUE.
1339 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001343 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001346 if (check_range_index_two(lp->ll_list,
1347 &lp->ll_n1, lp->ll_li,
1348 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001350 }
1351
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 }
1355
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001356 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 return p;
1359}
1360
1361/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001362 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001365clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366{
1367 vim_free(lp->ll_exp_name);
1368 vim_free(lp->ll_newkey);
1369}
1370
1371/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001372 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001374 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1375 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378set_var_lval(
1379 lval_T *lp,
1380 char_u *endp,
1381 typval_T *rettv,
1382 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001383 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1384 char_u *op,
1385 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386{
1387 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001388 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389
1390 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001392 cc = *endp;
1393 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001394 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1395 return;
1396
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001397 if (lp->ll_blob != NULL)
1398 {
1399 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001401 if (op != NULL && *op != '=')
1402 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001403 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001404 return;
1405 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001406 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001407 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408
1409 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1410 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001411 if (lp->ll_empty2)
1412 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1413
Bram Moolenaar68452172021-04-12 21:21:02 +02001414 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1415 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001416 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001417 }
1418 else
1419 {
1420 val = (int)tv_get_number_chk(rettv, &error);
1421 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001422 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001423 }
1424 }
1425 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001428
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001429 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1430 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001431 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001432 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 *endp = cc;
1434 return;
1435 }
1436
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001438 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001439 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001440 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 {
1442 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001443 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1444 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001445 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001446 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001447 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001448 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001451 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001452 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001453 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1454 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001455 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001456 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001457 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001458 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001459 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001461 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001462 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001463 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001464 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 else if (lp->ll_range)
1466 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001467 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1468 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001469 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001470 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001471 return;
1472 }
1473
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001474 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1475 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 }
1477 else
1478 {
1479 /*
1480 * Assign to a List or Dictionary item.
1481 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001482 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1483 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001484 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001485 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001486 return;
1487 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001488
1489 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001490 && check_typval_arg_type(lp->ll_valtype, rettv,
1491 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001492 return;
1493
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 if (lp->ll_newkey != NULL)
1495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 if (op != NULL && *op != '=')
1497 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001498 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 return;
1500 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001501 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1502 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001503 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001505 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001506 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 if (di == NULL)
1508 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001509 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1510 {
1511 vim_free(di);
1512 return;
1513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 else if (op != NULL && *op != '=')
1517 {
1518 tv_op(lp->ll_tv, rettv, op);
1519 return;
1520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001522 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 /*
1525 * Assign the value to the variable or list item.
1526 */
1527 if (copy)
1528 copy_tv(rettv, lp->ll_tv);
1529 else
1530 {
1531 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001532 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 }
1535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536}
1537
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001539 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1540 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 * Returns OK or FAIL.
1542 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001546 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 char_u numbuf[NUMBUFLEN];
1548 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001549 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001551 // Can't do anything with a Funcref or Dict on the right.
1552 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001553 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001554 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1555 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 {
1557 switch (tv1->v_type)
1558 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001559 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001560 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 case VAR_DICT:
1563 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001564 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001565 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001566 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001567 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001568 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001569 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 break;
1571
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001572 case VAR_BLOB:
1573 if (*op != '+' || tv2->v_type != VAR_BLOB)
1574 break;
1575 // BLOB += BLOB
1576 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1577 {
1578 blob_T *b1 = tv1->vval.v_blob;
1579 blob_T *b2 = tv2->vval.v_blob;
1580 int i, len = blob_len(b2);
1581 for (i = 0; i < len; i++)
1582 ga_append(&b1->bv_ga, blob_get(b2, i));
1583 }
1584 return OK;
1585
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 case VAR_LIST:
1587 if (*op != '+' || tv2->v_type != VAR_LIST)
1588 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001590 if (tv2->vval.v_list != NULL)
1591 {
1592 if (tv1->vval.v_list == NULL)
1593 {
1594 tv1->vval.v_list = tv2->vval.v_list;
1595 ++tv1->vval.v_list->lv_refcount;
1596 }
1597 else
1598 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1599 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 return OK;
1601
1602 case VAR_NUMBER:
1603 case VAR_STRING:
1604 if (tv2->v_type == VAR_LIST)
1605 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610#ifdef FEAT_FLOAT
1611 if (tv2->v_type == VAR_FLOAT)
1612 {
1613 float_T f = n;
1614
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 if (*op == '%')
1616 break;
1617 switch (*op)
1618 {
1619 case '+': f += tv2->vval.v_float; break;
1620 case '-': f -= tv2->vval.v_float; break;
1621 case '*': f *= tv2->vval.v_float; break;
1622 case '/': f /= tv2->vval.v_float; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 clear_tv(tv1);
1625 tv1->v_type = VAR_FLOAT;
1626 tv1->vval.v_float = f;
1627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629#endif
1630 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001631 switch (*op)
1632 {
1633 case '+': n += tv_get_number(tv2); break;
1634 case '-': n -= tv_get_number(tv2); break;
1635 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001636 case '/': n = num_divide(n, tv_get_number(tv2),
1637 &failed); break;
1638 case '%': n = num_modulus(n, tv_get_number(tv2),
1639 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 clear_tv(tv1);
1642 tv1->v_type = VAR_NUMBER;
1643 tv1->vval.v_number = n;
1644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 }
1646 else
1647 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648 if (tv2->v_type == VAR_FLOAT)
1649 break;
1650
Bram Moolenaarff697e62019-02-12 22:28:33 +01001651 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001652 s = tv_get_string(tv1);
1653 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 clear_tv(tv1);
1655 tv1->v_type = VAR_STRING;
1656 tv1->vval.v_string = s;
1657 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001658 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001660 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001661#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001662 {
1663 float_T f;
1664
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 if (*op == '%' || *op == '.'
1666 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001667 && tv2->v_type != VAR_NUMBER
1668 && tv2->v_type != VAR_STRING))
1669 break;
1670 if (tv2->v_type == VAR_FLOAT)
1671 f = tv2->vval.v_float;
1672 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001673 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001674 switch (*op)
1675 {
1676 case '+': tv1->vval.v_float += f; break;
1677 case '-': tv1->vval.v_float -= f; break;
1678 case '*': tv1->vval.v_float *= f; break;
1679 case '/': tv1->vval.v_float /= f; break;
1680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001683 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 }
1685 }
1686
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001687 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 return FAIL;
1689}
1690
1691/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 * Evaluate the expression used in a ":for var in expr" command.
1693 * "arg" points to "var".
1694 * Set "*errp" to TRUE for an error, FALSE otherwise;
1695 * Return a pointer that holds the info. Null when there is an error.
1696 */
1697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001698eval_for_line(
1699 char_u *arg,
1700 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001701 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001702 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703{
Bram Moolenaar33570922005-01-25 22:26:29 +00001704 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001705 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001707 typval_T tv;
1708 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001711 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001713 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (fi == NULL)
1715 return NULL;
1716
Bram Moolenaar404557e2021-07-05 21:41:48 +02001717 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1718 &fi->fi_semicolon, FALSE);
1719 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return fi;
1721
Bram Moolenaar404557e2021-07-05 21:41:48 +02001722 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001723 if (expr[0] != 'i' || expr[1] != 'n'
1724 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001726 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1727 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1728 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001729 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 return fi;
1731 }
1732
1733 if (skip)
1734 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001735 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1736 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 {
1738 *errp = FALSE;
1739 if (!skip)
1740 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001742 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 l = tv.vval.v_list;
1744 if (l == NULL)
1745 {
1746 // a null list is like an empty list: do nothing
1747 clear_tv(&tv);
1748 }
1749 else
1750 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001752 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 // No need to increment the refcount, it's already set for
1755 // the list being used in "tv".
1756 fi->fi_list = l;
1757 list_add_watch(l, &fi->fi_lw);
1758 fi->fi_lw.lw_item = l->lv_first;
1759 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001760 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001762 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001763 fi->fi_bi = 0;
1764 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001765 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001766 typval_T btv;
1767
1768 // Make a copy, so that the iteration still works when the
1769 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001771 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001772 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001773 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001774 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 else if (tv.v_type == VAR_STRING)
1776 {
1777 fi->fi_byte_idx = 0;
1778 fi->fi_string = tv.vval.v_string;
1779 tv.vval.v_string = NULL;
1780 if (fi->fi_string == NULL)
1781 fi->fi_string = vim_strsave((char_u *)"");
1782 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 else
1784 {
Sean Dewar80d73952021-08-04 19:25:54 +02001785 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001786 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 }
1788 }
1789 }
1790 if (skip)
1791 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001792 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793
1794 return fi;
1795}
1796
1797/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001798 * Used when looping over a :for line, skip the "in expr" part.
1799 */
1800 void
1801skip_for_lines(void *fi_void, evalarg_T *evalarg)
1802{
1803 forinfo_T *fi = (forinfo_T *)fi_void;
1804 int i;
1805
1806 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001807 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001808}
1809
1810/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 * Use the first item in a ":for" list. Advance to the next.
1812 * Assign the values to the variable (list). "arg" points to the first one.
1813 * Return TRUE when a valid item was found, FALSE when at end of list or
1814 * something wrong.
1815 */
1816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001819 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001821 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001822 ? (ASSIGN_FINAL
1823 // first round: error if variable exists
1824 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1825 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001826 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001828 int skip_assign = in_vim9script() && arg[0] == '_'
1829 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001831 if (fi->fi_blob != NULL)
1832 {
1833 typval_T tv;
1834
1835 if (fi->fi_bi >= blob_len(fi->fi_blob))
1836 return FALSE;
1837 tv.v_type = VAR_NUMBER;
1838 tv.v_lock = VAR_FIXED;
1839 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1840 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001841 if (skip_assign)
1842 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001843 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 fi->fi_varcount, flag, NULL) == OK;
1845 }
1846
1847 if (fi->fi_string != NULL)
1848 {
1849 typval_T tv;
1850 int len;
1851
1852 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1853 if (len == 0)
1854 return FALSE;
1855 tv.v_type = VAR_STRING;
1856 tv.v_lock = VAR_FIXED;
1857 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1858 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001859 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001860 if (skip_assign)
1861 result = TRUE;
1862 else
1863 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001864 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001865 vim_free(tv.vval.v_string);
1866 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 }
1868
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 item = fi->fi_lw.lw_item;
1870 if (item == NULL)
1871 result = FALSE;
1872 else
1873 {
1874 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001875 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001876 if (skip_assign)
1877 result = TRUE;
1878 else
1879 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001880 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 }
1882 return result;
1883}
1884
1885/*
1886 * Free the structure used to store info used by ":for".
1887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001893 if (fi == NULL)
1894 return;
1895 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001898 list_unref(fi->fi_list);
1899 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001900 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001901 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001902 else
1903 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 vim_free(fi);
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001908set_context_for_expression(
1909 expand_T *xp,
1910 char_u *arg,
1911 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001917 if (cmdidx == CMD_let || cmdidx == CMD_var
1918 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 {
1920 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 {
1926 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001927 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 break;
1930 }
1931 return;
1932 }
1933 }
1934 else
1935 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1936 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 while ((xp->xp_pattern = vim_strpbrk(arg,
1938 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1939 {
1940 c = *xp->xp_pattern;
1941 if (c == '&')
1942 {
1943 c = xp->xp_pattern[1];
1944 if (c == '&')
1945 {
1946 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001947 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001952 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1953 xp->xp_pattern += 2;
1954
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 else if (c == '$')
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 xp->xp_context = EXPAND_ENV_VARS;
1961 }
1962 else if (c == '=')
1963 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001964 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 xp->xp_context = EXPAND_EXPRESSION;
1966 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001967 else if (c == '#'
1968 && xp->xp_context == EXPAND_EXPRESSION)
1969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001971 break;
1972 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001973 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 && xp->xp_context == EXPAND_FUNCTIONS
1975 && vim_strchr(xp->xp_pattern, '(') == NULL)
1976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 break;
1979 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001980 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001982 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1985 if (c == '\\' && xp->xp_pattern[1] != NUL)
1986 ++xp->xp_pattern;
1987 xp->xp_context = EXPAND_NOTHING;
1988 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001989 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001991 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1993 /* skip */ ;
1994 xp->xp_context = EXPAND_NOTHING;
1995 }
1996 else if (c == '|')
1997 {
1998 if (xp->xp_pattern[1] == '|')
1999 {
2000 ++xp->xp_pattern;
2001 xp->xp_context = EXPAND_EXPRESSION;
2002 }
2003 else
2004 xp->xp_context = EXPAND_COMMANDS;
2005 }
2006 else
2007 xp->xp_context = EXPAND_EXPRESSION;
2008 }
2009 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // Doesn't look like something valid, expand as an expression
2011 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 arg = xp->xp_pattern;
2014 if (*arg != NUL)
2015 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2016 /* skip */ ;
2017 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002018
2019 // ":exe one two" completes "two"
2020 if ((cmdidx == CMD_execute
2021 || cmdidx == CMD_echo
2022 || cmdidx == CMD_echon
2023 || cmdidx == CMD_echomsg)
2024 && xp->xp_context == EXPAND_EXPRESSION)
2025 {
2026 for (;;)
2027 {
2028 char_u *n = skiptowhite(arg);
2029
2030 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2031 break;
2032 arg = skipwhite(n);
2033 }
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 xp->xp_pattern = arg;
2037}
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002040 * Return TRUE if "pat" matches "text".
2041 * Does not use 'cpo' and always uses 'magic'.
2042 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002043 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002044pattern_match(char_u *pat, char_u *text, int ic)
2045{
2046 int matches = FALSE;
2047 char_u *save_cpo;
2048 regmatch_T regmatch;
2049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002051 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002052 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002053 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2054 if (regmatch.regprog != NULL)
2055 {
2056 regmatch.rm_ic = ic;
2057 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2058 vim_regfree(regmatch.regprog);
2059 }
2060 p_cpo = save_cpo;
2061 return matches;
2062}
2063
2064/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 * Handle a name followed by "(". Both for just "name(arg)" and for
2066 * "expr->name(arg)".
2067 * Returns OK or FAIL.
2068 */
2069 static int
2070eval_func(
2071 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002072 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002073 char_u *name,
2074 int name_len,
2075 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002076 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077 typval_T *basetv) // "expr" for "expr->name(arg)"
2078{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002079 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002080 char_u *s = name;
2081 int len = name_len;
2082 partial_T *partial;
2083 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002084 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002085 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086
2087 if (!evaluate)
2088 check_vars(s, len);
2089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002090 // If "s" is the name of a variable of type VAR_FUNC
2091 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002092 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002093 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // Need to make a copy, in case evaluating the arguments makes
2096 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002097 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002098 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002099 ret = FAIL;
2100 else
2101 {
2102 funcexe_T funcexe;
2103
2104 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002105 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002106 funcexe.fe_firstline = curwin->w_cursor.lnum;
2107 funcexe.fe_lastline = curwin->w_cursor.lnum;
2108 funcexe.fe_evaluate = evaluate;
2109 funcexe.fe_partial = partial;
2110 funcexe.fe_basetv = basetv;
2111 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002112 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002113 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002114 }
2115 vim_free(s);
2116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002117 // If evaluate is FALSE rettv->v_type was not set in
2118 // get_func_tv, but it's needed in handle_subscript() to parse
2119 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002120 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2121 {
2122 rettv->vval.v_string = NULL;
2123 rettv->v_type = VAR_FUNC;
2124 }
2125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002126 // Stop the expression evaluation when immediately
2127 // aborting on error, or when an interrupt occurred or
2128 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002129 if (evaluate && aborting())
2130 {
2131 if (ret == OK)
2132 clear_tv(rettv);
2133 ret = FAIL;
2134 }
2135 return ret;
2136}
2137
2138/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002139 * Get the next line source line without advancing. But do skip over comment
2140 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002141 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002142 */
2143 static char_u *
2144getline_peek_skip_comments(evalarg_T *evalarg)
2145{
2146 for (;;)
2147 {
2148 char_u *next = getline_peek(evalarg->eval_getline,
2149 evalarg->eval_cookie);
2150 char_u *p;
2151
2152 if (next == NULL)
2153 break;
2154 p = skipwhite(next);
2155 if (*p != NUL && !vim9_comment_start(p))
2156 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002157 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002158 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002159 }
2160 return NULL;
2161}
2162
2163/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002164 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2165 * comment) and there is a next line, return the next line (skipping blanks)
2166 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002167 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2168 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 * "arg" must point somewhere inside a line, not at the start.
2170 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002171 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002172eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2173{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002174 char_u *p = skipwhite(arg);
2175
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002176 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002177 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002178 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002179 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2180 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002181 && (*p == NUL || *p == NL
2182 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002184 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002185
Bram Moolenaare442d592022-05-05 12:20:28 +01002186 if (*p == NL)
2187 next = p + 1;
2188 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002189 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002190 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002192
Bram Moolenaar9d489562020-07-30 20:08:50 +02002193 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194 {
2195 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002196 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002197 }
2198 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002199 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002200}
2201
2202/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002203 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002204 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002205 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002206 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002207eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002208{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002209 garray_T *gap = &evalarg->eval_ga;
2210 char_u *line;
2211
Bram Moolenaar39be4982022-05-06 21:51:50 +01002212 if (arg != NULL)
2213 {
2214 if (*arg == NL)
2215 return skipwhite(arg + 1);
2216 // Truncate before a trailing comment, so that concatenating the lines
2217 // won't turn the rest into a comment.
2218 if (*skipwhite(arg) == '#')
2219 *arg = NUL;
2220 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002221
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002222 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002223 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2224 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002225 else
2226 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002227 if (line == NULL)
2228 return NULL;
2229
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002230 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002231 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2232 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002233 char_u *p = skipwhite(line);
2234
2235 // Going to concatenate the lines after parsing. For an empty or
2236 // comment line use an empty string.
2237 if (*p == NUL || vim9_comment_start(p))
2238 {
2239 vim_free(line);
2240 line = vim_strsave((char_u *)"");
2241 }
2242
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002243 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2244 ++gap->ga_len;
2245 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002246 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002247 {
2248 vim_free(evalarg->eval_tofree);
2249 evalarg->eval_tofree = line;
2250 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002251
2252 // Advanced to the next line, "arg" no longer points into the previous
2253 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002254 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002255 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002256}
2257
Bram Moolenaare6b53242020-07-01 17:28:33 +02002258/*
2259 * Call eval_next_non_blank() and get the next line if needed.
2260 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002261 char_u *
2262skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2263{
2264 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002265 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002266
Bram Moolenaar962d7212020-07-04 14:15:00 +02002267 if (evalarg == NULL)
2268 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002269 eval_next_non_blank(p, evalarg, &getnext);
2270 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002271 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002272 return p;
2273}
2274
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002275/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002276 * Initialize "evalarg" for use.
2277 */
2278 void
2279init_evalarg(evalarg_T *evalarg)
2280{
2281 CLEAR_POINTER(evalarg);
2282 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2283}
2284
2285/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002286 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002287 */
2288 void
2289clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2290{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002291 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002292 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002293 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002294 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002295 if (eap != NULL)
2296 {
2297 // We may need to keep the original command line, e.g. for
2298 // ":let" it has the variable names. But we may also need the
2299 // new one, "nextcmd" points into it. Keep both.
2300 vim_free(eap->cmdline_tofree);
2301 eap->cmdline_tofree = *eap->cmdlinep;
2302 *eap->cmdlinep = evalarg->eval_tofree;
2303 }
2304 else
2305 vim_free(evalarg->eval_tofree);
2306 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002307 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002308
Bram Moolenaar844fb642021-10-23 13:32:30 +01002309 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002310 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002311 }
2312}
2313
2314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2318 */
2319
2320/*
2321 * Handle zero level expression.
2322 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002324 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 * Return OK or FAIL.
2327 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002329eval0(
2330 char_u *arg,
2331 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002332 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002335 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2336}
2337
2338/*
2339 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2340 * expression and don't check what comes after the expression.
2341 */
2342 int
2343eval0_retarg(
2344 char_u *arg,
2345 typval_T *rettv,
2346 exarg_T *eap,
2347 evalarg_T *evalarg,
2348 char_u **retarg)
2349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 int ret;
2351 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002352 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 int did_emsg_before = did_emsg;
2354 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002356 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002357 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002361 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002362 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002363
Bram Moolenaar02929a32021-12-17 14:46:12 +00002364 // In Vim9 script a command block is not split at NL characters for
2365 // commands using an expression argument. Skip over a '#' comment to check
2366 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002367 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002368 while (*p == '#')
2369 {
2370 char_u *nl = vim_strchr(p, NL);
2371
2372 if (nl == NULL)
2373 break;
2374 p = skipwhite(nl + 1);
2375 if (eap != NULL && *p != NUL)
2376 eap->nextcmd = p;
2377 check_for_end = FALSE;
2378 }
2379
2380 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002381 end_error = !ends_excmd2(arg, p);
2382 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 /*
2387 * Report the invalid expression unless the expression evaluation has
2388 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002389 * exception, or we already gave a more specific error.
2390 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002392 if (!aborting()
2393 && did_emsg == did_emsg_before
2394 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002395 && (flags & EVAL_CONSTANT) == 0
2396 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002397 {
2398 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002399 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002400 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002401 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002402 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002403
2404 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002405 // a next command to avoid more errors, unless "|" is following, which
2406 // could only be a command separator.
2407 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2408 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002411
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002412 if (retarg != NULL)
2413 *retarg = p;
2414 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002415 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return ret;
2418}
2419
2420/*
2421 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002422 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002423 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 *
2425 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002426 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002428 * Note: "rettv.v_lock" is not set.
2429 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 * Return OK or FAIL.
2431 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002432 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002435 char_u *p;
2436 int getnext;
2437
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002438 CLEAR_POINTER(rettv);
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /*
2441 * Get the first variable.
2442 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
2445
Bram Moolenaar793648f2020-06-26 21:28:25 +02002446 p = eval_next_non_blank(*arg, evalarg, &getnext);
2447 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002449 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450 int result;
2451 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 evalarg_T *evalarg_used = evalarg;
2453 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002455 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002456 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457
2458 if (evalarg == NULL)
2459 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002460 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002461 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463 orig_flags = evalarg_used->eval_flags;
2464 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002467 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002469 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002470 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002471 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002472 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002473 clear_tv(rettv);
2474 return FAIL;
2475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 int error = FALSE;
2483
Bram Moolenaar13106602020-10-04 16:06:05 +02002484 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002485 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002486 else if (vim9script)
2487 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002488 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (error || !op_falsy || !result)
2491 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 if (error)
2493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002497 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002499 if (op_falsy)
2500 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002501 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002502 {
mityu4ac198c2021-05-28 17:52:40 +02002503 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002504 clear_tv(rettv);
2505 return FAIL;
2506 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002508 evalarg_used->eval_flags = (op_falsy ? !result : result)
2509 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2510 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002511 {
2512 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002514 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002515 if (!op_falsy || !result)
2516 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002518 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002520 /*
2521 * Check for the ":".
2522 */
2523 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2524 if (*p != ':')
2525 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002526 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002527 if (evaluate && result)
2528 clear_tv(rettv);
2529 evalarg_used->eval_flags = orig_flags;
2530 return FAIL;
2531 }
2532 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002533 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002534 else
2535 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002536 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002537 {
2538 error_white_both(p, 1);
2539 clear_tv(rettv);
2540 evalarg_used->eval_flags = orig_flags;
2541 return FAIL;
2542 }
2543 *arg = p;
2544 }
2545
2546 /*
2547 * Get the third variable. Recursive!
2548 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002549 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002550 {
mityu4ac198c2021-05-28 17:52:40 +02002551 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002552 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002553 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002554 return FAIL;
2555 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002556 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2557 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002559 if (eval1(arg, &var2, evalarg_used) == FAIL)
2560 {
2561 if (evaluate && result)
2562 clear_tv(rettv);
2563 evalarg_used->eval_flags = orig_flags;
2564 return FAIL;
2565 }
2566 if (evaluate && !result)
2567 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569
2570 if (evalarg == NULL)
2571 clear_evalarg(&local_evalarg, NULL);
2572 else
2573 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
2576 return OK;
2577}
2578
2579/*
2580 * Handle first level expression:
2581 * expr2 || expr2 || expr2 logical OR
2582 *
2583 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002584 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 *
2586 * Return OK or FAIL.
2587 */
2588 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002591 char_u *p;
2592 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002595 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 return FAIL;
2599
2600 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002603 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606 evalarg_T *evalarg_used = evalarg;
2607 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002608 int evaluate;
2609 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 long result = FALSE;
2611 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002613 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002614
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002615 if (evalarg == NULL)
2616 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002617 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 orig_flags = evalarg_used->eval_flags;
2621 evaluate = orig_flags & EVAL_EVALUATE;
2622 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002623 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002624 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002625 result = tv_get_bool_chk(rettv, &error);
2626 else if (tv_get_number_chk(rettv, &error) != 0)
2627 result = TRUE;
2628 clear_tv(rettv);
2629 if (error)
2630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632
2633 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 while (p[0] == '|' && p[1] == '|')
2637 {
2638 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002639 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002640 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002642 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002643 {
2644 error_white_both(p, 2);
2645 clear_tv(rettv);
2646 return FAIL;
2647 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002648 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650
2651 /*
2652 * Get the second variable.
2653 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002654 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002655 {
mityu4ac198c2021-05-28 17:52:40 +02002656 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002657 clear_tv(rettv);
2658 return FAIL;
2659 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2661 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002662 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
2666 /*
2667 * Compute the result.
2668 */
2669 if (evaluate && !result)
2670 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002671 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002672 result = tv_get_bool_chk(&var2, &error);
2673 else if (tv_get_number_chk(&var2, &error) != 0)
2674 result = TRUE;
2675 clear_tv(&var2);
2676 if (error)
2677 return FAIL;
2678 }
2679 if (evaluate)
2680 {
2681 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 rettv->v_type = VAR_BOOL;
2684 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 }
2686 else
2687 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002688 rettv->v_type = VAR_NUMBER;
2689 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692
2693 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002695
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 if (evalarg == NULL)
2697 clear_evalarg(&local_evalarg, NULL);
2698 else
2699 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 return OK;
2703}
2704
2705/*
2706 * Handle second level expression:
2707 * expr3 && expr3 && expr3 logical AND
2708 *
2709 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002710 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 *
2712 * Return OK or FAIL.
2713 */
2714 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002717 char_u *p;
2718 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002721 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725
2726 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002729 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002730 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 evalarg_T *evalarg_used = evalarg;
2733 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002734 int orig_flags;
2735 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736 long result = TRUE;
2737 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002739 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002740
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002741 if (evalarg == NULL)
2742 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002743 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002744 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746 orig_flags = evalarg_used->eval_flags;
2747 evaluate = orig_flags & EVAL_EVALUATE;
2748 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002750 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002751 result = tv_get_bool_chk(rettv, &error);
2752 else if (tv_get_number_chk(rettv, &error) == 0)
2753 result = FALSE;
2754 clear_tv(rettv);
2755 if (error)
2756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758
2759 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 while (p[0] == '&' && p[1] == '&')
2763 {
2764 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002765 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002766 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002767 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002768 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002769 {
2770 error_white_both(p, 2);
2771 clear_tv(rettv);
2772 return FAIL;
2773 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002774 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002775 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002776
2777 /*
2778 * Get the second variable.
2779 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002780 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002781 {
mityu4ac198c2021-05-28 17:52:40 +02002782 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002783 clear_tv(rettv);
2784 return FAIL;
2785 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002786 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2787 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002788 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002789 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002791 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002792
2793 /*
2794 * Compute the result.
2795 */
2796 if (evaluate && result)
2797 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002798 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002799 result = tv_get_bool_chk(&var2, &error);
2800 else if (tv_get_number_chk(&var2, &error) == 0)
2801 result = FALSE;
2802 clear_tv(&var2);
2803 if (error)
2804 return FAIL;
2805 }
2806 if (evaluate)
2807 {
2808 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002809 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002810 rettv->v_type = VAR_BOOL;
2811 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002812 }
2813 else
2814 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002815 rettv->v_type = VAR_NUMBER;
2816 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002817 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002819
2820 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002822
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002823 if (evalarg == NULL)
2824 clear_evalarg(&local_evalarg, NULL);
2825 else
2826 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
2829 return OK;
2830}
2831
2832/*
2833 * Handle third level expression:
2834 * var1 == var2
2835 * var1 =~ var2
2836 * var1 != var2
2837 * var1 !~ var2
2838 * var1 > var2
2839 * var1 >= var2
2840 * var1 < var2
2841 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002842 * var1 is var2
2843 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *
2845 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 *
2848 * Return OK or FAIL.
2849 */
2850 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002855 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002857 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002860 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 return FAIL;
2864
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002865 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002866
Bram Moolenaar696ba232020-07-29 21:20:41 +02002867 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002870 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002872 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002874 typval_T var2;
2875 int ic;
2876 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002877 int evaluate = evalarg == NULL
2878 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002879 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002880
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002881 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002882 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002883 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002884 p = *arg;
2885 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002886 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2887 {
mityu4ac198c2021-05-28 17:52:40 +02002888 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002889 clear_tv(rettv);
2890 return FAIL;
2891 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002892
Bram Moolenaar696ba232020-07-29 21:20:41 +02002893 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2894 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002895 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002896 clear_tv(rettv);
2897 return FAIL;
2898 }
2899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002900 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (p[len] == '?')
2902 {
2903 ic = TRUE;
2904 ++len;
2905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 else if (p[len] == '#')
2908 {
2909 ic = FALSE;
2910 ++len;
2911 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002912 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002914 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 /*
2917 * Get the second variable.
2918 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002919 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2920 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002921 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002922 clear_tv(rettv);
2923 return FAIL;
2924 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002925 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002928 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 return FAIL;
2930 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002931 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002932 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002933 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002934
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002935 // use the line of the comparison for messages
2936 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002937 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002938 {
2939 ret = FAIL;
2940 clear_tv(rettv);
2941 }
2942 else
2943 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002944 clear_tv(&var2);
2945 return ret;
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948
2949 return OK;
2950}
2951
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002952/*
2953 * Make a copy of blob "tv1" and append blob "tv2".
2954 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 void
2956eval_addblob(typval_T *tv1, typval_T *tv2)
2957{
2958 blob_T *b1 = tv1->vval.v_blob;
2959 blob_T *b2 = tv2->vval.v_blob;
2960 blob_T *b = blob_alloc();
2961 int i;
2962
2963 if (b != NULL)
2964 {
2965 for (i = 0; i < blob_len(b1); i++)
2966 ga_append(&b->bv_ga, blob_get(b1, i));
2967 for (i = 0; i < blob_len(b2); i++)
2968 ga_append(&b->bv_ga, blob_get(b2, i));
2969
2970 clear_tv(tv1);
2971 rettv_blob_set(tv1, b);
2972 }
2973}
2974
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002975/*
2976 * Make a copy of list "tv1" and append list "tv2".
2977 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 int
2979eval_addlist(typval_T *tv1, typval_T *tv2)
2980{
2981 typval_T var3;
2982
2983 // concatenate Lists
2984 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2985 {
2986 clear_tv(tv1);
2987 clear_tv(tv2);
2988 return FAIL;
2989 }
2990 clear_tv(tv1);
2991 *tv1 = var3;
2992 return OK;
2993}
2994
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002996 * Handle the bitwise left/right shift operator expression:
2997 * var1 << var2
2998 * var1 >> var2
2999 *
3000 * "arg" must point to the first non-white of the expression.
3001 * "arg" is advanced to just after the recognized expression.
3002 *
3003 * Return OK or FAIL.
3004 */
3005 static int
3006eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3007{
3008 /*
3009 * Get the first expression.
3010 */
3011 if (eval6(arg, rettv, evalarg) == FAIL)
3012 return FAIL;
3013
3014 /*
3015 * Repeat computing, until no '<<' or '>>' is following.
3016 */
3017 for (;;)
3018 {
3019 char_u *p;
3020 int getnext;
3021 exprtype_T type;
3022 int evaluate;
3023 typval_T var2;
3024 int vim9script;
3025
3026 p = eval_next_non_blank(*arg, evalarg, &getnext);
3027 if (p[0] == '<' && p[1] == '<')
3028 type = EXPR_LSHIFT;
3029 else if (p[0] == '>' && p[1] == '>')
3030 type = EXPR_RSHIFT;
3031 else
3032 return OK;
3033
3034 // Handle a bitwise left or right shift operator
3035 if (rettv->v_type != VAR_NUMBER)
3036 {
3037 // left operand should be a number
3038 emsg(_(e_bitshift_ops_must_be_number));
3039 clear_tv(rettv);
3040 return FAIL;
3041 }
3042
3043 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3044 vim9script = in_vim9script();
3045 if (getnext)
3046 {
3047 *arg = eval_next_line(*arg, evalarg);
3048 p = *arg;
3049 }
3050 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3051 {
3052 error_white_both(*arg, 2);
3053 clear_tv(rettv);
3054 return FAIL;
3055 }
3056
3057 /*
3058 * Get the second variable.
3059 */
3060 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3061 {
3062 error_white_both(p, 2);
3063 clear_tv(rettv);
3064 return FAIL;
3065 }
3066 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3067 if (eval6(arg, &var2, evalarg) == FAIL)
3068 {
3069 clear_tv(rettv);
3070 return FAIL;
3071 }
3072
3073 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3074 {
3075 // right operand should be a positive number
3076 if (var2.v_type != VAR_NUMBER)
3077 emsg(_(e_bitshift_ops_must_be_number));
3078 else
3079 emsg(_(e_bitshift_ops_must_be_postive));
3080 clear_tv(rettv);
3081 clear_tv(&var2);
3082 return FAIL;
3083 }
3084
3085 if (evaluate)
3086 {
3087 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3088 // shifting more bits than we have always results in zero
3089 rettv->vval.v_number = 0;
3090 else if (type == EXPR_LSHIFT)
3091 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003092 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003093 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003094 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003095 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003096 }
3097
3098 clear_tv(&var2);
3099 }
3100
3101 return OK;
3102}
3103
3104/*
3105 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003106 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003108 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003109 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 *
3111 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003112 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 *
3114 * Return OK or FAIL.
3115 */
3116 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003117eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003120 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003122 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return FAIL;
3124
3125 /*
3126 * Repeat computing, until no '+', '-' or '.' is following.
3127 */
3128 for (;;)
3129 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003130 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 int getnext;
3132 char_u *p;
3133 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003134 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003135 int concat;
3136 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003137 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003138
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003139 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003140 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003141 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142 p = eval_next_non_blank(*arg, evalarg, &getnext);
3143 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003144 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003145 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3146 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003148 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3149 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003150
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003151 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003152 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003153 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003154 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003155 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003156 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003157 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003158 {
mityu4ac198c2021-05-28 17:52:40 +02003159 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003160 clear_tv(rettv);
3161 return FAIL;
3162 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003163 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003164 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003165 if ((op != '+' || (rettv->v_type != VAR_LIST
3166 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167#ifdef FEAT_FLOAT
3168 && (op == '.' || rettv->v_type != VAR_FLOAT)
3169#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003170 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003171 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003172 int error = FALSE;
3173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003174 // For "list + ...", an illegal use of the first operand as
3175 // a number cannot be determined before evaluating the 2nd
3176 // operand: if this is also a list, all is ok.
3177 // For "something . ...", "something - ..." or "non-list + ...",
3178 // we know that the first operand needs to be a string or number
3179 // without evaluating the 2nd operand. So check before to avoid
3180 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003181 if (op != '.')
3182 tv_get_number_chk(rettv, &error);
3183 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003184 {
3185 clear_tv(rettv);
3186 return FAIL;
3187 }
3188 }
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /*
3191 * Get the second variable.
3192 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003193 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003194 {
mityu89dcb4d2021-05-28 13:50:17 +02003195 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003196 clear_tv(rettv);
3197 return FAIL;
3198 }
3199 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003200 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 return FAIL;
3204 }
3205
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003206 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 /*
3209 * Compute the result.
3210 */
3211 if (op == '.')
3212 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3214 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003215 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216
Bram Moolenaar2e086612020-08-25 22:37:48 +02003217 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003218 || var2.v_type == VAR_CHANNEL
3219 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003220 semsg(_(e_using_invalid_value_as_string_str),
3221 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003222#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003223 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003224 {
3225 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3226 var2.vval.v_float);
3227 s2 = buf2;
3228 }
3229#endif
3230 else
3231 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003232 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003233 {
3234 clear_tv(rettv);
3235 clear_tv(&var2);
3236 return FAIL;
3237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003238 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 clear_tv(rettv);
3240 rettv->v_type = VAR_STRING;
3241 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003243 else if (op == '+' && rettv->v_type == VAR_BLOB
3244 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003246 else if (op == '+' && rettv->v_type == VAR_LIST
3247 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003250 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 else
3253 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003254 int error = FALSE;
3255 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003256#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003257 float_T f1 = 0, f2 = 0;
3258
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003259 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003260 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003261 f1 = rettv->vval.v_float;
3262 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003264 else
3265#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003267 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 if (error)
3269 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003270 // This can only happen for "list + non-list" or
3271 // "blob + non-blob". For "non-list + ..." or
3272 // "something - ...", we returned before evaluating the
3273 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003274 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003275 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003276 return FAIL;
3277 }
3278#ifdef FEAT_FLOAT
3279 if (var2.v_type == VAR_FLOAT)
3280 f1 = n1;
3281#endif
3282 }
3283#ifdef FEAT_FLOAT
3284 if (var2.v_type == VAR_FLOAT)
3285 {
3286 f2 = var2.vval.v_float;
3287 n2 = 0;
3288 }
3289 else
3290#endif
3291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003292 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003293 if (error)
3294 {
3295 clear_tv(rettv);
3296 clear_tv(&var2);
3297 return FAIL;
3298 }
3299#ifdef FEAT_FLOAT
3300 if (rettv->v_type == VAR_FLOAT)
3301 f2 = n2;
3302#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003304 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003305
3306#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003307 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003308 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3309 {
3310 if (op == '+')
3311 f1 = f1 + f2;
3312 else
3313 f1 = f1 - f2;
3314 rettv->v_type = VAR_FLOAT;
3315 rettv->vval.v_float = f1;
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318#endif
3319 {
3320 if (op == '+')
3321 n1 = n1 + n2;
3322 else
3323 n1 = n1 - n2;
3324 rettv->v_type = VAR_NUMBER;
3325 rettv->vval.v_number = n1;
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 }
3331 return OK;
3332}
3333
3334/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003335 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * * number multiplication
3337 * / number division
3338 * % number modulo
3339 *
3340 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003341 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 *
3343 * Return OK or FAIL.
3344 */
3345 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003346eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347 char_u **arg,
3348 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003349 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003350 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003352#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003353 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003357 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003359 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 return FAIL;
3361
3362 /*
3363 * Repeat computing, until no '*', '/' or '%' is following.
3364 */
3365 for (;;)
3366 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003367 int evaluate;
3368 int getnext;
3369 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003370 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003371 int op;
3372 varnumber_T n1, n2;
3373#ifdef FEAT_FLOAT
3374 float_T f1, f2;
3375#endif
3376 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003377
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003378 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003379 p = eval_next_non_blank(*arg, evalarg, &getnext);
3380 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003381 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003383
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003384 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003385 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003386 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003387 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003388 {
3389 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3390 {
mityu4ac198c2021-05-28 17:52:40 +02003391 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003392 clear_tv(rettv);
3393 return FAIL;
3394 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003395 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003398#ifdef FEAT_FLOAT
3399 f1 = 0;
3400 f2 = 0;
3401#endif
3402 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003403 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003405#ifdef FEAT_FLOAT
3406 if (rettv->v_type == VAR_FLOAT)
3407 {
3408 f1 = rettv->vval.v_float;
3409 use_float = TRUE;
3410 n1 = 0;
3411 }
3412 else
3413#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003414 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003415 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 if (error)
3417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419 else
3420 n1 = 0;
3421
3422 /*
3423 * Get the second variable.
3424 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003425 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3426 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003427 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003428 clear_tv(rettv);
3429 return FAIL;
3430 }
3431 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003432 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 return FAIL;
3434
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003435 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003437#ifdef FEAT_FLOAT
3438 if (var2.v_type == VAR_FLOAT)
3439 {
3440 if (!use_float)
3441 {
3442 f1 = n1;
3443 use_float = TRUE;
3444 }
3445 f2 = var2.vval.v_float;
3446 n2 = 0;
3447 }
3448 else
3449#endif
3450 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003451 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003452 clear_tv(&var2);
3453 if (error)
3454 return FAIL;
3455#ifdef FEAT_FLOAT
3456 if (use_float)
3457 f2 = n2;
3458#endif
3459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
3461 /*
3462 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003463 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003465#ifdef FEAT_FLOAT
3466 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003468 if (op == '*')
3469 f1 = f1 * f2;
3470 else if (op == '/')
3471 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003472# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003473 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003474 if (f2 == 0.0)
3475 {
3476 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003478 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003479 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003480 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003481 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003482 }
3483 else
3484 f1 = f1 / f2;
3485# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003486 // We rely on the floating point library to handle divide
3487 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003488 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003489# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003493 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494 return FAIL;
3495 }
3496 rettv->v_type = VAR_FLOAT;
3497 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003502 int failed = FALSE;
3503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003504 if (op == '*')
3505 n1 = n1 * n2;
3506 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003507 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003509 n1 = num_modulus(n1, n2, &failed);
3510 if (failed)
3511 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003513 rettv->v_type = VAR_NUMBER;
3514 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
3518
3519 return OK;
3520}
3521
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003522/*
3523 * Handle a type cast before a base level expression.
3524 * "arg" must point to the first non-white of the expression.
3525 * "arg" is advanced to just after the recognized expression.
3526 * Return OK or FAIL.
3527 */
3528 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003529eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003530 char_u **arg,
3531 typval_T *rettv,
3532 evalarg_T *evalarg,
3533 int want_string) // after "." operator
3534{
3535 type_T *want_type = NULL;
3536 garray_T type_list; // list of pointers to allocated types
3537 int res;
3538 int evaluate = evalarg == NULL ? 0
3539 : (evalarg->eval_flags & EVAL_EVALUATE);
3540
3541 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003542 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3543 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003544 {
3545 ++*arg;
3546 ga_init2(&type_list, sizeof(type_T *), 10);
3547 want_type = parse_type(arg, &type_list, TRUE);
3548 if (want_type == NULL && (evaluate || **arg != '>'))
3549 {
3550 clear_type_list(&type_list);
3551 return FAIL;
3552 }
3553
3554 if (**arg != '>')
3555 {
3556 if (*skipwhite(*arg) == '>')
3557 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3558 else
3559 emsg(_(e_missing_gt));
3560 clear_type_list(&type_list);
3561 return FAIL;
3562 }
3563 ++*arg;
3564 *arg = skipwhite_and_linebreak(*arg, evalarg);
3565 }
3566
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003567 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003568
3569 if (want_type != NULL && evaluate)
3570 {
3571 if (res == OK)
3572 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003573 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3574 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003575
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003576 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003577 {
3578 if (want_type == &t_bool && actual != &t_bool
3579 && (actual->tt_flags & TTFLAG_BOOL_OK))
3580 {
3581 int n = tv2bool(rettv);
3582
3583 // can use "0" and "1" for boolean in some places
3584 clear_tv(rettv);
3585 rettv->v_type = VAR_BOOL;
3586 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3587 }
3588 else
3589 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003590 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003591
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003592 where.wt_variable = TRUE;
3593 res = check_type(want_type, actual, TRUE, where);
3594 }
3595 }
3596 }
3597 clear_type_list(&type_list);
3598 }
3599
3600 return res;
3601}
3602
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003603 int
3604eval_leader(char_u **arg, int vim9)
3605{
3606 char_u *s = *arg;
3607 char_u *p = *arg;
3608
3609 while (*p == '!' || *p == '-' || *p == '+')
3610 {
3611 char_u *n = skipwhite(p + 1);
3612
3613 // ++, --, -+ and +- are not accepted in Vim9 script
3614 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3615 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003616 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003617 return FAIL;
3618 }
3619 p = n;
3620 }
3621 *arg = p;
3622 return OK;
3623}
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003626 * Check for a predefined value "true", "false" and "null.*".
3627 * Return OK when recognized.
3628 */
3629 int
3630handle_predefined(char_u *s, int len, typval_T *rettv)
3631{
3632 switch (len)
3633 {
3634 case 4: if (STRNCMP(s, "true", 4) == 0)
3635 {
3636 rettv->v_type = VAR_BOOL;
3637 rettv->vval.v_number = VVAL_TRUE;
3638 return OK;
3639 }
3640 if (STRNCMP(s, "null", 4) == 0)
3641 {
3642 rettv->v_type = VAR_SPECIAL;
3643 rettv->vval.v_number = VVAL_NULL;
3644 return OK;
3645 }
3646 break;
3647 case 5: if (STRNCMP(s, "false", 5) == 0)
3648 {
3649 rettv->v_type = VAR_BOOL;
3650 rettv->vval.v_number = VVAL_FALSE;
3651 return OK;
3652 }
3653 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003654 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3655 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003656#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003657 rettv->v_type = VAR_JOB;
3658 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003659#else
3660 rettv->v_type = VAR_SPECIAL;
3661 rettv->vval.v_number = VVAL_NULL;
3662#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003663 return OK;
3664 }
3665 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003666 case 9:
3667 if (STRNCMP(s, "null_", 5) != 0)
3668 break;
3669 if (STRNCMP(s + 5, "list", 4) == 0)
3670 {
3671 rettv->v_type = VAR_LIST;
3672 rettv->vval.v_list = NULL;
3673 return OK;
3674 }
3675 if (STRNCMP(s + 5, "dict", 4) == 0)
3676 {
3677 rettv->v_type = VAR_DICT;
3678 rettv->vval.v_dict = NULL;
3679 return OK;
3680 }
3681 if (STRNCMP(s + 5, "blob", 4) == 0)
3682 {
3683 rettv->v_type = VAR_BLOB;
3684 rettv->vval.v_blob = NULL;
3685 return OK;
3686 }
3687 break;
3688 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3689 {
3690 rettv->v_type = VAR_STRING;
3691 rettv->vval.v_string = NULL;
3692 return OK;
3693 }
3694 break;
3695 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003696 if (STRNCMP(s, "null_channel", 12) == 0)
3697 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003698#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003699 rettv->v_type = VAR_CHANNEL;
3700 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003701#else
3702 rettv->v_type = VAR_SPECIAL;
3703 rettv->vval.v_number = VVAL_NULL;
3704#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003705 return OK;
3706 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003707 if (STRNCMP(s, "null_partial", 12) == 0)
3708 {
3709 rettv->v_type = VAR_PARTIAL;
3710 rettv->vval.v_partial = NULL;
3711 return OK;
3712 }
3713 break;
3714 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3715 {
3716 rettv->v_type = VAR_FUNC;
3717 rettv->vval.v_string = NULL;
3718 return OK;
3719 }
3720 break;
3721 }
3722 return FAIL;
3723}
3724
3725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * Handle sixth level expression:
3727 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003728 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003729 * "string" string constant
3730 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 * &option-name option value
3732 * @r register contents
3733 * identifier variable value
3734 * function() function call
3735 * $VAR environment variable
3736 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003737 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003739 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003740 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 *
3742 * Also handle:
3743 * ! in front logical NOT
3744 * - in front unary minus
3745 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003746 * trailing [] subscript in String or List
3747 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003748 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *
3750 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003751 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 *
3753 * Return OK or FAIL.
3754 */
3755 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003756eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757 char_u **arg,
3758 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003759 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003762 int evaluate = evalarg != NULL
3763 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int len;
3765 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003766 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 char_u *start_leader, *end_leader;
3768 int ret = OK;
3769 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003770 static int recurse = 0;
3771 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003775 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003780 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 */
3782 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003783 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003784 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 end_leader = *arg;
3786
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003787 if (**arg == '.' && (!isdigit(*(*arg + 1))
3788#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003789 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003790#endif
3791 ))
3792 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003793 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003794 ++*arg;
3795 return FAIL;
3796 }
3797
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003798 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003799 // and crash. With MSVC the stack is smaller.
3800 if (recurse ==
3801#ifdef _MSC_VER
3802 300
3803#else
3804 1000
3805#endif
3806 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003807 {
3808 semsg(_(e_expression_too_recursive_str), *arg);
3809 return FAIL;
3810 }
3811 ++recurse;
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 switch (**arg)
3814 {
3815 /*
3816 * Number constant.
3817 */
3818 case '0':
3819 case '1':
3820 case '2':
3821 case '3':
3822 case '4':
3823 case '5':
3824 case '6':
3825 case '7':
3826 case '8':
3827 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003828 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003829
3830 // Apply prefixed "-" and "+" now. Matters especially when
3831 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003832 if (ret == OK && evaluate && end_leader > start_leader
3833 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003834 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 break;
3836
3837 /*
3838 * String constant: "string".
3839 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003840 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 break;
3842
3843 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003846 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847 break;
3848
3849 /*
3850 * List: [expr, expr]
3851 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003852 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
3855 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003856 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003857 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003858 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003859 {
3860 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3861 }
3862 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003863 {
3864 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003865 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003866 }
3867 else
3868 ret = NOTDONE;
3869 break;
3870
3871 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003872 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003873 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003874 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003875 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003876 ret = NOTDONE;
3877 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003878 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003879 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003880 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003881 break;
3882
3883 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003884 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003886 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 break;
3888
3889 /*
3890 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003891 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
LemonBoy2eaef102022-05-06 13:14:50 +01003893 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3894 ret = eval_interp_string(arg, rettv, evaluate);
3895 else
3896 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 break;
3898
3899 /*
3900 * Register contents: @r.
3901 */
3902 case '@': ++*arg;
3903 if (evaluate)
3904 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003905 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003906 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003907 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003908 emsg_invreg(**arg);
3909 else
3910 {
3911 rettv->v_type = VAR_STRING;
3912 rettv->vval.v_string = get_reg_contents(**arg,
3913 GREG_EXPR_SRC);
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 if (**arg != NUL)
3917 ++*arg;
3918 break;
3919
3920 /*
3921 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003922 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003924 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003925 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003926 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003927 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003928 if (ret == OK && evaluate)
3929 {
3930 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3931
Bram Moolenaara9931532021-06-12 15:58:16 +02003932 // Compile it here to get the return type. The return
3933 // type is optional, when it's missing use t_unknown.
3934 // This is recognized in compile_return().
3935 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3936 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003937 if (compile_def_function(ufunc, FALSE,
3938 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003939 {
3940 clear_tv(rettv);
3941 ret = FAIL;
3942 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003943 }
3944 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003945 if (ret == NOTDONE)
3946 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003947 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003948 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003949
Bram Moolenaar9215f012020-06-27 21:18:00 +02003950 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003951 if (**arg == ')')
3952 ++*arg;
3953 else if (ret == OK)
3954 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003955 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003956 clear_tv(rettv);
3957 ret = FAIL;
3958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 break;
3961
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 break;
3964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003965
3966 if (ret == NOTDONE)
3967 {
3968 /*
3969 * Must be a variable or function name.
3970 * Can also be a curly-braces kind of name: {expr}.
3971 */
3972 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003973 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 if (alias != NULL)
3975 s = alias;
3976
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003977 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 ret = FAIL;
3979 else
3980 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003981 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3982
Bram Moolenaar4525a572022-02-13 11:57:33 +00003983 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003984 {
3985 emsg(_(e_cannot_use_underscore_here));
3986 ret = FAIL;
3987 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003988 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00003989 && s[0] == 's' && s[1] == ':')
3990 {
3991 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
3992 ret = FAIL;
3993 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003994 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003995 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003996 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003997 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003998 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003999 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004000 else if (flags & EVAL_CONSTANT)
4001 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004002 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004003 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004004 // get the value of "true", "false", etc. or a variable
4005 ret = FAIL;
4006 if (vim9script)
4007 ret = handle_predefined(s, len, rettv);
4008 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004009 {
4010 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004011 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004012 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004013 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004014 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004015 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004016 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004017 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004018 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004019 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004021 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004022 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 }
4024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4026 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004027 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004028 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030 /*
4031 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4032 */
4033 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004034 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004035
4036 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004037 return ret;
4038}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004039
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004040/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004041 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004042 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004043 * Adjusts "end_leaderp" until it is at "start_leader".
4044 */
4045 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004046eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004047 typval_T *rettv,
4048 int numeric_only,
4049 char_u *start_leader,
4050 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004051{
4052 char_u *end_leader = *end_leaderp;
4053 int ret = OK;
4054 int error = FALSE;
4055 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004056 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004058#ifdef FEAT_FLOAT
4059 float_T f = 0.0;
4060
4061 if (rettv->v_type == VAR_FLOAT)
4062 f = rettv->vval.v_float;
4063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004064#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004065 {
4066 while (VIM_ISWHITE(end_leader[-1]))
4067 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004068 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004069 val = tv2bool(rettv);
4070 else
4071 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004072 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004073 if (error)
4074 {
4075 clear_tv(rettv);
4076 ret = FAIL;
4077 }
4078 else
4079 {
4080 while (end_leader > start_leader)
4081 {
4082 --end_leader;
4083 if (*end_leader == '!')
4084 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004085 if (numeric_only)
4086 {
4087 ++end_leader;
4088 break;
4089 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004090#ifdef FEAT_FLOAT
4091 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004092 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004093 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004094 {
4095 rettv->v_type = VAR_BOOL;
4096 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4097 }
4098 else
4099 f = !f;
4100 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004101 else
4102#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004103 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004104 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004105 type = VAR_BOOL;
4106 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004107 }
4108 else if (*end_leader == '-')
4109 {
4110#ifdef FEAT_FLOAT
4111 if (rettv->v_type == VAR_FLOAT)
4112 f = -f;
4113 else
4114#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004115 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004116 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004117 type = VAR_NUMBER;
4118 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004119 }
4120 }
4121#ifdef FEAT_FLOAT
4122 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004125 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004127 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004128#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004129 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004130 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004131 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004132 rettv->v_type = type;
4133 else
4134 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004135 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004138 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 return ret;
4140}
4141
4142/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004143 * Call the function referred to in "rettv".
4144 */
4145 static int
4146call_func_rettv(
4147 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004148 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004149 typval_T *rettv,
4150 int evaluate,
4151 dict_T *selfdict,
4152 typval_T *basetv)
4153{
4154 partial_T *pt = NULL;
4155 funcexe_T funcexe;
4156 typval_T functv;
4157 char_u *s;
4158 int ret;
4159
4160 // need to copy the funcref so that we can clear rettv
4161 if (evaluate)
4162 {
4163 functv = *rettv;
4164 rettv->v_type = VAR_UNKNOWN;
4165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004166 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004167 if (functv.v_type == VAR_PARTIAL)
4168 {
4169 pt = functv.vval.v_partial;
4170 s = partial_name(pt);
4171 }
4172 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004173 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004174 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004175 if (s == NULL || *s == NUL)
4176 {
4177 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004178 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004179 goto theend;
4180 }
4181 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004182 }
4183 else
4184 s = (char_u *)"";
4185
Bram Moolenaara80faa82020-04-12 19:37:17 +02004186 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004187 funcexe.fe_firstline = curwin->w_cursor.lnum;
4188 funcexe.fe_lastline = curwin->w_cursor.lnum;
4189 funcexe.fe_evaluate = evaluate;
4190 funcexe.fe_partial = pt;
4191 funcexe.fe_selfdict = selfdict;
4192 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004193 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004194
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004195theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004196 // Clear the funcref afterwards, so that deleting it while
4197 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004198 if (evaluate)
4199 clear_tv(&functv);
4200
4201 return ret;
4202}
4203
4204/*
4205 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004206 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004207 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4208 */
4209 static int
4210eval_lambda(
4211 char_u **arg,
4212 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004213 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004214 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004215{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004216 int evaluate = evalarg != NULL
4217 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004218 typval_T base = *rettv;
4219 int ret;
4220
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004221 rettv->v_type = VAR_UNKNOWN;
4222
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004223 if (**arg == '{')
4224 {
4225 // ->{lambda}()
4226 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4227 }
4228 else
4229 {
4230 // ->(lambda)()
4231 ++*arg;
4232 ret = eval1(arg, rettv, evalarg);
4233 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004234 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004235 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004236 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004237 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004238 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004239 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4240 && rettv->v_type != VAR_PARTIAL)
4241 {
4242 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4243 return FAIL;
4244 }
4245 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004246 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004247 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004248 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004249
4250 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004251 {
4252 if (verbose)
4253 {
4254 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004255 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004257 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004258 }
4259 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004260 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004261 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004262 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004263 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004264
4265 // Clear the funcref afterwards, so that deleting it while
4266 // evaluating the arguments is possible (see test55).
4267 if (evaluate)
4268 clear_tv(&base);
4269
4270 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004271}
4272
4273/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004274 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004275 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004276 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4277 */
4278 static int
4279eval_method(
4280 char_u **arg,
4281 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004282 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004283 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004284{
4285 char_u *name;
4286 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004287 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004288 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004289 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004290 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004291 int evaluate = evalarg != NULL
4292 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004293
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004294 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004295
Bram Moolenaarac92e252019-08-03 21:58:38 +02004296 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004297 len = get_name_len(arg, &alias, evaluate, TRUE);
4298 if (alias != NULL)
4299 name = alias;
4300
4301 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004302 {
4303 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004304 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004305 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004306 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004307 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004308 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004309 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004310
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004311 // If there is no "(" immediately following, but there is further on,
4312 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4313 // Does not handle anything where "(" is part of the expression.
4314 *arg = skipwhite(*arg);
4315
4316 if (**arg != '(' && alias == NULL
4317 && (paren = vim_strchr(*arg, '(')) != NULL)
4318 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004319 char_u *deref;
4320
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004321 *arg = name;
4322 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004323 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4324 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004325 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004326 *arg = name + len;
4327 ret = FAIL;
4328 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004329 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004330 {
4331 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004332 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004333 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004334 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004335 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004336
4337 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004338 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004339 *arg = skipwhite(*arg);
4340
4341 if (**arg != '(')
4342 {
4343 if (verbose)
4344 semsg(_(e_missing_parenthesis_str), name);
4345 ret = FAIL;
4346 }
4347 else if (VIM_ISWHITE((*arg)[-1]))
4348 {
4349 if (verbose)
4350 emsg(_(e_no_white_space_allowed_before_parenthesis));
4351 ret = FAIL;
4352 }
4353 else
4354 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004355 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004356 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004357 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004358
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004359 // Clear the funcref afterwards, so that deleting it while
4360 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004361 if (evaluate)
4362 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004363 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004364
Bram Moolenaarac92e252019-08-03 21:58:38 +02004365 return ret;
4366}
4367
4368/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004369 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4370 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004371 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4372 */
4373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004374eval_index(
4375 char_u **arg,
4376 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004377 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004378 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004380 int evaluate = evalarg != NULL
4381 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004382 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004386 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004387 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004388
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004389 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4390 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004391
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004392 init_tv(&var1);
4393 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004394 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004396 /*
4397 * dict.name
4398 */
4399 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004400 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004401 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004402 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004404 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004405 }
4406 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004408 /*
4409 * something[idx]
4410 *
4411 * Get the (first) variable from inside the [].
4412 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004413 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004414 if (**arg == ':')
4415 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004416 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004417 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004418 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004419 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004420 semsg(_(e_white_space_required_before_and_after_str_at_str),
4421 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004422 clear_tv(&var1);
4423 return FAIL;
4424 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004425 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004427 int error = FALSE;
4428
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004429#ifdef FEAT_FLOAT
4430 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004431 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004432 && var1.v_type == VAR_FLOAT)
4433 {
4434 var1.vval.v_string = typval_tostring(&var1, TRUE);
4435 var1.v_type = VAR_STRING;
4436 }
4437#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004438 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004439 tv_get_number_chk(&var1, &error);
4440 else
4441 error = tv_get_string_chk(&var1) == NULL;
4442 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004443 {
4444 // not a number or string
4445 clear_tv(&var1);
4446 return FAIL;
4447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004448 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449
4450 /*
4451 * Get the second variable from inside the [:].
4452 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004453 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 if (**arg == ':')
4455 {
4456 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004457 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004458 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004459 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004460 semsg(_(e_white_space_required_before_and_after_str_at_str),
4461 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004462 if (!empty1)
4463 clear_tv(&var1);
4464 return FAIL;
4465 }
4466 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 if (**arg == ']')
4468 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004469 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004471 if (!empty1)
4472 clear_tv(&var1);
4473 return FAIL;
4474 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004475 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004478 if (!empty1)
4479 clear_tv(&var1);
4480 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 return FAIL;
4482 }
4483 }
4484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004486 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487 if (**arg != ']')
4488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004489 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004490 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 clear_tv(&var1);
4492 if (range)
4493 clear_tv(&var2);
4494 return FAIL;
4495 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004496 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004497 }
4498
4499 if (evaluate)
4500 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004501 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004502 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004503 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004504
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004505 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004506 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004507 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004508 clear_tv(&var2);
4509 return res;
4510 }
4511 return OK;
4512}
4513
4514/*
4515 * Check if "rettv" can have an [index] or [sli:ce]
4516 */
4517 int
4518check_can_index(typval_T *rettv, int evaluate, int verbose)
4519{
4520 switch (rettv->v_type)
4521 {
4522 case VAR_FUNC:
4523 case VAR_PARTIAL:
4524 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004525 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004526 return FAIL;
4527 case VAR_FLOAT:
4528#ifdef FEAT_FLOAT
4529 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004530 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004531 return FAIL;
4532#endif
4533 case VAR_BOOL:
4534 case VAR_SPECIAL:
4535 case VAR_JOB:
4536 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004537 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004538 if (verbose)
4539 emsg(_(e_cannot_index_special_variable));
4540 return FAIL;
4541 case VAR_UNKNOWN:
4542 case VAR_ANY:
4543 case VAR_VOID:
4544 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004546 emsg(_(e_cannot_index_special_variable));
4547 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004549 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004551 case VAR_STRING:
4552 case VAR_LIST:
4553 case VAR_DICT:
4554 case VAR_BLOB:
4555 break;
4556 case VAR_NUMBER:
4557 if (in_vim9script())
4558 emsg(_(e_cannot_index_number));
4559 break;
4560 }
4561 return OK;
4562}
4563
4564/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004565 * slice() function
4566 */
4567 void
4568f_slice(typval_T *argvars, typval_T *rettv)
4569{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004570 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004571 && ((argvars[0].v_type != VAR_STRING
4572 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004573 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004574 && check_for_list_arg(argvars, 0) == FAIL)
4575 || check_for_number_arg(argvars, 1) == FAIL
4576 || check_for_opt_number_arg(argvars, 2) == FAIL))
4577 return;
4578
Bram Moolenaar6601b622021-01-13 21:47:15 +01004579 if (check_can_index(argvars, TRUE, FALSE) == OK)
4580 {
4581 copy_tv(argvars, rettv);
4582 eval_index_inner(rettv, TRUE, argvars + 1,
4583 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4584 TRUE, NULL, 0, FALSE);
4585 }
4586}
4587
4588/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004589 * Apply index or range to "rettv".
4590 * "var1" is the first index, NULL for [:expr].
4591 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004592 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4593 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004594 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4595 */
4596 int
4597eval_index_inner(
4598 typval_T *rettv,
4599 int is_range,
4600 typval_T *var1,
4601 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004602 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004603 char_u *key,
4604 int keylen,
4605 int verbose)
4606{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004607 varnumber_T n1, n2 = 0;
4608 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004609
4610 n1 = 0;
4611 if (var1 != NULL && rettv->v_type != VAR_DICT)
4612 n1 = tv_get_number(var1);
4613
4614 if (is_range)
4615 {
4616 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004618 if (verbose)
4619 emsg(_(e_cannot_slice_dictionary));
4620 return FAIL;
4621 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004622 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004624 else
4625 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004626 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004627
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004628 switch (rettv->v_type)
4629 {
4630 case VAR_UNKNOWN:
4631 case VAR_ANY:
4632 case VAR_VOID:
4633 case VAR_FUNC:
4634 case VAR_PARTIAL:
4635 case VAR_FLOAT:
4636 case VAR_BOOL:
4637 case VAR_SPECIAL:
4638 case VAR_JOB:
4639 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004640 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004641 break; // not evaluating, skipping over subscript
4642
4643 case VAR_NUMBER:
4644 case VAR_STRING:
4645 {
4646 char_u *s = tv_get_string(rettv);
4647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004649 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004650 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004651 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004652 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004653 else
4654 s = char_from_string(s, n1);
4655 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004656 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 // The resulting variable is a substring. If the indexes
4659 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 if (n1 < 0)
4661 {
4662 n1 = len + n1;
4663 if (n1 < 0)
4664 n1 = 0;
4665 }
4666 if (n2 < 0)
4667 n2 = len + n2;
4668 else if (n2 >= len)
4669 n2 = len;
4670 if (n1 >= len || n2 < 0 || n1 > n2)
4671 s = NULL;
4672 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004673 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675 else
4676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004677 // The resulting variable is a string of a single
4678 // character. If the index is too big or negative the
4679 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 if (n1 >= len || n1 < 0)
4681 s = NULL;
4682 else
4683 s = vim_strnsave(s + n1, 1);
4684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 clear_tv(rettv);
4686 rettv->v_type = VAR_STRING;
4687 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004688 }
4689 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004691 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004692 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4693 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004694 break;
4695
4696 case VAR_LIST:
4697 if (var1 == NULL)
4698 n1 = 0;
4699 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004700 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004701 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004702 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004703 return FAIL;
4704 break;
4705
4706 case VAR_DICT:
4707 {
4708 dictitem_T *item;
4709 typval_T tmp;
4710
4711 if (key == NULL)
4712 {
4713 key = tv_get_string_chk(var1);
4714 if (key == NULL)
4715 return FAIL;
4716 }
4717
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004718 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004719
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004720 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004721 {
4722 if (verbose)
4723 {
4724 if (keylen > 0)
4725 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004726 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004727 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004728 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004729 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730
4731 copy_tv(&item->di_tv, &tmp);
4732 clear_tv(rettv);
4733 *rettv = tmp;
4734 }
4735 break;
4736 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 return OK;
4738}
4739
4740/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004741 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004742 */
4743 char_u *
4744partial_name(partial_T *pt)
4745{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004746 if (pt != NULL)
4747 {
4748 if (pt->pt_name != NULL)
4749 return pt->pt_name;
4750 if (pt->pt_func != NULL)
4751 return pt->pt_func->uf_name;
4752 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004753 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004754}
4755
Bram Moolenaarddecc252016-04-06 22:59:37 +02004756 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004757partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004758{
4759 int i;
4760
4761 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004762 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004763 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004765 if (pt->pt_name != NULL)
4766 {
4767 func_unref(pt->pt_name);
4768 vim_free(pt->pt_name);
4769 }
4770 else
4771 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004772
Bram Moolenaar54656012021-06-09 20:50:46 +02004773 // "out_up" is no longer used, decrement refcount on partial that owns it.
4774 partial_unref(pt->pt_outer.out_up_partial);
4775
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004776 // Using pt_outer from another partial.
4777 partial_unref(pt->pt_outer_partial);
4778
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004779 // Decrease the reference count for the context of a closure. If down
4780 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004781 if (pt->pt_funcstack != NULL)
4782 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004783 --pt->pt_funcstack->fs_refcount;
4784 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004785 }
4786
Bram Moolenaarddecc252016-04-06 22:59:37 +02004787 vim_free(pt);
4788}
4789
4790/*
4791 * Unreference a closure: decrement the reference count and free it when it
4792 * becomes zero.
4793 */
4794 void
4795partial_unref(partial_T *pt)
4796{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004797 if (pt != NULL)
4798 {
4799 if (--pt->pt_refcount <= 0)
4800 partial_free(pt);
4801
4802 // If the reference count goes down to one, the funcstack may be the
4803 // only reference and can be freed if no other partials reference it.
4804 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4805 funcstack_check_refcount(pt->pt_funcstack);
4806 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004807}
4808
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004809/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004810 * Return the next (unique) copy ID.
4811 * Used for serializing nested structures.
4812 */
4813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004815{
4816 current_copyID += COPYID_INC;
4817 return current_copyID;
4818}
4819
4820/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004821 * Garbage collection for lists and dictionaries.
4822 *
4823 * We use reference counts to be able to free most items right away when they
4824 * are no longer used. But for composite items it's possible that it becomes
4825 * unused while the reference count is > 0: When there is a recursive
4826 * reference. Example:
4827 * :let l = [1, 2, 3]
4828 * :let d = {9: l}
4829 * :let l[1] = d
4830 *
4831 * Since this is quite unusual we handle this with garbage collection: every
4832 * once in a while find out which lists and dicts are not referenced from any
4833 * variable.
4834 *
4835 * Here is a good reference text about garbage collection (refers to Python
4836 * but it applies to all reference-counting mechanisms):
4837 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004838 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004839
4840/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004841 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004842 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004845 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004846garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004850 buf_T *buf;
4851 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004852 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004853 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004854
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004855 if (!testing)
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004858 want_garbage_collect = FALSE;
4859 may_garbage_collect = FALSE;
4860 garbage_collect_at_exit = FALSE;
4861 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004862
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004863 // The execution stack can grow big, limit the size.
4864 if (exestack.ga_maxlen - exestack.ga_len > 500)
4865 {
4866 size_t new_len;
4867 char_u *pp;
4868 int n;
4869
4870 // Keep 150% of the current size, with a minimum of the growth size.
4871 n = exestack.ga_len / 2;
4872 if (n < exestack.ga_growsize)
4873 n = exestack.ga_growsize;
4874
4875 // Don't make it bigger though.
4876 if (exestack.ga_len + n < exestack.ga_maxlen)
4877 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004878 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004879 pp = vim_realloc(exestack.ga_data, new_len);
4880 if (pp == NULL)
4881 return FAIL;
4882 exestack.ga_maxlen = exestack.ga_len + n;
4883 exestack.ga_data = pp;
4884 }
4885 }
4886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004887 // We advance by two because we add one for items referenced through
4888 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004889 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004891 /*
4892 * 1. Go through all accessible variables and mark all lists and dicts
4893 * with copyID.
4894 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004896 // Don't free variables in the previous_funccal list unless they are only
4897 // referenced through previous_funccal. This must be first, because if
4898 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004902 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004905 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004906 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4907 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004910 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004911 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4912 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004913 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004914 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4915 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004916#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004917 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004918 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4919 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004920 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004921 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004922 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4923 NULL, NULL);
4924#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004927 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004928 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4929 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004931 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004937 abort = abort || set_ref_in_functions(copyID);
4938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004940 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004941
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004942 // funcstacks keep variables for closures
4943 abort = abort || set_ref_in_funcstacks(copyID);
4944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004946 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004947
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004948 // callbacks in buffers
4949 abort = abort || set_ref_in_buffers(copyID);
4950
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004951 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4952 abort = abort || set_ref_in_insexpand_funcs(copyID);
4953
4954 // 'operatorfunc' callback
4955 abort = abort || set_ref_in_opfunc(copyID);
4956
4957 // 'tagfunc' callback
4958 abort = abort || set_ref_in_tagfunc(copyID);
4959
4960 // 'imactivatefunc' and 'imstatusfunc' callbacks
4961 abort = abort || set_ref_in_im_funcs(copyID);
4962
Bram Moolenaar1dced572012-04-05 16:54:08 +02004963#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004964 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004965#endif
4966
Bram Moolenaardb913952012-06-29 12:54:53 +02004967#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004968 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004969#endif
4970
4971#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004972 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004973#endif
4974
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004975#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004976 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004977 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004978#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004979#ifdef FEAT_NETBEANS_INTG
4980 abort = abort || set_ref_in_nb_channel(copyID);
4981#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004982
Bram Moolenaare3188e22016-05-31 21:13:04 +02004983#ifdef FEAT_TIMERS
4984 abort = abort || set_ref_in_timer(copyID);
4985#endif
4986
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004987#ifdef FEAT_QUICKFIX
4988 abort = abort || set_ref_in_quickfix(copyID);
4989#endif
4990
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004991#ifdef FEAT_TERMINAL
4992 abort = abort || set_ref_in_term(copyID);
4993#endif
4994
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004995#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004996 abort = abort || set_ref_in_popups(copyID);
4997#endif
4998
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005000 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005001 /*
5002 * 2. Free lists and dictionaries that are not referenced.
5003 */
5004 did_free = free_unref_items(copyID);
5005
5006 /*
5007 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005011 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005012 else if (p_verbose > 0)
5013 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005014 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005015 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005016
5017 return did_free;
5018}
5019
5020/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005021 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005022 */
5023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005024free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005025{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005026 int did_free = FALSE;
5027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // Let all "free" functions know that we are here. This means no
5029 // dictionaries, lists, channels or jobs are to be freed, because we will
5030 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005031 in_free_unref_items = TRUE;
5032
5033 /*
5034 * PASS 1: free the contents of the items. We don't free the items
5035 * themselves yet, so that it is possible to decrement refcount counters
5036 */
5037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005039 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005042 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005043
5044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // Go through the list of jobs and free items without the copyID. This
5046 // must happen before doing channels, because jobs refer to channels, but
5047 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005048 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005050 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005051 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5052#endif
5053
5054 /*
5055 * PASS 2: free the items themselves.
5056 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005057 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005058 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005059
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005060#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // Go through the list of jobs and free items without the copyID. This
5062 // must happen before doing channels, because jobs refer to channels, but
5063 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005064 free_unused_jobs(copyID, COPYID_MASK);
5065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005067 free_unused_channels(copyID, COPYID_MASK);
5068#endif
5069
5070 in_free_unref_items = FALSE;
5071
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005072 return did_free;
5073}
5074
5075/*
5076 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005077 * "list_stack" is used to add lists to be marked. Can be NULL.
5078 *
5079 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005080 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005083{
5084 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005085 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005086 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005087 hashtab_T *cur_ht;
5088 ht_stack_T *ht_stack = NULL;
5089 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005091 cur_ht = ht;
5092 for (;;)
5093 {
5094 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // Mark each item in the hashtab. If the item contains a hashtab
5097 // it is added to ht_stack, if it contains a list it is added to
5098 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005099 todo = (int)cur_ht->ht_used;
5100 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5101 if (!HASHITEM_EMPTY(hi))
5102 {
5103 --todo;
5104 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5105 &ht_stack, list_stack);
5106 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005107 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005108
5109 if (ht_stack == NULL)
5110 break;
5111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005112 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005113 cur_ht = ht_stack->ht;
5114 tempitem = ht_stack;
5115 ht_stack = ht_stack->prev;
5116 free(tempitem);
5117 }
5118
5119 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005120}
5121
Dominique Pelle748b3082022-01-08 12:41:16 +00005122#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5123 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005124/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005125 * Mark a dict and its items with "copyID".
5126 * Returns TRUE if setting references failed somehow.
5127 */
5128 int
5129set_ref_in_dict(dict_T *d, int copyID)
5130{
5131 if (d != NULL && d->dv_copyID != copyID)
5132 {
5133 d->dv_copyID = copyID;
5134 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5135 }
5136 return FALSE;
5137}
Dominique Pelle748b3082022-01-08 12:41:16 +00005138#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005139
5140/*
5141 * Mark a list and its items with "copyID".
5142 * Returns TRUE if setting references failed somehow.
5143 */
5144 int
5145set_ref_in_list(list_T *ll, int copyID)
5146{
5147 if (ll != NULL && ll->lv_copyID != copyID)
5148 {
5149 ll->lv_copyID = copyID;
5150 return set_ref_in_list_items(ll, copyID, NULL);
5151 }
5152 return FALSE;
5153}
5154
5155/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005156 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5158 *
5159 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005160 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005162set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005163{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005164 listitem_T *li;
5165 int abort = FALSE;
5166 list_T *cur_l;
5167 list_stack_T *list_stack = NULL;
5168 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005169
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 cur_l = l;
5171 for (;;)
5172 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005173 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005174 // Mark each item in the list. If the item contains a hashtab
5175 // it is added to ht_stack, if it contains a list it is added to
5176 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005177 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5178 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5179 ht_stack, &list_stack);
5180 if (list_stack == NULL)
5181 break;
5182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005184 cur_l = list_stack->list;
5185 tempitem = list_stack;
5186 list_stack = list_stack->prev;
5187 free(tempitem);
5188 }
5189
5190 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005191}
5192
5193/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005194 * Mark the partial in callback 'cb' with "copyID".
5195 */
5196 int
5197set_ref_in_callback(callback_T *cb, int copyID)
5198{
5199 typval_T tv;
5200
5201 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5202 return FALSE;
5203
5204 tv.v_type = VAR_PARTIAL;
5205 tv.vval.v_partial = cb->cb_partial;
5206 return set_ref_in_item(&tv, copyID, NULL, NULL);
5207}
5208
5209/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005210 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005211 * "list_stack" is used to add lists to be marked. Can be NULL.
5212 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5213 *
5214 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005217set_ref_in_item(
5218 typval_T *tv,
5219 int copyID,
5220 ht_stack_T **ht_stack,
5221 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005222{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005223 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005224
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005225 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005227 dict_T *dd = tv->vval.v_dict;
5228
Bram Moolenaara03f2332016-02-06 18:09:59 +01005229 if (dd != NULL && dd->dv_copyID != copyID)
5230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005232 dd->dv_copyID = copyID;
5233 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005234 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005235 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5236 }
5237 else
5238 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005239 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5240
Bram Moolenaara03f2332016-02-06 18:09:59 +01005241 if (newitem == NULL)
5242 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005243 else
5244 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005245 newitem->ht = &dd->dv_hashtab;
5246 newitem->prev = *ht_stack;
5247 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005249 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005250 }
5251 }
5252 else if (tv->v_type == VAR_LIST)
5253 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005254 list_T *ll = tv->vval.v_list;
5255
Bram Moolenaara03f2332016-02-06 18:09:59 +01005256 if (ll != NULL && ll->lv_copyID != copyID)
5257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005259 ll->lv_copyID = copyID;
5260 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005261 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005262 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005263 }
5264 else
5265 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005266 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5267
Bram Moolenaara03f2332016-02-06 18:09:59 +01005268 if (newitem == NULL)
5269 abort = TRUE;
5270 else
5271 {
5272 newitem->list = ll;
5273 newitem->prev = *list_stack;
5274 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005275 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005276 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005277 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005278 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005279 else if (tv->v_type == VAR_FUNC)
5280 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005281 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005282 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005283 else if (tv->v_type == VAR_PARTIAL)
5284 {
5285 partial_T *pt = tv->vval.v_partial;
5286 int i;
5287
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005288 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005289 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005290 // Didn't see this partial yet.
5291 pt->pt_copyID = copyID;
5292
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005293 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005294
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005295 if (pt->pt_dict != NULL)
5296 {
5297 typval_T dtv;
5298
5299 dtv.v_type = VAR_DICT;
5300 dtv.vval.v_dict = pt->pt_dict;
5301 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5302 }
5303
5304 for (i = 0; i < pt->pt_argc; ++i)
5305 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5306 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005307 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005308 }
5309 }
5310#ifdef FEAT_JOB_CHANNEL
5311 else if (tv->v_type == VAR_JOB)
5312 {
5313 job_T *job = tv->vval.v_job;
5314 typval_T dtv;
5315
5316 if (job != NULL && job->jv_copyID != copyID)
5317 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005318 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005319 if (job->jv_channel != NULL)
5320 {
5321 dtv.v_type = VAR_CHANNEL;
5322 dtv.vval.v_channel = job->jv_channel;
5323 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5324 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005325 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005326 {
5327 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005328 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005329 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5330 }
5331 }
5332 }
5333 else if (tv->v_type == VAR_CHANNEL)
5334 {
5335 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005336 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005337 typval_T dtv;
5338 jsonq_T *jq;
5339 cbq_T *cq;
5340
5341 if (ch != NULL && ch->ch_copyID != copyID)
5342 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005343 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005344 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 {
5346 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5347 jq = jq->jq_next)
5348 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5349 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5350 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005351 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005352 {
5353 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005354 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5356 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005357 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 {
5359 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005360 dtv.vval.v_partial =
5361 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5363 }
5364 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005365 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 {
5367 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005368 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5370 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005371 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005372 {
5373 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005374 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005375 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5376 }
5377 }
5378 }
5379#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005380 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005381}
5382
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 * Return a string with the string representation of a variable.
5385 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005387 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005388 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005389 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005390 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005391 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5392 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005393 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005395 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005396echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005397 typval_T *tv,
5398 char_u **tofree,
5399 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005400 int copyID,
5401 int echo_style,
5402 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005403 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005405 static int recurse = 0;
5406 char_u *r = NULL;
5407
Bram Moolenaar33570922005-01-25 22:26:29 +00005408 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005409 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005410 if (!did_echo_string_emsg)
5411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005412 // Only give this message once for a recursive call to avoid
5413 // flooding the user with errors. And stop iterating over lists
5414 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005415 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005416 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005418 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005419 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005420 }
5421 ++recurse;
5422
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 switch (tv->v_type)
5424 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005425 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005426 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005427 {
5428 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005429 r = tv->vval.v_string;
5430 if (r == NULL)
5431 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005432 }
5433 else
5434 {
5435 *tofree = string_quote(tv->vval.v_string, FALSE);
5436 r = *tofree;
5437 }
5438 break;
5439
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005441 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005442 char_u buf[MAX_FUNC_NAME_LEN];
5443
5444 if (echo_style)
5445 {
LemonBoya5d35902022-04-29 21:15:02 +01005446 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5447 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005448 buf, MAX_FUNC_NAME_LEN);
5449 if (r == buf)
5450 {
5451 r = vim_strsave(buf);
5452 *tofree = r;
5453 }
5454 else
5455 *tofree = NULL;
5456 }
5457 else
5458 {
5459 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5460 : make_ufunc_name_readable(
5461 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5462 TRUE);
5463 r = *tofree;
5464 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005466 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005467
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005468 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005469 {
5470 partial_T *pt = tv->vval.v_partial;
5471 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005472 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005473 garray_T ga;
5474 int i;
5475 char_u *tf;
5476
5477 ga_init2(&ga, 1, 100);
5478 ga_concat(&ga, (char_u *)"function(");
5479 if (fname != NULL)
5480 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005481 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005482 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005483 && vim_isupper(fname[1]))
5484 {
5485 ga_concat(&ga, (char_u *)"'g:");
5486 ga_concat(&ga, fname + 1);
5487 }
5488 else
5489 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005490 vim_free(fname);
5491 }
5492 if (pt != NULL && pt->pt_argc > 0)
5493 {
5494 ga_concat(&ga, (char_u *)", [");
5495 for (i = 0; i < pt->pt_argc; ++i)
5496 {
5497 if (i > 0)
5498 ga_concat(&ga, (char_u *)", ");
5499 ga_concat(&ga,
5500 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5501 vim_free(tf);
5502 }
5503 ga_concat(&ga, (char_u *)"]");
5504 }
5505 if (pt != NULL && pt->pt_dict != NULL)
5506 {
5507 typval_T dtv;
5508
5509 ga_concat(&ga, (char_u *)", ");
5510 dtv.v_type = VAR_DICT;
5511 dtv.vval.v_dict = pt->pt_dict;
5512 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5513 vim_free(tf);
5514 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005515 // terminate with ')' and a NUL
5516 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005517
5518 *tofree = ga.ga_data;
5519 r = *tofree;
5520 break;
5521 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005522
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005523 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005524 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005525 break;
5526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005528 if (tv->vval.v_list == NULL)
5529 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005530 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005531 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005532 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005533 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005534 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5535 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005536 {
5537 *tofree = NULL;
5538 r = (char_u *)"[...]";
5539 }
5540 else
5541 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005542 int old_copyID = tv->vval.v_list->lv_copyID;
5543
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005544 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005545 *tofree = list2string(tv, copyID, restore_copyID);
5546 if (restore_copyID)
5547 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005548 r = *tofree;
5549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005550 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005551
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005553 if (tv->vval.v_dict == NULL)
5554 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005555 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005556 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005557 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005558 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005559 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5560 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005561 {
5562 *tofree = NULL;
5563 r = (char_u *)"{...}";
5564 }
5565 else
5566 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005567 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005568
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005569 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005570 *tofree = dict2string(tv, copyID, restore_copyID);
5571 if (restore_copyID)
5572 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005573 r = *tofree;
5574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005575 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005578 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005579 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005581 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005583 break;
5584
Bram Moolenaar835dc632016-02-07 14:27:38 +01005585 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005586 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005588 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005589 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5590 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005591 if (composite_val)
5592 {
5593 *tofree = string_quote(r, FALSE);
5594 r = *tofree;
5595 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005596#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005599 case VAR_INSTR:
5600 *tofree = NULL;
5601 r = (char_u *)"instructions";
5602 break;
5603
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005604 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005605#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005606 *tofree = NULL;
5607 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5608 r = numbuf;
5609 break;
5610#endif
5611
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005612 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005613 case VAR_SPECIAL:
5614 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005615 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005616 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005618
Bram Moolenaar8502c702014-06-17 12:51:16 +02005619 if (--recurse == 0)
5620 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005621 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005622}
5623
5624/*
5625 * Return a string with the string representation of a variable.
5626 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5627 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005628 * Does not put quotes around strings, as ":echo" displays values.
5629 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5630 * May return NULL.
5631 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005632 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005633echo_string(
5634 typval_T *tv,
5635 char_u **tofree,
5636 char_u *numbuf,
5637 int copyID)
5638{
5639 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5640}
5641
5642/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005643 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5644 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005645 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005646 */
5647 int
5648buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5649{
5650 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005651 char_u *t;
5652 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005653
5654 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5655 return -1;
5656
5657 if (lnum > buf->b_ml.ml_line_count)
5658 lnum = buf->b_ml.ml_line_count;
5659
5660 str = ml_get_buf(buf, lnum, FALSE);
5661 if (str == NULL)
5662 return -1;
5663
5664 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005665 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005666
Bram Moolenaar91458462021-01-13 20:08:38 +01005667 // count the number of characters
5668 t = str;
5669 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5670 t += mb_ptr2len(t);
5671
5672 // In insert mode, when the cursor is at the end of a non-empty line,
5673 // byteidx points to the NUL character immediately past the end of the
5674 // string. In this case, add one to the character count.
5675 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5676 count++;
5677
5678 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005679}
5680
5681/*
5682 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005683 * byte index. Works only for loaded buffers. Returns -1 on failure.
5684 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005685 */
5686 int
5687buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5688{
5689 char_u *str;
5690 char_u *t;
5691
5692 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5693 return -1;
5694
5695 if (lnum > buf->b_ml.ml_line_count)
5696 lnum = buf->b_ml.ml_line_count;
5697
5698 str = ml_get_buf(buf, lnum, FALSE);
5699 if (str == NULL)
5700 return -1;
5701
5702 // Convert the character offset to a byte offset
5703 t = str;
5704 while (*t != NUL && --charidx > 0)
5705 t += mb_ptr2len(t);
5706
Bram Moolenaar91458462021-01-13 20:08:38 +01005707 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005708}
5709
5710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005712 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715var2fpos(
5716 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005718 int *fnum, // set to fnum for '0, 'A, etc.
5719 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005721 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005723 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005725 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005726 if (varp->v_type == VAR_LIST)
5727 {
5728 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005729 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005730 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005731 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005732
5733 l = varp->vval.v_list;
5734 if (l == NULL)
5735 return NULL;
5736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005737 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005738 pos.lnum = list_find_nr(l, 0L, &error);
5739 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005740 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005741 if (charcol)
5742 len = (long)mb_charlen(ml_get(pos.lnum));
5743 else
5744 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005745
Bram Moolenaarec65d772020-08-20 22:29:12 +02005746 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005748 li = list_find(l, 1L);
5749 if (li != NULL && li->li_tv.v_type == VAR_STRING
5750 && li->li_tv.vval.v_string != NULL
5751 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005752 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005753 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005754 }
5755 else
5756 {
5757 pos.col = list_find_nr(l, 1L, &error);
5758 if (error)
5759 return NULL;
5760 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005762 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005763 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005764 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005765 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005767 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005768 pos.coladd = list_find_nr(l, 2L, &error);
5769 if (error)
5770 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005771
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005772 return &pos;
5773 }
5774
Bram Moolenaarc5809432021-03-27 21:23:30 +01005775 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5776 return NULL;
5777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005778 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005779 if (name == NULL)
5780 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005781
5782 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005783 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005784 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005785 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005786 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005787 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005788 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005789 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005790 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005791 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005792 pos = VIsual;
5793 else
5794 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005795 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005796 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005797 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005799 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005800 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5802 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005803 pos = *pp;
5804 }
5805 if (pos.lnum != 0)
5806 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005807 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005808 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5809 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005811
Bram Moolenaara5525202006-03-02 22:52:09 +00005812 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005813
Bram Moolenaar477933c2007-07-17 14:32:23 +00005814 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005815 {
5816 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005818 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005819 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // In silent Ex mode topline is zero, but that's not a valid line
5821 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005822 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005823 return &pos;
5824 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005826 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005827 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005829 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005830 return &pos;
5831 }
5832 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005835 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
5837 pos.lnum = curbuf->b_ml.ml_line_count;
5838 pos.col = 0;
5839 }
5840 else
5841 {
5842 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005843 if (charcol)
5844 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5845 else
5846 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
5848 return &pos;
5849 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005850 if (in_vim9script())
5851 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 return NULL;
5853}
5854
5855/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005856 * Convert list in "arg" into a position and optional file number.
5857 * When "fnump" is NULL there is no file number, only 3 items.
5858 * Note that the column is passed on as-is, the caller may want to decrement
5859 * it to use 1 for the first column.
5860 * Return FAIL when conversion is not possible, doesn't check the position for
5861 * validity.
5862 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864list2fpos(
5865 typval_T *arg,
5866 pos_T *posp,
5867 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005868 colnr_T *curswantp,
5869 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005870{
5871 list_T *l = arg->vval.v_list;
5872 long i = 0;
5873 long n;
5874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005875 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5876 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005877 if (arg->v_type != VAR_LIST
5878 || l == NULL
5879 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005880 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005881 return FAIL;
5882
5883 if (fnump != NULL)
5884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005885 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005886 if (n < 0)
5887 return FAIL;
5888 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005890 *fnump = n;
5891 }
5892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005894 if (n < 0)
5895 return FAIL;
5896 posp->lnum = n;
5897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005899 if (n < 0)
5900 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005901 // If character position is specified, then convert to byte position
5902 if (charcol)
5903 {
5904 buf_T *buf;
5905
5906 // Get the text for the specified line in a loaded buffer
5907 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5908 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5909 return FAIL;
5910
Bram Moolenaar91458462021-01-13 20:08:38 +01005911 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005912 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005913 posp->col = n;
5914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005915 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005916 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005917 posp->coladd = 0;
5918 else
5919 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005920
Bram Moolenaar493c1782014-05-28 14:34:46 +02005921 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005923
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005924 return OK;
5925}
5926
5927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 * Get the length of an environment variable name.
5929 * Advance "arg" to the first character after the name.
5930 * Return 0 for error.
5931 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005932 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005933get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 char_u *p;
5936 int len;
5937
5938 for (p = *arg; vim_isIDc(*p); ++p)
5939 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 return 0;
5942
5943 len = (int)(p - *arg);
5944 *arg = p;
5945 return len;
5946}
5947
5948/*
5949 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005950 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 * Return 0 if something is wrong.
5952 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005954get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 char_u *p;
5957 int len;
5958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005961 {
5962 if (*p == ':')
5963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // "s:" is start of "s:var", but "n:" is not and can be used in
5965 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005966 len = (int)(p - *arg);
5967 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5968 || len > 1)
5969 break;
5970 }
5971 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005972 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 return 0;
5974
5975 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005976 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 return len;
5979}
5980
5981/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005982 * Get the length of the name of a variable or function.
5983 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005985 * Return -1 if curly braces expansion failed.
5986 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 * If the name contains 'magic' {}'s, expand them and return the
5988 * expanded name in an allocated string via 'alias' - caller must free.
5989 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991get_name_len(
5992 char_u **arg,
5993 char_u **alias,
5994 int evaluate,
5995 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 char_u *p;
5999 char_u *expr_start;
6000 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006002 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6005 && (*arg)[2] == (int)KE_SNR)
6006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 *arg += 3;
6009 return get_id_len(arg) + 3;
6010 }
6011 len = eval_fname_script(*arg);
6012 if (len > 0)
6013 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006014 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 *arg += len;
6016 }
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006019 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006021 p = find_name_end(*arg, &expr_start, &expr_end,
6022 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 if (expr_start != NULL)
6024 {
6025 char_u *temp_string;
6026
6027 if (!evaluate)
6028 {
6029 len += (int)(p - *arg);
6030 *arg = skipwhite(p);
6031 return len;
6032 }
6033
6034 /*
6035 * Include any <SID> etc in the expanded string:
6036 * Thus the -len here.
6037 */
6038 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6039 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006040 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 *alias = temp_string;
6042 *arg = skipwhite(p);
6043 return (int)STRLEN(temp_string);
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006047 // Only give an error when there is something, otherwise it will be
6048 // reported at a higher level.
6049 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006050 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
6052 return len;
6053}
6054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006055/*
6056 * Find the end of a variable or function name, taking care of magic braces.
6057 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6058 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006059 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006060 * Return a pointer to just after the name. Equal to "arg" if there is no
6061 * valid name.
6062 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064find_name_end(
6065 char_u *arg,
6066 char_u **expr_start,
6067 char_u **expr_end,
6068 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 int mb_nest = 0;
6071 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006073 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006074 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006076 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006078 *expr_start = NULL;
6079 *expr_end = NULL;
6080 }
6081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006083 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6084 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006085 return arg;
6086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 for (p = arg; *p != NUL
6088 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006089 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006090 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006091 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006092 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006093 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006094 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006095 if (*p == '\'')
6096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006098 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006099 ;
6100 if (*p == NUL)
6101 break;
6102 }
6103 else if (*p == '"')
6104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006105 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006106 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006107 if (*p == '\\' && p[1] != NUL)
6108 ++p;
6109 if (*p == NUL)
6110 break;
6111 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006112 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 // "s:" is start of "s:var", but "n:" is not and can be used in
6115 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006116 len = (int)(p - arg);
6117 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006118 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006119 break;
6120 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006121
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006122 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006124 if (*p == '[')
6125 ++br_nest;
6126 else if (*p == ']')
6127 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006129
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006130 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006132 if (*p == '{')
6133 {
6134 mb_nest++;
6135 if (expr_start != NULL && *expr_start == NULL)
6136 *expr_start = p;
6137 }
6138 else if (*p == '}')
6139 {
6140 mb_nest--;
6141 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6142 *expr_end = p;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146
6147 return p;
6148}
6149
6150/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006151 * Expands out the 'magic' {}'s in a variable/function name.
6152 * Note that this can call itself recursively, to deal with
6153 * constructs like foo{bar}{baz}{bam}
6154 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6155 * "in_start" ^
6156 * "expr_start" ^
6157 * "expr_end" ^
6158 * "in_end" ^
6159 *
6160 * Returns a new allocated string, which the caller must free.
6161 * Returns NULL for failure.
6162 */
6163 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006164make_expanded_name(
6165 char_u *in_start,
6166 char_u *expr_start,
6167 char_u *expr_end,
6168 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006169{
6170 char_u c1;
6171 char_u *retval = NULL;
6172 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006173
6174 if (expr_end == NULL || in_end == NULL)
6175 return NULL;
6176 *expr_start = NUL;
6177 *expr_end = NUL;
6178 c1 = *in_end;
6179 *in_end = NUL;
6180
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006181 temp_result = eval_to_string(expr_start + 1, FALSE);
6182 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006183 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006184 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6185 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006186 if (retval != NULL)
6187 {
6188 STRCPY(retval, in_start);
6189 STRCAT(retval, temp_result);
6190 STRCAT(retval, expr_end + 1);
6191 }
6192 }
6193 vim_free(temp_result);
6194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006196 *expr_start = '{';
6197 *expr_end = '}';
6198
6199 if (retval != NULL)
6200 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006201 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006202 if (expr_start != NULL)
6203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006204 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006205 temp_result = make_expanded_name(retval, expr_start,
6206 expr_end, temp_result);
6207 vim_free(retval);
6208 retval = temp_result;
6209 }
6210 }
6211
6212 return retval;
6213}
6214
6215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006220eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006222 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006223}
6224
6225/*
6226 * Return TRUE if character "c" can be used as the first character in a
6227 * variable or function name (excluding '{' and '}').
6228 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006231{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006232 return ASCII_ISALPHA(c) || c == '_';
6233}
6234
6235/*
6236 * Return TRUE if character "c" can be used as the first character of a
6237 * dictionary key.
6238 */
6239 int
6240eval_isdictc(int c)
6241{
6242 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243}
6244
6245/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006246 * Handle:
6247 * - expr[expr], expr[expr:expr] subscript
6248 * - ".name" lookup
6249 * - function call with Funcref variable: func(expr)
6250 * - method call: var->method()
6251 *
6252 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006253 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006254 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006256handle_subscript(
6257 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006258 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006259 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006260 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006261 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006262{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006263 int evaluate = evalarg != NULL
6264 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006265 int ret = OK;
6266 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006267 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006268 int getnext;
6269 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006270
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006271 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006272 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006273 // When at the end of the line and ".name" or "->{" or "->X" follows in
6274 // the next line then consume the line break.
6275 p = eval_next_non_blank(*arg, evalarg, &getnext);
6276 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006277 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006278 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6279 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6280 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006281 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006282 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006283 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006284 check_white = FALSE;
6285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006286
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006287 if (rettv->v_type == VAR_ANY)
6288 {
6289 char_u *exp_name;
6290 int cc;
6291 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006292 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006293 type_T *type;
6294
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006295 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006296 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006297 if (**arg != '.')
6298 {
6299 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006300 semsg(_(e_expected_dot_after_name_str),
6301 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006302 ret = FAIL;
6303 break;
6304 }
6305 ++*arg;
6306 if (IS_WHITE_OR_NUL(**arg))
6307 {
6308 if (verbose)
6309 emsg(_(e_no_white_space_allowed_after_dot));
6310 ret = FAIL;
6311 break;
6312 }
6313
6314 // isolate the name
6315 exp_name = *arg;
6316 while (eval_isnamec(**arg))
6317 ++*arg;
6318 cc = **arg;
6319 **arg = NUL;
6320
6321 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006322 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006323 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006324
6325 if (idx < 0 && ufunc == NULL)
6326 {
6327 ret = FAIL;
6328 break;
6329 }
6330 if (idx >= 0)
6331 {
6332 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6333 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6334
6335 copy_tv(sv->sv_tv, rettv);
6336 }
6337 else
6338 {
6339 rettv->v_type = VAR_FUNC;
6340 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6341 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006342 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006343 }
6344
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006345 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6346 || rettv->v_type == VAR_PARTIAL))
6347 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006348 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006349 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6350 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006351
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006352 // Stop the expression evaluation when immediately aborting on
6353 // error, or when an interrupt occurred or an exception was thrown
6354 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006355 if (aborting())
6356 {
6357 if (ret == OK)
6358 clear_tv(rettv);
6359 ret = FAIL;
6360 }
6361 dict_unref(selfdict);
6362 selfdict = NULL;
6363 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006364 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006365 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006366 if (in_vim9script())
6367 *arg = skipwhite(p + 2);
6368 else
6369 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006370 if (ret == OK)
6371 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006372 if (VIM_ISWHITE(**arg))
6373 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006374 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006375 ret = FAIL;
6376 }
6377 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006378 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006379 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006380 else
6381 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006382 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006383 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006384 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006385 // "." is ".name" lookup when we found a dict or when evaluating and
6386 // scriptversion is at least 2, where string concatenation is "..".
6387 else if (**arg == '['
6388 || (**arg == '.' && (rettv->v_type == VAR_DICT
6389 || (!evaluate
6390 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006391 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006392 {
6393 dict_unref(selfdict);
6394 if (rettv->v_type == VAR_DICT)
6395 {
6396 selfdict = rettv->vval.v_dict;
6397 if (selfdict != NULL)
6398 ++selfdict->dv_refcount;
6399 }
6400 else
6401 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006402 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006403 {
6404 clear_tv(rettv);
6405 ret = FAIL;
6406 }
6407 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006408 else
6409 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006410 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006412 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6413 // Don't do this when "Func" is already a partial that was bound
6414 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006415 if (selfdict != NULL
6416 && (rettv->v_type == VAR_FUNC
6417 || (rettv->v_type == VAR_PARTIAL
6418 && (rettv->vval.v_partial->pt_auto
6419 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006420 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006422 dict_unref(selfdict);
6423 return ret;
6424}
6425
6426/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427 * Make a copy of an item.
6428 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006429 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006430 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6431 * reference to an already copied list/dict can be used.
6432 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006435item_copy(
6436 typval_T *from,
6437 typval_T *to,
6438 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006439 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441{
6442 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006443 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006446 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006447 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006448 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006449 }
6450 ++recurse;
6451
6452 switch (from->v_type)
6453 {
6454 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006455 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006456 case VAR_STRING:
6457 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006458 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006459 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006460 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006461 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006462 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006463 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464 copy_tv(from, to);
6465 break;
6466 case VAR_LIST:
6467 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006468 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 if (from->vval.v_list == NULL)
6470 to->vval.v_list = NULL;
6471 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 to->vval.v_list = from->vval.v_list->lv_copylist;
6475 ++to->vval.v_list->lv_refcount;
6476 }
6477 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006478 to->vval.v_list = list_copy(from->vval.v_list,
6479 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006480 if (to->vval.v_list == NULL)
6481 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006483 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006485 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 case VAR_DICT:
6487 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006488 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 if (from->vval.v_dict == NULL)
6490 to->vval.v_dict = NULL;
6491 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006494 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6495 ++to->vval.v_dict->dv_refcount;
6496 }
6497 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006498 to->vval.v_dict = dict_copy(from->vval.v_dict,
6499 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 if (to->vval.v_dict == NULL)
6501 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006503 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006504 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006506 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508 }
6509 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511}
6512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006513 void
6514echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6515{
6516 char_u *tofree;
6517 char_u numbuf[NUMBUFLEN];
6518 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6519
6520 if (*atstart)
6521 {
6522 *atstart = FALSE;
6523 // Call msg_start() after eval1(), evaluating the expression
6524 // may cause a message to appear.
6525 if (with_space)
6526 {
6527 // Mark the saved text as finishing the line, so that what
6528 // follows is displayed on a new line when scrolling back
6529 // at the more prompt.
6530 msg_sb_eol();
6531 msg_start();
6532 }
6533 }
6534 else if (with_space)
6535 msg_puts_attr(" ", echo_attr);
6536
6537 if (p != NULL)
6538 for ( ; *p != NUL && !got_int; ++p)
6539 {
6540 if (*p == '\n' || *p == '\r' || *p == TAB)
6541 {
6542 if (*p != TAB && *needclr)
6543 {
6544 // remove any text still there from the command
6545 msg_clr_eos();
6546 *needclr = FALSE;
6547 }
6548 msg_putchar_attr(*p, echo_attr);
6549 }
6550 else
6551 {
6552 if (has_mbyte)
6553 {
6554 int i = (*mb_ptr2len)(p);
6555
6556 (void)msg_outtrans_len_attr(p, i, echo_attr);
6557 p += i - 1;
6558 }
6559 else
6560 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6561 }
6562 }
6563 vim_free(tofree);
6564}
6565
Bram Moolenaare9a41262005-01-15 22:18:47 +00006566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 * ":echo expr1 ..." print each argument separated with a space, add a
6568 * newline at the end.
6569 * ":echon expr1 ..." print each argument plain.
6570 */
6571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006576 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 int needclr = TRUE;
6578 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006579 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006580 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006581 evalarg_T evalarg;
6582
Bram Moolenaare707c882020-07-01 13:07:37 +02006583 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 if (eap->skip)
6586 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006587 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006589 // If eval1() causes an error message the text from the command may
6590 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006591 need_clr_eos = needclr;
6592
Bram Moolenaar68db9962021-05-09 23:19:22 +02006593 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006594 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
6596 /*
6597 * Report the invalid expression unless the expression evaluation
6598 * has been cancelled due to an aborting error, an interrupt, or an
6599 * exception.
6600 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006601 if (!aborting() && did_emsg == did_emsg_before
6602 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006603 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006604 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 break;
6606 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006607 need_clr_eos = FALSE;
6608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006610 {
6611 if (rettv.v_type == VAR_VOID)
6612 {
6613 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6614 break;
6615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006617 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 arg = skipwhite(arg);
6621 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006622 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006623 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 if (eap->skip)
6626 --emsg_skip;
6627 else
6628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006629 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (needclr)
6631 msg_clr_eos();
6632 if (eap->cmdidx == CMD_echo)
6633 msg_end();
6634 }
6635}
6636
6637/*
6638 * ":echohl {name}".
6639 */
6640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006643 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006647 * Returns the :echo attribute
6648 */
6649 int
6650get_echo_attr(void)
6651{
6652 return echo_attr;
6653}
6654
6655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 * ":execute expr1 ..." execute the result of an expression.
6657 * ":echomsg expr1 ..." Print a message
6658 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006659 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 * Each gets spaces around each argument and a newline at the end for
6661 * echo commands
6662 */
6663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006664ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 int ret = OK;
6669 char_u *p;
6670 garray_T ga;
6671 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006672 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 ga_init2(&ga, 1, 80);
6675
6676 if (eap->skip)
6677 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006678 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006680 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006681 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 if (!eap->skip)
6685 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006686 char_u buf[NUMBUFLEN];
6687
6688 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006689 {
6690 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6691 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006692 semsg(_(e_using_invalid_value_as_string_str),
6693 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006694 p = NULL;
6695 }
6696 else
6697 p = tv_get_string_buf(&rettv, buf);
6698 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006699 else
6700 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006701 if (p == NULL)
6702 {
6703 clear_tv(&rettv);
6704 ret = FAIL;
6705 break;
6706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 len = (int)STRLEN(p);
6708 if (ga_grow(&ga, len + 2) == FAIL)
6709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 ret = FAIL;
6712 break;
6713 }
6714 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 ga.ga_len += len;
6718 }
6719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006720 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 arg = skipwhite(arg);
6722 }
6723
6724 if (ret != FAIL && ga.ga_data != NULL)
6725 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006726 // use the first line of continuation lines for messages
6727 SOURCING_LNUM = start_lnum;
6728
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006729 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006731 // Mark the already saved text as finishing the line, so that what
6732 // follows is displayed on a new line when scrolling back at the
6733 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006734 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006735 }
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006738 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006739 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006740 out_flush();
6741 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006742 else if (eap->cmdidx == CMD_echoconsole)
6743 {
6744 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6745 ui_write((char_u *)"\r\n", 2, TRUE);
6746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 else if (eap->cmdidx == CMD_echoerr)
6748 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006749 int save_did_emsg = did_emsg;
6750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006751 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006752 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (!force_abort)
6754 did_emsg = save_did_emsg;
6755 }
6756 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006757 {
6758 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6759
6760 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6761 sticky_cmdmod_flags = cmdmod.cmod_flags
6762 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 do_cmdline((char_u *)ga.ga_data,
6764 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006765 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
6768
6769 ga_clear(&ga);
6770
6771 if (eap->skip)
6772 --emsg_skip;
6773
Bram Moolenaar63b91732021-08-05 20:40:03 +02006774 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776
6777/*
6778 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6779 * "arg" points to the "&" or '+' when called, to "option" when returning.
6780 * Returns NULL when no option name found. Otherwise pointer to the char
6781 * after the option name.
6782 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006783 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006784find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 char_u *p = *arg;
6787
6788 ++p;
6789 if (*p == 'g' && p[1] == ':')
6790 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006791 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 p += 2;
6793 }
6794 else if (*p == 'l' && p[1] == ':')
6795 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006796 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 p += 2;
6798 }
6799 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006800 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
6802 if (!ASCII_ISALPHA(*p))
6803 return NULL;
6804 *arg = p;
6805
6806 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006807 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 else
6809 while (ASCII_ISALPHA(*p))
6810 ++p;
6811 return p;
6812}
6813
6814/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006815 * Display script name where an item was last set.
6816 * Should only be invoked when 'verbose' is non-zero.
6817 */
6818 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006819last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006820{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006821 char_u *p;
6822
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006823 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006824 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006825 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006826 if (p != NULL)
6827 {
6828 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006829 msg_puts(_("\n\tLast set from "));
6830 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006831 if (script_ctx.sc_lnum > 0)
6832 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006833 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006834 msg_outnum((long)script_ctx.sc_lnum);
6835 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006836 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006837 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006838 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006839 }
6840}
6841
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006842#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
6844/*
6845 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006846 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 * "flags" can be "g" to do a global substitute.
6848 * Returns an allocated string, NULL for error.
6849 */
6850 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851do_string_sub(
6852 char_u *str,
6853 char_u *pat,
6854 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006855 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 int sublen;
6859 regmatch_T regmatch;
6860 int i;
6861 int do_all;
6862 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006863 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 garray_T ga;
6865 char_u *ret;
6866 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006867 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006869 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006871 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873 ga_init2(&ga, 1, 200);
6874
6875 do_all = (flags[0] == 'g');
6876
6877 regmatch.rm_ic = p_ic;
6878 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6879 if (regmatch.regprog != NULL)
6880 {
6881 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006882 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006885 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006886 if (regmatch.startp[0] == regmatch.endp[0])
6887 {
6888 if (zero_width == regmatch.startp[0])
6889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006890 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006891 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006892 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6893 (size_t)i);
6894 ga.ga_len += i;
6895 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006896 continue;
6897 }
6898 zero_width = regmatch.startp[0];
6899 }
6900
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 /*
6902 * Get some space for a temporary buffer to do the substitution
6903 * into. It will contain:
6904 * - The text up to where the match is.
6905 * - The substituted text.
6906 * - The text after the match.
6907 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006908 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006909 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6911 {
6912 ga_clear(&ga);
6913 break;
6914 }
6915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006916 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 i = (int)(regmatch.startp[0] - tail);
6918 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006920 (void)vim_regsub(&regmatch, sub, expr,
6921 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6922 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006924 tail = regmatch.endp[0];
6925 if (*tail == NUL)
6926 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (!do_all)
6928 break;
6929 }
6930
6931 if (ga.ga_data != NULL)
6932 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6933
Bram Moolenaar473de612013-06-08 18:19:48 +02006934 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
6937 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6938 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006939 if (p_cpo == empty_option)
6940 p_cpo = save_cpo;
6941 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006944 // If it's still empty it was changed and restored, need to restore in
6945 // the complicated way.
6946 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006947 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006948 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
6951 return ret;
6952}