blob: 5ec5b8ae7b7336301db88452d1492de24a72ed10 [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 Moolenaar58779852022-09-06 18:31:14 +0100266 // FIXME: should create a funccal and link it in current_funccal.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200267 if (call_def_function(partial->pt_func, argc, argv,
Bram Moolenaar58779852022-09-06 18:31:14 +0100268 partial, NULL, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269 return FAIL;
270 }
271 else
272 {
273 s = partial_name(partial);
274 if (s == NULL || *s == NUL)
275 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200276 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000277 funcexe.fe_evaluate = TRUE;
278 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
280 return FAIL;
281 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100282 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200283 else if (expr->v_type == VAR_INSTR)
284 {
285 return exe_typval_instr(expr, rettv);
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 else
288 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000289 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 if (s == NULL)
291 return FAIL;
292 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200293 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200295 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200297 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200298 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
300 }
301 }
302 return OK;
303}
304
305/*
306 * Like eval_to_bool() but using a typval_T instead of a string.
307 * Works for string, funcref and partial.
308 */
309 int
310eval_expr_to_bool(typval_T *expr, int *error)
311{
312 typval_T rettv;
313 int res;
314
315 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
316 {
317 *error = TRUE;
318 return FALSE;
319 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200320 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 clear_tv(&rettv);
322 return res;
323}
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325/*
326 * Top level evaluation function, returning a string. If "skip" is TRUE,
327 * only parsing to "nextcmd" is done, without reporting errors. Return
328 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
329 */
330 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100331eval_to_string_skip(
332 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200333 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100334 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
Bram Moolenaar33570922005-01-25 22:26:29 +0000336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200338 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
Bram Moolenaar37c83712020-06-30 21:18:36 +0200340 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (skip)
342 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 retval = NULL;
345 else
346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100347 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000348 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 }
350 if (skip)
351 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353
354 return retval;
355}
356
357/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100358 * Initialize "evalarg" for use.
359 */
360 void
361init_evalarg(evalarg_T *evalarg)
362{
363 CLEAR_POINTER(evalarg);
364 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
365}
366
367/*
368 * If "evalarg->eval_tofree" is not NULL free it later.
369 * Caller is expected to overwrite "evalarg->eval_tofree" next.
370 */
371 static void
372free_eval_tofree_later(evalarg_T *evalarg)
373{
374 if (evalarg->eval_tofree != NULL)
375 {
376 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
377 ((char_u **)evalarg->eval_tofree_ga.ga_data)
378 [evalarg->eval_tofree_ga.ga_len++]
379 = evalarg->eval_tofree;
380 else
381 vim_free(evalarg->eval_tofree);
382 }
383}
384
385/*
386 * After using "evalarg" filled from "eap": free the memory.
387 */
388 void
389clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
390{
391 if (evalarg != NULL)
392 {
393 if (evalarg->eval_tofree != NULL)
394 {
395 if (eap != NULL)
396 {
397 // We may need to keep the original command line, e.g. for
398 // ":let" it has the variable names. But we may also need the
399 // new one, "nextcmd" points into it. Keep both.
400 vim_free(eap->cmdline_tofree);
401 eap->cmdline_tofree = *eap->cmdlinep;
402 *eap->cmdlinep = evalarg->eval_tofree;
403 }
404 else
405 vim_free(evalarg->eval_tofree);
406 evalarg->eval_tofree = NULL;
407 }
408
409 ga_clear_strings(&evalarg->eval_tofree_ga);
410 VIM_CLEAR(evalarg->eval_tofree_lambda);
411 }
412}
413
414/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000415 * Skip over an expression at "*pp".
416 * Return FAIL for an error, OK otherwise.
417 */
418 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200419skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000420{
Bram Moolenaar33570922005-01-25 22:26:29 +0000421 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000422
423 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200424 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000425}
426
427/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200428 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200429 * If in Vim9 script and line breaks are encountered, the lines are
430 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 * "arg" is advanced to just after the expression.
432 * "start" is set to the start of the expression, "end" to just after the end.
433 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434 * Return FAIL for an error, OK otherwise.
435 */
436 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200437skip_expr_concatenate(
438 char_u **arg,
439 char_u **start,
440 char_u **end,
441 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200442{
443 typval_T rettv;
444 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200445 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200446 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200447 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200449 int evaluate = evalarg == NULL
450 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200451
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200452 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200454 {
455 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200456 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200458 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200459 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200460 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200461 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462
463 // Don't evaluate the expression.
464 if (evalarg != NULL)
465 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200466 *arg = skipwhite(*arg);
467 res = eval1(arg, &rettv, evalarg);
468 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200469 if (evalarg != NULL)
470 evalarg->eval_flags = save_flags;
471
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200472 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200473 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200474 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200475 if (evalarg->eval_ga.ga_len == 1)
476 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200477 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200478 ga_clear(gap);
479 gap->ga_itemsize = 0;
480 }
481 else
482 {
483 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200484 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200485
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200486 // Line breaks encountered, concatenate all the lines.
487 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200488 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200489
490 // free the lines only when using getsourceline()
491 if (evalarg->eval_cookie != NULL)
492 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200493 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200494 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200495 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100496 // later. Also free "eval_tofree" later if needed.
497 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200498 evalarg->eval_tofree =
499 ((char_u **)gap->ga_data)[gap->ga_len - 1];
500 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200501 ga_clear_strings(gap);
502 }
503 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200504 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200505 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200506
507 // free lines that were explicitly marked for freeing
508 ga_clear_strings(freegap);
509 }
510
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200511 gap->ga_itemsize = 0;
512 if (p == NULL)
513 return FAIL;
514 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200515 vim_free(evalarg->eval_tofree_lambda);
516 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200517 // Compute "end" relative to the end.
518 *end = *start + STRLEN(*start) - endoff;
519 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200520 }
521
522 return res;
523}
524
525/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100526 * Convert "tv" to a string.
527 * When "convert" is TRUE convert a List into a sequence of lines and convert
528 * a Float to a String.
529 * Returns an allocated string (NULL when out of memory).
530 */
531 char_u *
532typval2string(typval_T *tv, int convert)
533{
534 garray_T ga;
535 char_u *retval;
536#ifdef FEAT_FLOAT
537 char_u numbuf[NUMBUFLEN];
538#endif
539
540 if (convert && tv->v_type == VAR_LIST)
541 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000542 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100543 if (tv->vval.v_list != NULL)
544 {
545 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
546 if (tv->vval.v_list->lv_len > 0)
547 ga_append(&ga, NL);
548 }
549 ga_append(&ga, NUL);
550 retval = (char_u *)ga.ga_data;
551 }
552#ifdef FEAT_FLOAT
553 else if (convert && tv->v_type == VAR_FLOAT)
554 {
555 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
556 retval = vim_strsave(numbuf);
557 }
558#endif
559 else
560 retval = vim_strsave(tv_get_string(tv));
561 return retval;
562}
563
564/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200565 * Top level evaluation function, returning a string. Does not handle line
566 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000567 * When "convert" is TRUE convert a List into a sequence of lines and convert
568 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 * Return pointer to allocated memory, or NULL for failure.
570 */
571 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100572eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100573 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100574 int convert,
575 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
Bram Moolenaar33570922005-01-25 22:26:29 +0000577 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100579 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100581 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
582 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 retval = NULL;
584 else
585 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000587 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100589 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 return retval;
592}
593
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100594 char_u *
595eval_to_string(
596 char_u *arg,
597 int convert)
598{
599 return eval_to_string_eap(arg, convert, NULL);
600}
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100604 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200605 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 */
607 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608eval_to_string_safe(
609 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000610 int use_sandbox,
611 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200614 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200615 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200616 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar9530b582022-01-22 13:39:08 +0000618 if (!keep_script_version)
619 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200620 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000621 if (use_sandbox)
622 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100623 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200624 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200625 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626 if (use_sandbox)
627 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100628 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200629 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200630 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200631 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return retval;
633}
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635/*
636 * Top level evaluation function, returning a number.
637 * Evaluates "expr" silently.
638 * Returns -1 for an error.
639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar33570922005-01-25 22:26:29 +0000643 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200644 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000645 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647 ++emsg_off;
648
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200649 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 retval = -1;
651 else
652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100653 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000654 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 --emsg_off;
657
658 return retval;
659}
660
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000661/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662 * Top level evaluation function.
663 * Returns an allocated typval_T with the result.
664 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665 */
666 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200667eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000668{
669 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200670 evalarg_T evalarg;
671
672 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000673
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200674 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200675 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100676 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000677
Bram Moolenaar37c83712020-06-30 21:18:36 +0200678 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000679 return tv;
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000683 * "*arg" points to what can be a function name in the form of "import.Name" or
684 * "Funcref". Return the name of the function. Set "tofree" to something that
685 * was allocated.
686 * If "verbose" is FALSE no errors are given.
687 * Return NULL for any failure.
688 */
689 static char_u *
690deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000691 char_u **arg,
692 char_u **tofree,
693 evalarg_T *evalarg,
694 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000695{
696 typval_T ref;
697 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100698 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000699
700 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100701 if (evalarg != NULL)
702 {
703 // need to evaluate this to get an import, like in "a.Func"
704 save_flags = evalarg->eval_flags;
705 evalarg->eval_flags |= EVAL_EVALUATE;
706 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100707 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100708 {
709 dictitem_T *v;
710
711 // If <SID>VarName was used it would not be found, try another way.
712 v = find_var_also_in_script(name, NULL, FALSE);
713 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100714 {
715 name = NULL;
716 goto theend;
717 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100718 copy_tv(&v->di_tv, &ref);
719 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000720 if (*skipwhite(*arg) != NUL)
721 {
722 if (verbose)
723 semsg(_(e_trailing_characters_str), *arg);
724 name = NULL;
725 }
726 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
727 {
728 name = ref.vval.v_string;
729 ref.vval.v_string = NULL;
730 *tofree = name;
731 }
732 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
733 {
734 if (ref.vval.v_partial->pt_argc > 0
735 || ref.vval.v_partial->pt_dict != NULL)
736 {
737 if (verbose)
738 emsg(_(e_cannot_use_partial_here));
739 name = NULL;
740 }
741 else
742 {
743 name = vim_strsave(partial_name(ref.vval.v_partial));
744 *tofree = name;
745 }
746 }
747 else
748 {
749 if (verbose)
750 semsg(_(e_not_callable_type_str), name);
751 name = NULL;
752 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100753
754theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000755 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100756 if (evalarg != NULL)
757 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000758 return name;
759}
760
761/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100762 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200763 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
764 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000765 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100768call_vim_function(
769 char_u *func,
770 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200771 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200772 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000774 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200775 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000776 char_u *arg;
777 char_u *name;
778 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000779 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200782 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000783 funcexe.fe_firstline = curwin->w_cursor.lnum;
784 funcexe.fe_lastline = curwin->w_cursor.lnum;
785 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000786
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000787 // The name might be "import.Func" or "Funcref". We don't know, we need to
788 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000789 // autoload script has errors. Guess that when there is a dot in the name
790 // showing errors is the right choice.
791 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000792 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000793 if (ignore_errors)
794 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000795 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000796 if (ignore_errors)
797 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000798 if (name == NULL)
799 name = func;
800
801 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
802
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000803 if (ret == FAIL)
804 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000805 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806
807 return ret;
808}
809
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100810/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100811 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000812 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
813 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000814 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000815 */
816 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100817call_func_retstr(
818 char_u *func,
819 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200820 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000821{
822 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000823 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000824
Bram Moolenaarded27a12018-08-01 19:06:03 +0200825 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000826 return NULL;
827
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100828 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000829 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 return retval;
831}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000833/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100834 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000835 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000836 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000837 */
838 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839call_func_retlist(
840 char_u *func,
841 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200842 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000843{
844 typval_T rettv;
845
Bram Moolenaarded27a12018-08-01 19:06:03 +0200846 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000847 return NULL;
848
849 if (rettv.v_type != VAR_LIST)
850 {
851 clear_tv(&rettv);
852 return NULL;
853 }
854
855 return rettv.vval.v_list;
856}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaare70dd112022-01-21 16:31:11 +0000858#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100860 * Evaluate "arg", which is 'foldexpr'.
861 * Note: caller must set "curwin" to match "arg".
862 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
863 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 */
865 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000866eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000868 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200870 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000872 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000873 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
874 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaare70dd112022-01-21 16:31:11 +0000876 arg = wp->w_p_fde;
877 current_sctx = wp->w_p_script_ctx[WV_FDE];
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000880 if (use_sandbox)
881 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100882 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200884 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 retval = 0;
886 else
887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100888 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 if (tv.v_type == VAR_NUMBER)
890 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000891 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 retval = 0;
893 else
894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100895 // If the result is a string, check if there is a non-digit before
896 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (!VIM_ISDIGIT(*s) && *s != '-')
899 *cp = *s++;
900 retval = atol((char *)s);
901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000905 if (use_sandbox)
906 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100907 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200908 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000909 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200911 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912}
913#endif
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 * Get an lval: variable, Dict item or List item that can be assigned a value
917 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
918 * "name.key", "name.key[expr]" etc.
919 * Indexing only works if "name" is an existing List or Dictionary.
920 * "name" points to the start of the name.
921 * If "rettv" is not NULL it points to the value to be assigned.
922 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
923 * wrong; must end in space or cmd separator.
924 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100925 * flags:
926 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100927 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100928 * GLV_NO_AUTOLOAD: do not use script autoloading
929 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000931 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000932 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000933 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200934 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100935get_lval(
936 char_u *name,
937 typval_T *rettv,
938 lval_T *lp,
939 int unlet,
940 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100941 int flags, // GLV_ values
942 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 char_u *expr_start, *expr_end;
946 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 dictitem_T *v;
948 typval_T var1;
949 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100953 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100954 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100955 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000956 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100958 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200959 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100961 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100963 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200965 lp->ll_name_end = find_name_end(name, NULL, NULL,
966 FNE_INCL_BR | fne_flags);
967 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968 }
969
Bram Moolenaara749a422022-02-12 19:52:25 +0000970 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000971 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000972 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
973 {
974 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
975 return NULL;
976 }
977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000979 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000981 if (expr_start != NULL)
982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100984 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 && *p != '[' && *p != '.')
986 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000987 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return NULL;
989 }
990
991 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
992 if (lp->ll_exp_name == NULL)
993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100994 // Report an invalid expression in braces, unless the
995 // expression evaluation has been cancelled due to an
996 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 if (!aborting() && !quiet)
998 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000999 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001000 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 return NULL;
1002 }
1003 }
1004 lp->ll_name = lp->ll_exp_name;
1005 }
1006 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 lp->ll_name = name;
1009
Bram Moolenaar4525a572022-02-13 11:57:33 +00001010 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001012 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001013 // However, "g:[key]" is indexing a dictionary.
1014 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001015 {
1016 --p;
1017 lp->ll_name_end = p;
1018 }
1019 if (*p == ':')
1020 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001021 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001022
1023 if (tp == p + 1 && !quiet)
1024 {
1025 semsg(_(e_white_space_required_after_str_str), ":", p);
1026 return NULL;
1027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001029 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001030 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001031 semsg(_(e_using_type_not_in_script_context_str), p);
1032 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001033 }
1034
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001035 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001036 lp->ll_type = parse_type(&tp,
1037 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1038 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001039 if (lp->ll_type == NULL && !quiet)
1040 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001041 lp->ll_name_end = tp;
1042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 }
1044 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001045 if (lp->ll_name == NULL)
1046 return p;
1047
Bram Moolenaar62aec932022-01-29 21:45:34 +00001048 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001049 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001050 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001051
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001052 if (import != NULL)
1053 {
1054 ufunc_T *ufunc;
1055 type_T *type;
1056
Bram Moolenaar753885b2022-08-24 16:30:36 +01001057 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001058 lp->ll_sid = import->imp_sid;
1059 lp->ll_name = skipwhite(p + 1);
1060 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1061 lp->ll_name_end = p;
1062
1063 // check the item is exported
1064 cc = *p;
1065 *p = NUL;
1066 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001067 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001068 {
1069 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001070 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001071 }
1072 *p = cc;
1073 }
1074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001076 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001077 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 return p;
1079
Bram Moolenaar4525a572022-02-13 11:57:33 +00001080 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001081 {
1082 // using local variable
1083 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001084 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001085 }
1086 else
1087 {
1088 cc = *p;
1089 *p = NUL;
1090 // When we would write to the variable pass &ht and prevent autoload.
1091 writing = !(flags & GLV_READ_ONLY);
1092 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001093 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001094 if (v == NULL && !quiet)
1095 semsg(_(e_undefined_variable_str), lp->ll_name);
1096 *p = cc;
1097 if (v == NULL)
1098 return NULL;
1099 lp->ll_tv = &v->di_tv;
1100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001101
Bram Moolenaar4525a572022-02-13 11:57:33 +00001102 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001103 {
1104 if (!quiet)
1105 semsg(_(e_variable_already_declared), lp->ll_name);
1106 return NULL;
1107 }
1108
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 /*
1110 * Loop until no more [idx] or .key is following.
1111 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001112 var1.v_type = VAR_UNKNOWN;
1113 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001114 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001115 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001116 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1117 {
1118 if (!quiet)
1119 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1120 return NULL;
1121 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001122 if (lp->ll_tv->v_type != VAR_LIST
1123 && lp->ll_tv->v_type != VAR_DICT
1124 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001127 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001130
1131 // a NULL list/blob works like an empty list/blob, allocate one now.
1132 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1133 rettv_list_alloc(lp->ll_tv);
1134 else if (lp->ll_tv->v_type == VAR_BLOB
1135 && lp->ll_tv->vval.v_blob == NULL)
1136 rettv_blob_alloc(lp->ll_tv);
1137
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001141 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001144
Bram Moolenaar4525a572022-02-13 11:57:33 +00001145 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001146 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001147 && lp->ll_tv == &v->di_tv
1148 && ht != NULL && ht == get_script_local_ht())
1149 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001150 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001151
1152 // Vim9 script local variable: get the type
1153 if (sv != NULL)
1154 lp->ll_valtype = sv->sv_type;
1155 }
1156
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 len = -1;
1158 if (*p == '.')
1159 {
1160 key = p + 1;
1161 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1162 ;
1163 if (len == 0)
1164 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001166 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 }
1169 p = key + len;
1170 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001171 else
1172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001173 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001174 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 if (*p == ':')
1176 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001177 else
1178 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001179 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001180 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001182 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001184 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001185 clear_tv(&var1);
1186 return NULL;
1187 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001188 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 }
1190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001191 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001192 if (*p == ':')
1193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001197 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001198 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001200 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 if (rettv != NULL
1202 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001203 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001205 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001208 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001209 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 }
1212 p = skipwhite(p + 1);
1213 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001215 else
1216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001218 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001219 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001221 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001224 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001226 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001227 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001228 clear_tv(&var2);
1229 return NULL;
1230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001233 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001238 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001240 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001241 clear_tv(&var1);
1242 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 }
1245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 ++p;
1248 }
1249
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 {
1252 if (len == -1)
1253 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // "[key]": get key from "var1"
1255 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001256 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 }
1261 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001262 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001263
1264 // a NULL dict is equivalent with an empty dict
1265 if (lp->ll_tv->vval.v_dict == NULL)
1266 {
1267 lp->ll_tv->vval.v_dict = dict_alloc();
1268 if (lp->ll_tv->vval.v_dict == NULL)
1269 {
1270 clear_tv(&var1);
1271 return NULL;
1272 }
1273 ++lp->ll_tv->vval.v_dict->dv_refcount;
1274 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001275 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001276
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001277 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001279 // When assigning to a scope dictionary check that a function and
1280 // variable name is valid (only variable name unless it is l: or
1281 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001282 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001283 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001284 int prevval;
1285 int wrong;
1286
1287 if (len != -1)
1288 {
1289 prevval = key[len];
1290 key[len] = NUL;
1291 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001292 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001293 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001294 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1295 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001296 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001297 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001298 if (len != -1)
1299 key[len] = prevval;
1300 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001301 {
1302 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001303 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001304 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001305 }
1306
Bram Moolenaar10c65862020-10-08 21:16:42 +02001307 if (lp->ll_valtype != NULL)
1308 // use the type of the member
1309 lp->ll_valtype = lp->ll_valtype->tt_member;
1310
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001312 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001313 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001314 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001315 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001316 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001317 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001318 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001319 return NULL;
1320 }
1321
Bram Moolenaar31b81602019-02-10 22:14:27 +01001322 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001326 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001327 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 }
1330 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001334 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 p = NULL;
1337 break;
1338 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001339 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001340 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001341 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1342 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001343 {
1344 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001345 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001346 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001347
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001350 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001351 else if (lp->ll_tv->v_type == VAR_BLOB)
1352 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001353 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1354
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001355 /*
1356 * Get the number and item for the only or first index of the List.
1357 */
1358 if (empty1)
1359 lp->ll_n1 = 0;
1360 else
1361 // is number or string
1362 lp->ll_n1 = (long)tv_get_number(&var1);
1363 clear_tv(&var1);
1364
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001365 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001367 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001368 return NULL;
1369 }
1370 if (lp->ll_range && !lp->ll_empty2)
1371 {
1372 lp->ll_n2 = (long)tv_get_number(&var2);
1373 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001374 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1375 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001376 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001377 }
1378 lp->ll_blob = lp->ll_tv->vval.v_blob;
1379 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001380 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001382 else
1383 {
1384 /*
1385 * Get the number and item for the only or first index of the List.
1386 */
1387 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001389 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001390 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001392 clear_tv(&var1);
1393
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001394 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001396 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1397 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001398 if (lp->ll_li == NULL)
1399 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001400 clear_tv(&var2);
1401 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001402 }
1403
Bram Moolenaar10c65862020-10-08 21:16:42 +02001404 if (lp->ll_valtype != NULL)
1405 // use the type of the member
1406 lp->ll_valtype = lp->ll_valtype->tt_member;
1407
Bram Moolenaar8c711452005-01-14 21:53:12 +00001408 /*
1409 * May need to find the item or absolute index for the second
1410 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 * When no index given: "lp->ll_empty2" is TRUE.
1412 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001413 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001415 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001416 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001418 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001419 if (check_range_index_two(lp->ll_list,
1420 &lp->ll_n1, lp->ll_li,
1421 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001423 }
1424
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 }
1428
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001429 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 return p;
1432}
1433
1434/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001438clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439{
1440 vim_free(lp->ll_exp_name);
1441 vim_free(lp->ll_newkey);
1442}
1443
1444/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001445 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001447 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1448 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001451set_var_lval(
1452 lval_T *lp,
1453 char_u *endp,
1454 typval_T *rettv,
1455 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001456 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1457 char_u *op,
1458 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459{
1460 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001461 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001462
1463 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001465 cc = *endp;
1466 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001467 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1468 return;
1469
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001470 if (lp->ll_blob != NULL)
1471 {
1472 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001473
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 if (op != NULL && *op != '=')
1475 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001476 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001477 return;
1478 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001479 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001480 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481
1482 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1483 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001484 if (lp->ll_empty2)
1485 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1486
Bram Moolenaar68452172021-04-12 21:21:02 +02001487 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1488 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001490 }
1491 else
1492 {
1493 val = (int)tv_get_number_chk(rettv, &error);
1494 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001495 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001496 }
1497 }
1498 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001500 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001502 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1503 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001504 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001505 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001506 *endp = cc;
1507 return;
1508 }
1509
Bram Moolenaarff697e62019-02-12 22:28:33 +01001510 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001511 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001512 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001513 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001514 {
1515 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001516 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1517 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001518 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001519 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001520 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001521 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001523 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001524 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001525 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001526 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1527 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001528 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001529 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001530 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001531 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001532 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001533 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001534 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001535 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001536 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001537 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001538 else if (lp->ll_range)
1539 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001540 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1541 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001542 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001544 return;
1545 }
1546
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001547 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1548 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001549 }
1550 else
1551 {
1552 /*
1553 * Assign to a List or Dictionary item.
1554 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001555 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1556 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001557 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001558 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001559 return;
1560 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001561
1562 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001563 && check_typval_arg_type(lp->ll_valtype, rettv,
1564 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001565 return;
1566
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001567 if (lp->ll_newkey != NULL)
1568 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 if (op != NULL && *op != '=')
1570 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001571 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001572 return;
1573 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001574 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1575 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001576 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001578 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001579 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 if (di == NULL)
1581 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001582 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1583 {
1584 vim_free(di);
1585 return;
1586 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001587 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 else if (op != NULL && *op != '=')
1590 {
1591 tv_op(lp->ll_tv, rettv, op);
1592 return;
1593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 /*
1598 * Assign the value to the variable or list item.
1599 */
1600 if (copy)
1601 copy_tv(rettv, lp->ll_tv);
1602 else
1603 {
1604 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001605 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001606 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 }
1608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609}
1610
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001612 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1613 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 * Returns OK or FAIL.
1615 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001617tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001619 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 char_u numbuf[NUMBUFLEN];
1621 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001622 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001623
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001624 // Can't do anything with a Funcref or Dict on the right.
1625 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001626 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001627 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1628 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 {
1630 switch (tv1->v_type)
1631 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001632 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001633 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001635 case VAR_DICT:
1636 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001637 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001638 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001639 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001640 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001641 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001642 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 break;
1644
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 case VAR_BLOB:
1646 if (*op != '+' || tv2->v_type != VAR_BLOB)
1647 break;
1648 // BLOB += BLOB
1649 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1650 {
1651 blob_T *b1 = tv1->vval.v_blob;
1652 blob_T *b2 = tv2->vval.v_blob;
1653 int i, len = blob_len(b2);
1654 for (i = 0; i < len; i++)
1655 ga_append(&b1->bv_ga, blob_get(b2, i));
1656 }
1657 return OK;
1658
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 case VAR_LIST:
1660 if (*op != '+' || tv2->v_type != VAR_LIST)
1661 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001662 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001663 if (tv2->vval.v_list != NULL)
1664 {
1665 if (tv1->vval.v_list == NULL)
1666 {
1667 tv1->vval.v_list = tv2->vval.v_list;
1668 ++tv1->vval.v_list->lv_refcount;
1669 }
1670 else
1671 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 return OK;
1674
1675 case VAR_NUMBER:
1676 case VAR_STRING:
1677 if (tv2->v_type == VAR_LIST)
1678 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001679 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001681 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001682 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001683#ifdef FEAT_FLOAT
1684 if (tv2->v_type == VAR_FLOAT)
1685 {
1686 float_T f = n;
1687
Bram Moolenaarff697e62019-02-12 22:28:33 +01001688 if (*op == '%')
1689 break;
1690 switch (*op)
1691 {
1692 case '+': f += tv2->vval.v_float; break;
1693 case '-': f -= tv2->vval.v_float; break;
1694 case '*': f *= tv2->vval.v_float; break;
1695 case '/': f /= tv2->vval.v_float; break;
1696 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001697 clear_tv(tv1);
1698 tv1->v_type = VAR_FLOAT;
1699 tv1->vval.v_float = f;
1700 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001702#endif
1703 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001704 switch (*op)
1705 {
1706 case '+': n += tv_get_number(tv2); break;
1707 case '-': n -= tv_get_number(tv2); break;
1708 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001709 case '/': n = num_divide(n, tv_get_number(tv2),
1710 &failed); break;
1711 case '%': n = num_modulus(n, tv_get_number(tv2),
1712 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001713 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001714 clear_tv(tv1);
1715 tv1->v_type = VAR_NUMBER;
1716 tv1->vval.v_number = n;
1717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 }
1719 else
1720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001721 if (tv2->v_type == VAR_FLOAT)
1722 break;
1723
Bram Moolenaarff697e62019-02-12 22:28:33 +01001724 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001725 s = tv_get_string(tv1);
1726 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 clear_tv(tv1);
1728 tv1->v_type = VAR_STRING;
1729 tv1->vval.v_string = s;
1730 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001731 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001732
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001733 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001734#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001735 {
1736 float_T f;
1737
Bram Moolenaarff697e62019-02-12 22:28:33 +01001738 if (*op == '%' || *op == '.'
1739 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740 && tv2->v_type != VAR_NUMBER
1741 && tv2->v_type != VAR_STRING))
1742 break;
1743 if (tv2->v_type == VAR_FLOAT)
1744 f = tv2->vval.v_float;
1745 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001746 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001747 switch (*op)
1748 {
1749 case '+': tv1->vval.v_float += f; break;
1750 case '-': tv1->vval.v_float -= f; break;
1751 case '*': tv1->vval.v_float *= f; break;
1752 case '/': tv1->vval.v_float /= f; break;
1753 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001754 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001755#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001756 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 }
1758 }
1759
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001760 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 return FAIL;
1762}
1763
1764/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 * Evaluate the expression used in a ":for var in expr" command.
1766 * "arg" points to "var".
1767 * Set "*errp" to TRUE for an error, FALSE otherwise;
1768 * Return a pointer that holds the info. Null when there is an error.
1769 */
1770 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001771eval_for_line(
1772 char_u *arg,
1773 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001774 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001775 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001778 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 typval_T tv;
1781 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001782 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001784 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001786 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 if (fi == NULL)
1788 return NULL;
1789
Bram Moolenaar404557e2021-07-05 21:41:48 +02001790 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1791 &fi->fi_semicolon, FALSE);
1792 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 return fi;
1794
Bram Moolenaar404557e2021-07-05 21:41:48 +02001795 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001796 if (expr[0] != 'i' || expr[1] != 'n'
1797 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001799 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1800 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1801 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001802 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 return fi;
1804 }
1805
1806 if (skip)
1807 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001808 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1809 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 {
1811 *errp = FALSE;
1812 if (!skip)
1813 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001814 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001815 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001816 l = tv.vval.v_list;
1817 if (l == NULL)
1818 {
1819 // a null list is like an empty list: do nothing
1820 clear_tv(&tv);
1821 }
1822 else
1823 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001825 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001827 // No need to increment the refcount, it's already set for
1828 // the list being used in "tv".
1829 fi->fi_list = l;
1830 list_add_watch(l, &fi->fi_lw);
1831 fi->fi_lw.lw_item = l->lv_first;
1832 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001833 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001834 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001835 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001836 fi->fi_bi = 0;
1837 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001838 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001839 typval_T btv;
1840
1841 // Make a copy, so that the iteration still works when the
1842 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001844 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001845 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001846 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001847 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001848 else if (tv.v_type == VAR_STRING)
1849 {
1850 fi->fi_byte_idx = 0;
1851 fi->fi_string = tv.vval.v_string;
1852 tv.vval.v_string = NULL;
1853 if (fi->fi_string == NULL)
1854 fi->fi_string = vim_strsave((char_u *)"");
1855 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 else
1857 {
Sean Dewar80d73952021-08-04 19:25:54 +02001858 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001859 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 }
1861 }
1862 }
1863 if (skip)
1864 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001865 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866
1867 return fi;
1868}
1869
1870/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001871 * Used when looping over a :for line, skip the "in expr" part.
1872 */
1873 void
1874skip_for_lines(void *fi_void, evalarg_T *evalarg)
1875{
1876 forinfo_T *fi = (forinfo_T *)fi_void;
1877 int i;
1878
1879 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001880 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001881}
1882
1883/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 * Use the first item in a ":for" list. Advance to the next.
1885 * Assign the values to the variable (list). "arg" points to the first one.
1886 * Return TRUE when a valid item was found, FALSE when at end of list or
1887 * something wrong.
1888 */
1889 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001892 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001894 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001895 ? (ASSIGN_FINAL
1896 // first round: error if variable exists
1897 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1898 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001899 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001901 int skip_assign = in_vim9script() && arg[0] == '_'
1902 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001904 if (fi->fi_blob != NULL)
1905 {
1906 typval_T tv;
1907
1908 if (fi->fi_bi >= blob_len(fi->fi_blob))
1909 return FALSE;
1910 tv.v_type = VAR_NUMBER;
1911 tv.v_lock = VAR_FIXED;
1912 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1913 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001914 if (skip_assign)
1915 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001916 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001917 fi->fi_varcount, flag, NULL) == OK;
1918 }
1919
1920 if (fi->fi_string != NULL)
1921 {
1922 typval_T tv;
1923 int len;
1924
1925 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1926 if (len == 0)
1927 return FALSE;
1928 tv.v_type = VAR_STRING;
1929 tv.v_lock = VAR_FIXED;
1930 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1931 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001932 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001933 if (skip_assign)
1934 result = TRUE;
1935 else
1936 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001937 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001938 vim_free(tv.vval.v_string);
1939 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 }
1941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 item = fi->fi_lw.lw_item;
1943 if (item == NULL)
1944 result = FALSE;
1945 else
1946 {
1947 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001948 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001949 if (skip_assign)
1950 result = TRUE;
1951 else
1952 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001953 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 }
1955 return result;
1956}
1957
1958/*
1959 * Free the structure used to store info used by ":for".
1960 */
1961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963{
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001966 if (fi == NULL)
1967 return;
1968 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001969 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001971 list_unref(fi->fi_list);
1972 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001973 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001974 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001975 else
1976 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 vim_free(fi);
1978}
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981set_context_for_expression(
1982 expand_T *xp,
1983 char_u *arg,
1984 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001986 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001990 if (cmdidx == CMD_let || cmdidx == CMD_var
1991 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 {
1993 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001994 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001996 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001997 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 {
1999 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002000 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002001 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 break;
2003 }
2004 return;
2005 }
2006 }
2007 else
2008 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2009 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 while ((xp->xp_pattern = vim_strpbrk(arg,
2011 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2012 {
2013 c = *xp->xp_pattern;
2014 if (c == '&')
2015 {
2016 c = xp->xp_pattern[1];
2017 if (c == '&')
2018 {
2019 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002020 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002025 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2026 xp->xp_pattern += 2;
2027
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030 else if (c == '$')
2031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 xp->xp_context = EXPAND_ENV_VARS;
2034 }
2035 else if (c == '=')
2036 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002037 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 xp->xp_context = EXPAND_EXPRESSION;
2039 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002040 else if (c == '#'
2041 && xp->xp_context == EXPAND_EXPRESSION)
2042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002044 break;
2045 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002046 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 && xp->xp_context == EXPAND_FUNCTIONS
2048 && vim_strchr(xp->xp_pattern, '(') == NULL)
2049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 break;
2052 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002053 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2058 if (c == '\\' && xp->xp_pattern[1] != NUL)
2059 ++xp->xp_pattern;
2060 xp->xp_context = EXPAND_NOTHING;
2061 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002064 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2066 /* skip */ ;
2067 xp->xp_context = EXPAND_NOTHING;
2068 }
2069 else if (c == '|')
2070 {
2071 if (xp->xp_pattern[1] == '|')
2072 {
2073 ++xp->xp_pattern;
2074 xp->xp_context = EXPAND_EXPRESSION;
2075 }
2076 else
2077 xp->xp_context = EXPAND_COMMANDS;
2078 }
2079 else
2080 xp->xp_context = EXPAND_EXPRESSION;
2081 }
2082 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002083 // Doesn't look like something valid, expand as an expression
2084 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 arg = xp->xp_pattern;
2087 if (*arg != NUL)
2088 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2089 /* skip */ ;
2090 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002091
2092 // ":exe one two" completes "two"
2093 if ((cmdidx == CMD_execute
2094 || cmdidx == CMD_echo
2095 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002096 || cmdidx == CMD_echomsg
2097 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002098 && xp->xp_context == EXPAND_EXPRESSION)
2099 {
2100 for (;;)
2101 {
2102 char_u *n = skiptowhite(arg);
2103
2104 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2105 break;
2106 arg = skipwhite(n);
2107 }
2108 }
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 xp->xp_pattern = arg;
2111}
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002114 * Return TRUE if "pat" matches "text".
2115 * Does not use 'cpo' and always uses 'magic'.
2116 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002117 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002118pattern_match(char_u *pat, char_u *text, int ic)
2119{
2120 int matches = FALSE;
2121 char_u *save_cpo;
2122 regmatch_T regmatch;
2123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002124 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002125 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002126 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002127 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2128 if (regmatch.regprog != NULL)
2129 {
2130 regmatch.rm_ic = ic;
2131 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2132 vim_regfree(regmatch.regprog);
2133 }
2134 p_cpo = save_cpo;
2135 return matches;
2136}
2137
2138/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002139 * Handle a name followed by "(". Both for just "name(arg)" and for
2140 * "expr->name(arg)".
2141 * Returns OK or FAIL.
2142 */
2143 static int
2144eval_func(
2145 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002146 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002147 char_u *name,
2148 int name_len,
2149 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002150 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002151 typval_T *basetv) // "expr" for "expr->name(arg)"
2152{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002153 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002154 char_u *s = name;
2155 int len = name_len;
2156 partial_T *partial;
2157 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002158 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002159 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002160
2161 if (!evaluate)
2162 check_vars(s, len);
2163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002164 // If "s" is the name of a variable of type VAR_FUNC
2165 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002166 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002167 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002169 // Need to make a copy, in case evaluating the arguments makes
2170 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002171 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002172 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002173 ret = FAIL;
2174 else
2175 {
2176 funcexe_T funcexe;
2177
2178 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002179 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002180 funcexe.fe_firstline = curwin->w_cursor.lnum;
2181 funcexe.fe_lastline = curwin->w_cursor.lnum;
2182 funcexe.fe_evaluate = evaluate;
2183 funcexe.fe_partial = partial;
2184 funcexe.fe_basetv = basetv;
2185 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002186 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002187 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002188 }
2189 vim_free(s);
2190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002191 // If evaluate is FALSE rettv->v_type was not set in
2192 // get_func_tv, but it's needed in handle_subscript() to parse
2193 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002194 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2195 {
2196 rettv->vval.v_string = NULL;
2197 rettv->v_type = VAR_FUNC;
2198 }
2199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002200 // Stop the expression evaluation when immediately
2201 // aborting on error, or when an interrupt occurred or
2202 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002203 if (evaluate && aborting())
2204 {
2205 if (ret == OK)
2206 clear_tv(rettv);
2207 ret = FAIL;
2208 }
2209 return ret;
2210}
2211
2212/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002213 * After a NL, skip over empty lines and comment-only lines.
2214 */
2215 static char_u *
2216newline_skip_comments(char_u *arg)
2217{
2218 char_u *p = arg + 1;
2219
2220 for (;;)
2221 {
2222 p = skipwhite(p);
2223
2224 if (*p == NUL)
2225 break;
2226 if (vim9_comment_start(p))
2227 {
2228 char_u *nl = vim_strchr(p, NL);
2229
2230 if (nl == NULL)
2231 break;
2232 p = nl;
2233 }
2234 if (*p != NL)
2235 break;
2236 ++p; // skip another NL
2237 }
2238 return p;
2239}
2240
2241/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002242 * Get the next line source line without advancing. But do skip over comment
2243 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002244 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002245 */
2246 static char_u *
2247getline_peek_skip_comments(evalarg_T *evalarg)
2248{
2249 for (;;)
2250 {
2251 char_u *next = getline_peek(evalarg->eval_getline,
2252 evalarg->eval_cookie);
2253 char_u *p;
2254
2255 if (next == NULL)
2256 break;
2257 p = skipwhite(next);
2258 if (*p != NUL && !vim9_comment_start(p))
2259 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002260 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002261 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002262 }
2263 return NULL;
2264}
2265
2266/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002267 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2268 * comment) and there is a next line, return the next line (skipping blanks)
2269 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002270 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2271 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 * "arg" must point somewhere inside a line, not at the start.
2273 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002274 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2276{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002277 char_u *p = skipwhite(arg);
2278
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002280 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002281 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002282 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2283 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002284 && (*p == NUL || *p == NL
2285 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002287 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002288
Bram Moolenaare442d592022-05-05 12:20:28 +01002289 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002290 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002291 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002292 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002293 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002294 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002295
Bram Moolenaar9d489562020-07-30 20:08:50 +02002296 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297 {
2298 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002299 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 }
2301 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303}
2304
2305/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002306 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002307 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002308 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002309 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002310eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002311{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002312 garray_T *gap = &evalarg->eval_ga;
2313 char_u *line;
2314
Bram Moolenaar39be4982022-05-06 21:51:50 +01002315 if (arg != NULL)
2316 {
2317 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002318 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002319 // Truncate before a trailing comment, so that concatenating the lines
2320 // won't turn the rest into a comment.
2321 if (*skipwhite(arg) == '#')
2322 *arg = NUL;
2323 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002324
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002325 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002326 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2327 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002328 else
2329 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002330 if (line == NULL)
2331 return NULL;
2332
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002333 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002334 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2335 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002336 char_u *p = skipwhite(line);
2337
2338 // Going to concatenate the lines after parsing. For an empty or
2339 // comment line use an empty string.
2340 if (*p == NUL || vim9_comment_start(p))
2341 {
2342 vim_free(line);
2343 line = vim_strsave((char_u *)"");
2344 }
2345
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002346 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2347 ++gap->ga_len;
2348 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002349 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002350 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002351 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002352 evalarg->eval_tofree = line;
2353 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002354
2355 // Advanced to the next line, "arg" no longer points into the previous
2356 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002357 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002358 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002359}
2360
Bram Moolenaare6b53242020-07-01 17:28:33 +02002361/*
2362 * Call eval_next_non_blank() and get the next line if needed.
2363 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002364 char_u *
2365skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2366{
2367 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002368 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002369
Bram Moolenaar962d7212020-07-04 14:15:00 +02002370 if (evalarg == NULL)
2371 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002372 eval_next_non_blank(p, evalarg, &getnext);
2373 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002374 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002375 return p;
2376}
2377
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2382 */
2383
2384/*
2385 * Handle zero level expression.
2386 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002388 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002389 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 * Return OK or FAIL.
2391 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002393eval0(
2394 char_u *arg,
2395 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002396 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002397 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002399 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2400}
2401
2402/*
2403 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2404 * expression and don't check what comes after the expression.
2405 */
2406 int
2407eval0_retarg(
2408 char_u *arg,
2409 typval_T *rettv,
2410 exarg_T *eap,
2411 evalarg_T *evalarg,
2412 char_u **retarg)
2413{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 int ret;
2415 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002416 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002417 int did_emsg_before = did_emsg;
2418 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002420 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002421 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002424 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002425
Bram Moolenaar79481362022-06-27 20:15:10 +01002426 if (ret != FAIL)
2427 {
2428 expr_end = p;
2429 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002430
Bram Moolenaar79481362022-06-27 20:15:10 +01002431 // In Vim9 script a command block is not split at NL characters for
2432 // commands using an expression argument. Skip over a '#' comment to
2433 // check for a following NL. Require white space before the '#'.
2434 if (in_vim9script() && p > expr_end && retarg == NULL)
2435 while (*p == '#')
2436 {
2437 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002438
Bram Moolenaar79481362022-06-27 20:15:10 +01002439 if (nl == NULL)
2440 break;
2441 p = skipwhite(nl + 1);
2442 if (eap != NULL && *p != NUL)
2443 eap->nextcmd = p;
2444 check_for_end = FALSE;
2445 }
2446
2447 if (check_for_end)
2448 end_error = !ends_excmd2(arg, p);
2449 }
2450
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002451 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /*
2456 * Report the invalid expression unless the expression evaluation has
2457 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002458 * exception, or we already gave a more specific error.
2459 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002461 if (!aborting()
2462 && did_emsg == did_emsg_before
2463 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002464 && (flags & EVAL_CONSTANT) == 0
2465 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002466 {
2467 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002468 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002469 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002470 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002471 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002472
2473 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002474 // a next command to avoid more errors, unless "|" is following, which
2475 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002476 if (eap != NULL && p != NULL
2477 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002478 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002481
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002482 if (retarg != NULL)
2483 *retarg = p;
2484 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002485 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return ret;
2488}
2489
2490/*
2491 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002492 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002493 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 *
2495 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002496 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002498 * Note: "rettv.v_lock" is not set.
2499 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * Return OK or FAIL.
2501 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002502 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002505 char_u *p;
2506 int getnext;
2507
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002508 CLEAR_POINTER(rettv);
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /*
2511 * Get the first variable.
2512 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002513 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 return FAIL;
2515
Bram Moolenaar793648f2020-06-26 21:28:25 +02002516 p = eval_next_non_blank(*arg, evalarg, &getnext);
2517 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002519 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 int result;
2521 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002522 evalarg_T *evalarg_used = evalarg;
2523 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002524 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002525 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002526 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002527
2528 if (evalarg == NULL)
2529 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002530 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 orig_flags = evalarg_used->eval_flags;
2534 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002537 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002538 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002539 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002540 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002541 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002542 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002543 clear_tv(rettv);
2544 return FAIL;
2545 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002546 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002547 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002552 int error = FALSE;
2553
Bram Moolenaar13106602020-10-04 16:06:05 +02002554 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002555 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002556 else if (vim9script)
2557 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002558 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002560 if (error || !op_falsy || !result)
2561 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002562 if (error)
2563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565
2566 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002567 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002569 if (op_falsy)
2570 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002571 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002572 {
mityu4ac198c2021-05-28 17:52:40 +02002573 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002574 clear_tv(rettv);
2575 return FAIL;
2576 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002578 evalarg_used->eval_flags = (op_falsy ? !result : result)
2579 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2580 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002581 {
2582 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002584 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002585 if (!op_falsy || !result)
2586 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002588 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002590 /*
2591 * Check for the ":".
2592 */
2593 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2594 if (*p != ':')
2595 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002596 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002597 if (evaluate && result)
2598 clear_tv(rettv);
2599 evalarg_used->eval_flags = orig_flags;
2600 return FAIL;
2601 }
2602 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002603 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002604 else
2605 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002606 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002607 {
2608 error_white_both(p, 1);
2609 clear_tv(rettv);
2610 evalarg_used->eval_flags = orig_flags;
2611 return FAIL;
2612 }
2613 *arg = p;
2614 }
2615
2616 /*
2617 * Get the third variable. Recursive!
2618 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002619 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002620 {
mityu4ac198c2021-05-28 17:52:40 +02002621 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002622 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002623 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002624 return FAIL;
2625 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002626 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2627 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002628 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002629 if (eval1(arg, &var2, evalarg_used) == FAIL)
2630 {
2631 if (evaluate && result)
2632 clear_tv(rettv);
2633 evalarg_used->eval_flags = orig_flags;
2634 return FAIL;
2635 }
2636 if (evaluate && !result)
2637 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639
2640 if (evalarg == NULL)
2641 clear_evalarg(&local_evalarg, NULL);
2642 else
2643 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645
2646 return OK;
2647}
2648
2649/*
2650 * Handle first level expression:
2651 * expr2 || expr2 || expr2 logical OR
2652 *
2653 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002654 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 *
2656 * Return OK or FAIL.
2657 */
2658 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002661 char_u *p;
2662 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002665 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 return FAIL;
2669
2670 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002671 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002673 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676 evalarg_T *evalarg_used = evalarg;
2677 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002678 int evaluate;
2679 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002680 long result = FALSE;
2681 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002682 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002683 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002684
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 if (evalarg == NULL)
2686 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002687 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002690 orig_flags = evalarg_used->eval_flags;
2691 evaluate = orig_flags & EVAL_EVALUATE;
2692 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002694 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002695 result = tv_get_bool_chk(rettv, &error);
2696 else if (tv_get_number_chk(rettv, &error) != 0)
2697 result = TRUE;
2698 clear_tv(rettv);
2699 if (error)
2700 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702
2703 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002704 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002706 while (p[0] == '|' && p[1] == '|')
2707 {
2708 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002709 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002710 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002711 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002712 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002713 {
2714 error_white_both(p, 2);
2715 clear_tv(rettv);
2716 return FAIL;
2717 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002718 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002719 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720
2721 /*
2722 * Get the second variable.
2723 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002724 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002725 {
mityu4ac198c2021-05-28 17:52:40 +02002726 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002727 clear_tv(rettv);
2728 return FAIL;
2729 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002730 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2731 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002733 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002735
2736 /*
2737 * Compute the result.
2738 */
2739 if (evaluate && !result)
2740 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002741 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002742 result = tv_get_bool_chk(&var2, &error);
2743 else if (tv_get_number_chk(&var2, &error) != 0)
2744 result = TRUE;
2745 clear_tv(&var2);
2746 if (error)
2747 return FAIL;
2748 }
2749 if (evaluate)
2750 {
2751 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002752 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002753 rettv->v_type = VAR_BOOL;
2754 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002755 }
2756 else
2757 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002758 rettv->v_type = VAR_NUMBER;
2759 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002760 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762
2763 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002765
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002766 if (evalarg == NULL)
2767 clear_evalarg(&local_evalarg, NULL);
2768 else
2769 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771
2772 return OK;
2773}
2774
2775/*
2776 * Handle second level expression:
2777 * expr3 && expr3 && expr3 logical AND
2778 *
2779 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002780 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 *
2782 * Return OK or FAIL.
2783 */
2784 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002785eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002787 char_u *p;
2788 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002791 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002793 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return FAIL;
2795
2796 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002799 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002800 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002802 evalarg_T *evalarg_used = evalarg;
2803 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002804 int orig_flags;
2805 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002806 long result = TRUE;
2807 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002808 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002809 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002810
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002811 if (evalarg == NULL)
2812 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002813 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002816 orig_flags = evalarg_used->eval_flags;
2817 evaluate = orig_flags & EVAL_EVALUATE;
2818 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002820 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002821 result = tv_get_bool_chk(rettv, &error);
2822 else if (tv_get_number_chk(rettv, &error) == 0)
2823 result = FALSE;
2824 clear_tv(rettv);
2825 if (error)
2826 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
2829 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002830 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002832 while (p[0] == '&' && p[1] == '&')
2833 {
2834 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002835 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002836 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002837 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002838 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002839 {
2840 error_white_both(p, 2);
2841 clear_tv(rettv);
2842 return FAIL;
2843 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002844 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002845 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002846
2847 /*
2848 * Get the second variable.
2849 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002850 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002851 {
mityu4ac198c2021-05-28 17:52:40 +02002852 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002853 clear_tv(rettv);
2854 return FAIL;
2855 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002856 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2857 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002858 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002859 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002860 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002862
2863 /*
2864 * Compute the result.
2865 */
2866 if (evaluate && result)
2867 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002868 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002869 result = tv_get_bool_chk(&var2, &error);
2870 else if (tv_get_number_chk(&var2, &error) == 0)
2871 result = FALSE;
2872 clear_tv(&var2);
2873 if (error)
2874 return FAIL;
2875 }
2876 if (evaluate)
2877 {
2878 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002879 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002880 rettv->v_type = VAR_BOOL;
2881 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002882 }
2883 else
2884 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002885 rettv->v_type = VAR_NUMBER;
2886 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002887 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002888 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002889
2890 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002892
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893 if (evalarg == NULL)
2894 clear_evalarg(&local_evalarg, NULL);
2895 else
2896 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898
2899 return OK;
2900}
2901
2902/*
2903 * Handle third level expression:
2904 * var1 == var2
2905 * var1 =~ var2
2906 * var1 != var2
2907 * var1 !~ var2
2908 * var1 > var2
2909 * var1 >= var2
2910 * var1 < var2
2911 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002912 * var1 is var2
2913 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 *
2915 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002916 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 *
2918 * Return OK or FAIL.
2919 */
2920 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002924 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002925 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002927 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002930 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 return FAIL;
2934
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002935 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002936
Bram Moolenaar696ba232020-07-29 21:20:41 +02002937 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002940 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002942 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002944 typval_T var2;
2945 int ic;
2946 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002947 int evaluate = evalarg == NULL
2948 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002949 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002950
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002951 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002952 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002953 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002954 p = *arg;
2955 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002956 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2957 {
mityu4ac198c2021-05-28 17:52:40 +02002958 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002959 clear_tv(rettv);
2960 return FAIL;
2961 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002962
Bram Moolenaar696ba232020-07-29 21:20:41 +02002963 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2964 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002965 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002966 clear_tv(rettv);
2967 return FAIL;
2968 }
2969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002970 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (p[len] == '?')
2972 {
2973 ic = TRUE;
2974 ++len;
2975 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002976 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else if (p[len] == '#')
2978 {
2979 ic = FALSE;
2980 ++len;
2981 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002982 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002984 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 /*
2987 * Get the second variable.
2988 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002989 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2990 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002991 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002992 clear_tv(rettv);
2993 return FAIL;
2994 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002995 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 return FAIL;
3000 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003001 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003002 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003003 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003004
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003005 // use the line of the comparison for messages
3006 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003007 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003008 {
3009 ret = FAIL;
3010 clear_tv(rettv);
3011 }
3012 else
3013 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003014 clear_tv(&var2);
3015 return ret;
3016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018
3019 return OK;
3020}
3021
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003022/*
3023 * Make a copy of blob "tv1" and append blob "tv2".
3024 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 void
3026eval_addblob(typval_T *tv1, typval_T *tv2)
3027{
3028 blob_T *b1 = tv1->vval.v_blob;
3029 blob_T *b2 = tv2->vval.v_blob;
3030 blob_T *b = blob_alloc();
3031 int i;
3032
3033 if (b != NULL)
3034 {
3035 for (i = 0; i < blob_len(b1); i++)
3036 ga_append(&b->bv_ga, blob_get(b1, i));
3037 for (i = 0; i < blob_len(b2); i++)
3038 ga_append(&b->bv_ga, blob_get(b2, i));
3039
3040 clear_tv(tv1);
3041 rettv_blob_set(tv1, b);
3042 }
3043}
3044
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003045/*
3046 * Make a copy of list "tv1" and append list "tv2".
3047 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 int
3049eval_addlist(typval_T *tv1, typval_T *tv2)
3050{
3051 typval_T var3;
3052
3053 // concatenate Lists
3054 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3055 {
3056 clear_tv(tv1);
3057 clear_tv(tv2);
3058 return FAIL;
3059 }
3060 clear_tv(tv1);
3061 *tv1 = var3;
3062 return OK;
3063}
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003066 * Handle the bitwise left/right shift operator expression:
3067 * var1 << var2
3068 * var1 >> var2
3069 *
3070 * "arg" must point to the first non-white of the expression.
3071 * "arg" is advanced to just after the recognized expression.
3072 *
3073 * Return OK or FAIL.
3074 */
3075 static int
3076eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3077{
3078 /*
3079 * Get the first expression.
3080 */
3081 if (eval6(arg, rettv, evalarg) == FAIL)
3082 return FAIL;
3083
3084 /*
3085 * Repeat computing, until no '<<' or '>>' is following.
3086 */
3087 for (;;)
3088 {
3089 char_u *p;
3090 int getnext;
3091 exprtype_T type;
3092 int evaluate;
3093 typval_T var2;
3094 int vim9script;
3095
3096 p = eval_next_non_blank(*arg, evalarg, &getnext);
3097 if (p[0] == '<' && p[1] == '<')
3098 type = EXPR_LSHIFT;
3099 else if (p[0] == '>' && p[1] == '>')
3100 type = EXPR_RSHIFT;
3101 else
3102 return OK;
3103
3104 // Handle a bitwise left or right shift operator
3105 if (rettv->v_type != VAR_NUMBER)
3106 {
3107 // left operand should be a number
3108 emsg(_(e_bitshift_ops_must_be_number));
3109 clear_tv(rettv);
3110 return FAIL;
3111 }
3112
3113 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3114 vim9script = in_vim9script();
3115 if (getnext)
3116 {
3117 *arg = eval_next_line(*arg, evalarg);
3118 p = *arg;
3119 }
3120 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3121 {
3122 error_white_both(*arg, 2);
3123 clear_tv(rettv);
3124 return FAIL;
3125 }
3126
3127 /*
3128 * Get the second variable.
3129 */
3130 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3131 {
3132 error_white_both(p, 2);
3133 clear_tv(rettv);
3134 return FAIL;
3135 }
3136 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3137 if (eval6(arg, &var2, evalarg) == FAIL)
3138 {
3139 clear_tv(rettv);
3140 return FAIL;
3141 }
3142
3143 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3144 {
3145 // right operand should be a positive number
3146 if (var2.v_type != VAR_NUMBER)
3147 emsg(_(e_bitshift_ops_must_be_number));
3148 else
3149 emsg(_(e_bitshift_ops_must_be_postive));
3150 clear_tv(rettv);
3151 clear_tv(&var2);
3152 return FAIL;
3153 }
3154
3155 if (evaluate)
3156 {
3157 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3158 // shifting more bits than we have always results in zero
3159 rettv->vval.v_number = 0;
3160 else if (type == EXPR_LSHIFT)
3161 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003162 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003163 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003164 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003165 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003166 }
3167
3168 clear_tv(&var2);
3169 }
3170
3171 return OK;
3172}
3173
3174/*
3175 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003176 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003178 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003179 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 *
3181 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003182 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 *
3184 * Return OK or FAIL.
3185 */
3186 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003187eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003190 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003192 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 return FAIL;
3194
3195 /*
3196 * Repeat computing, until no '+', '-' or '.' is following.
3197 */
3198 for (;;)
3199 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003200 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003201 int getnext;
3202 char_u *p;
3203 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003204 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003205 int concat;
3206 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003207 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003209 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003210 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003211 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003212 p = eval_next_non_blank(*arg, evalarg, &getnext);
3213 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003214 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003215 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3216 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003218 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3219 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003220
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003221 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003222 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003224 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003225 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003226 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003227 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003228 {
mityu4ac198c2021-05-28 17:52:40 +02003229 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003230 clear_tv(rettv);
3231 return FAIL;
3232 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003233 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003234 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003235 if ((op != '+' || (rettv->v_type != VAR_LIST
3236 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003237#ifdef FEAT_FLOAT
3238 && (op == '.' || rettv->v_type != VAR_FLOAT)
3239#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003240 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003241 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003242 int error = FALSE;
3243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003244 // For "list + ...", an illegal use of the first operand as
3245 // a number cannot be determined before evaluating the 2nd
3246 // operand: if this is also a list, all is ok.
3247 // For "something . ...", "something - ..." or "non-list + ...",
3248 // we know that the first operand needs to be a string or number
3249 // without evaluating the 2nd operand. So check before to avoid
3250 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003251 if (op != '.')
3252 tv_get_number_chk(rettv, &error);
3253 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003254 {
3255 clear_tv(rettv);
3256 return FAIL;
3257 }
3258 }
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /*
3261 * Get the second variable.
3262 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003263 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003264 {
mityu89dcb4d2021-05-28 13:50:17 +02003265 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003266 clear_tv(rettv);
3267 return FAIL;
3268 }
3269 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003270 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return FAIL;
3274 }
3275
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003276 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 /*
3279 * Compute the result.
3280 */
3281 if (op == '.')
3282 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003283 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3284 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003285 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003286
Bram Moolenaar2e086612020-08-25 22:37:48 +02003287 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003288 || var2.v_type == VAR_CHANNEL
3289 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003290 semsg(_(e_using_invalid_value_as_string_str),
3291 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003292#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003293 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003294 {
3295 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3296 var2.vval.v_float);
3297 s2 = buf2;
3298 }
3299#endif
3300 else
3301 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003302 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003303 {
3304 clear_tv(rettv);
3305 clear_tv(&var2);
3306 return FAIL;
3307 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003308 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003309 clear_tv(rettv);
3310 rettv->v_type = VAR_STRING;
3311 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003313 else if (op == '+' && rettv->v_type == VAR_BLOB
3314 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003316 else if (op == '+' && rettv->v_type == VAR_LIST
3317 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003318 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003320 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 else
3323 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003324 int error = FALSE;
3325 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003326#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003327 float_T f1 = 0, f2 = 0;
3328
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003329 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003330 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331 f1 = rettv->vval.v_float;
3332 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003333 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334 else
3335#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003338 if (error)
3339 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003340 // This can only happen for "list + non-list" or
3341 // "blob + non-blob". For "non-list + ..." or
3342 // "something - ...", we returned before evaluating the
3343 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003344 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003345 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003346 return FAIL;
3347 }
3348#ifdef FEAT_FLOAT
3349 if (var2.v_type == VAR_FLOAT)
3350 f1 = n1;
3351#endif
3352 }
3353#ifdef FEAT_FLOAT
3354 if (var2.v_type == VAR_FLOAT)
3355 {
3356 f2 = var2.vval.v_float;
3357 n2 = 0;
3358 }
3359 else
3360#endif
3361 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003362 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 if (error)
3364 {
3365 clear_tv(rettv);
3366 clear_tv(&var2);
3367 return FAIL;
3368 }
3369#ifdef FEAT_FLOAT
3370 if (rettv->v_type == VAR_FLOAT)
3371 f2 = n2;
3372#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003374 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375
3376#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003377 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003378 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3379 {
3380 if (op == '+')
3381 f1 = f1 + f2;
3382 else
3383 f1 = f1 - f2;
3384 rettv->v_type = VAR_FLOAT;
3385 rettv->vval.v_float = f1;
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003388#endif
3389 {
3390 if (op == '+')
3391 n1 = n1 + n2;
3392 else
3393 n1 = n1 - n2;
3394 rettv->v_type = VAR_NUMBER;
3395 rettv->vval.v_number = n1;
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 }
3401 return OK;
3402}
3403
3404/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003405 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 * * number multiplication
3407 * / number division
3408 * % number modulo
3409 *
3410 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003411 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 *
3413 * Return OK or FAIL.
3414 */
3415 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003416eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417 char_u **arg,
3418 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003419 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003420 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003422#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003423 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003427 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003429 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
3431
3432 /*
3433 * Repeat computing, until no '*', '/' or '%' is following.
3434 */
3435 for (;;)
3436 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003437 int evaluate;
3438 int getnext;
3439 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003440 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003441 int op;
3442 varnumber_T n1, n2;
3443#ifdef FEAT_FLOAT
3444 float_T f1, f2;
3445#endif
3446 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003448 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003449 p = eval_next_non_blank(*arg, evalarg, &getnext);
3450 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003451 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003453
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003454 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003455 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003456 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003457 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003458 {
3459 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3460 {
mityu4ac198c2021-05-28 17:52:40 +02003461 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003462 clear_tv(rettv);
3463 return FAIL;
3464 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003465 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003468#ifdef FEAT_FLOAT
3469 f1 = 0;
3470 f2 = 0;
3471#endif
3472 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003473 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003475#ifdef FEAT_FLOAT
3476 if (rettv->v_type == VAR_FLOAT)
3477 {
3478 f1 = rettv->vval.v_float;
3479 use_float = TRUE;
3480 n1 = 0;
3481 }
3482 else
3483#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003485 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003486 if (error)
3487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 else
3490 n1 = 0;
3491
3492 /*
3493 * Get the second variable.
3494 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003495 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3496 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003497 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003498 clear_tv(rettv);
3499 return FAIL;
3500 }
3501 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003502 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003505 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003507#ifdef FEAT_FLOAT
3508 if (var2.v_type == VAR_FLOAT)
3509 {
3510 if (!use_float)
3511 {
3512 f1 = n1;
3513 use_float = TRUE;
3514 }
3515 f2 = var2.vval.v_float;
3516 n2 = 0;
3517 }
3518 else
3519#endif
3520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003521 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522 clear_tv(&var2);
3523 if (error)
3524 return FAIL;
3525#ifdef FEAT_FLOAT
3526 if (use_float)
3527 f2 = n2;
3528#endif
3529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 /*
3532 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535#ifdef FEAT_FLOAT
3536 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003538 if (op == '*')
3539 f1 = f1 * f2;
3540 else if (op == '/')
3541 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003542# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003543 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003544 if (f2 == 0.0)
3545 {
3546 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003547 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003548 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003549 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003550 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003551 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003552 }
3553 else
3554 f1 = f1 / f2;
3555# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003556 // We rely on the floating point library to handle divide
3557 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003559# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003562 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003563 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 return FAIL;
3565 }
3566 rettv->v_type = VAR_FLOAT;
3567 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003572 int failed = FALSE;
3573
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574 if (op == '*')
3575 n1 = n1 * n2;
3576 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003577 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003579 n1 = num_modulus(n1, n2, &failed);
3580 if (failed)
3581 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003582
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 rettv->v_type = VAR_NUMBER;
3584 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 }
3588
3589 return OK;
3590}
3591
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003592/*
3593 * Handle a type cast before a base level expression.
3594 * "arg" must point to the first non-white of the expression.
3595 * "arg" is advanced to just after the recognized expression.
3596 * Return OK or FAIL.
3597 */
3598 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003599eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003600 char_u **arg,
3601 typval_T *rettv,
3602 evalarg_T *evalarg,
3603 int want_string) // after "." operator
3604{
3605 type_T *want_type = NULL;
3606 garray_T type_list; // list of pointers to allocated types
3607 int res;
3608 int evaluate = evalarg == NULL ? 0
3609 : (evalarg->eval_flags & EVAL_EVALUATE);
3610
3611 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003612 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3613 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003614 {
3615 ++*arg;
3616 ga_init2(&type_list, sizeof(type_T *), 10);
3617 want_type = parse_type(arg, &type_list, TRUE);
3618 if (want_type == NULL && (evaluate || **arg != '>'))
3619 {
3620 clear_type_list(&type_list);
3621 return FAIL;
3622 }
3623
3624 if (**arg != '>')
3625 {
3626 if (*skipwhite(*arg) == '>')
3627 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3628 else
3629 emsg(_(e_missing_gt));
3630 clear_type_list(&type_list);
3631 return FAIL;
3632 }
3633 ++*arg;
3634 *arg = skipwhite_and_linebreak(*arg, evalarg);
3635 }
3636
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003637 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003638
3639 if (want_type != NULL && evaluate)
3640 {
3641 if (res == OK)
3642 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003643 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3644 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003645
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003646 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003647 {
3648 if (want_type == &t_bool && actual != &t_bool
3649 && (actual->tt_flags & TTFLAG_BOOL_OK))
3650 {
3651 int n = tv2bool(rettv);
3652
3653 // can use "0" and "1" for boolean in some places
3654 clear_tv(rettv);
3655 rettv->v_type = VAR_BOOL;
3656 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3657 }
3658 else
3659 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003660 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003661
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003662 where.wt_variable = TRUE;
3663 res = check_type(want_type, actual, TRUE, where);
3664 }
3665 }
3666 }
3667 clear_type_list(&type_list);
3668 }
3669
3670 return res;
3671}
3672
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003673 int
3674eval_leader(char_u **arg, int vim9)
3675{
3676 char_u *s = *arg;
3677 char_u *p = *arg;
3678
3679 while (*p == '!' || *p == '-' || *p == '+')
3680 {
3681 char_u *n = skipwhite(p + 1);
3682
3683 // ++, --, -+ and +- are not accepted in Vim9 script
3684 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3685 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003686 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003687 return FAIL;
3688 }
3689 p = n;
3690 }
3691 *arg = p;
3692 return OK;
3693}
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003696 * Check for a predefined value "true", "false" and "null.*".
3697 * Return OK when recognized.
3698 */
3699 int
3700handle_predefined(char_u *s, int len, typval_T *rettv)
3701{
3702 switch (len)
3703 {
3704 case 4: if (STRNCMP(s, "true", 4) == 0)
3705 {
3706 rettv->v_type = VAR_BOOL;
3707 rettv->vval.v_number = VVAL_TRUE;
3708 return OK;
3709 }
3710 if (STRNCMP(s, "null", 4) == 0)
3711 {
3712 rettv->v_type = VAR_SPECIAL;
3713 rettv->vval.v_number = VVAL_NULL;
3714 return OK;
3715 }
3716 break;
3717 case 5: if (STRNCMP(s, "false", 5) == 0)
3718 {
3719 rettv->v_type = VAR_BOOL;
3720 rettv->vval.v_number = VVAL_FALSE;
3721 return OK;
3722 }
3723 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003724 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3725 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003727 rettv->v_type = VAR_JOB;
3728 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003729#else
3730 rettv->v_type = VAR_SPECIAL;
3731 rettv->vval.v_number = VVAL_NULL;
3732#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003733 return OK;
3734 }
3735 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003736 case 9:
3737 if (STRNCMP(s, "null_", 5) != 0)
3738 break;
3739 if (STRNCMP(s + 5, "list", 4) == 0)
3740 {
3741 rettv->v_type = VAR_LIST;
3742 rettv->vval.v_list = NULL;
3743 return OK;
3744 }
3745 if (STRNCMP(s + 5, "dict", 4) == 0)
3746 {
3747 rettv->v_type = VAR_DICT;
3748 rettv->vval.v_dict = NULL;
3749 return OK;
3750 }
3751 if (STRNCMP(s + 5, "blob", 4) == 0)
3752 {
3753 rettv->v_type = VAR_BLOB;
3754 rettv->vval.v_blob = NULL;
3755 return OK;
3756 }
3757 break;
3758 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3759 {
3760 rettv->v_type = VAR_STRING;
3761 rettv->vval.v_string = NULL;
3762 return OK;
3763 }
3764 break;
3765 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003766 if (STRNCMP(s, "null_channel", 12) == 0)
3767 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003769 rettv->v_type = VAR_CHANNEL;
3770 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003771#else
3772 rettv->v_type = VAR_SPECIAL;
3773 rettv->vval.v_number = VVAL_NULL;
3774#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003775 return OK;
3776 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003777 if (STRNCMP(s, "null_partial", 12) == 0)
3778 {
3779 rettv->v_type = VAR_PARTIAL;
3780 rettv->vval.v_partial = NULL;
3781 return OK;
3782 }
3783 break;
3784 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3785 {
3786 rettv->v_type = VAR_FUNC;
3787 rettv->vval.v_string = NULL;
3788 return OK;
3789 }
3790 break;
3791 }
3792 return FAIL;
3793}
3794
3795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * Handle sixth level expression:
3797 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003798 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003799 * "string" string constant
3800 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 * &option-name option value
3802 * @r register contents
3803 * identifier variable value
3804 * function() function call
3805 * $VAR environment variable
3806 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003809 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003810 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 *
3812 * Also handle:
3813 * ! in front logical NOT
3814 * - in front unary minus
3815 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003816 * trailing [] subscript in String or List
3817 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003818 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 *
3820 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003821 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 *
3823 * Return OK or FAIL.
3824 */
3825 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003826eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827 char_u **arg,
3828 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003829 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003832 int evaluate = evalarg != NULL
3833 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 int len;
3835 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003836 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 char_u *start_leader, *end_leader;
3838 int ret = OK;
3839 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003840 static int recurse = 0;
3841 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003844 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003850 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
3852 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003853 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 end_leader = *arg;
3856
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003857 if (**arg == '.' && (!isdigit(*(*arg + 1))
3858#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003859 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003860#endif
3861 ))
3862 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003863 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003864 ++*arg;
3865 return FAIL;
3866 }
3867
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003868 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003869 // and crash. With MSVC the stack is smaller.
3870 if (recurse ==
3871#ifdef _MSC_VER
3872 300
3873#else
3874 1000
3875#endif
3876 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003877 {
3878 semsg(_(e_expression_too_recursive_str), *arg);
3879 return FAIL;
3880 }
3881 ++recurse;
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 switch (**arg)
3884 {
3885 /*
3886 * Number constant.
3887 */
3888 case '0':
3889 case '1':
3890 case '2':
3891 case '3':
3892 case '4':
3893 case '5':
3894 case '6':
3895 case '7':
3896 case '8':
3897 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003898 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003899
3900 // Apply prefixed "-" and "+" now. Matters especially when
3901 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003902 if (ret == OK && evaluate && end_leader > start_leader
3903 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003904 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 break;
3906
3907 /*
3908 * String constant: "string".
3909 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003910 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 break;
3912
3913 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003914 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003916 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003917 break;
3918
3919 /*
3920 * List: [expr, expr]
3921 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003922 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 break;
3924
3925 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003926 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003927 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003928 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003929 {
3930 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3931 }
3932 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003933 {
3934 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003935 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003936 }
3937 else
3938 ret = NOTDONE;
3939 break;
3940
3941 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003942 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003943 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003944 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003945 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003946 ret = NOTDONE;
3947 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003948 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003949 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003950 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003951 break;
3952
3953 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003954 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003956 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 break;
3958
3959 /*
3960 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003961 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 */
LemonBoy2eaef102022-05-06 13:14:50 +01003963 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3964 ret = eval_interp_string(arg, rettv, evaluate);
3965 else
3966 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 break;
3968
3969 /*
3970 * Register contents: @r.
3971 */
3972 case '@': ++*arg;
3973 if (evaluate)
3974 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003975 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003976 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003977 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003978 emsg_invreg(**arg);
3979 else
3980 {
3981 rettv->v_type = VAR_STRING;
3982 rettv->vval.v_string = get_reg_contents(**arg,
3983 GREG_EXPR_SRC);
3984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 if (**arg != NUL)
3987 ++*arg;
3988 break;
3989
3990 /*
3991 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003992 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003994 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003995 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003996 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003997 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003998 if (ret == OK && evaluate)
3999 {
4000 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4001
Bram Moolenaara9931532021-06-12 15:58:16 +02004002 // Compile it here to get the return type. The return
4003 // type is optional, when it's missing use t_unknown.
4004 // This is recognized in compile_return().
4005 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4006 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004007 if (compile_def_function(ufunc, FALSE,
4008 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004009 {
4010 clear_tv(rettv);
4011 ret = FAIL;
4012 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004013 }
4014 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004015 if (ret == NOTDONE)
4016 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004017 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004018 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004019
Bram Moolenaar9215f012020-06-27 21:18:00 +02004020 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004021 if (**arg == ')')
4022 ++*arg;
4023 else if (ret == OK)
4024 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004025 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004026 clear_tv(rettv);
4027 ret = FAIL;
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 break;
4031
Bram Moolenaar8c711452005-01-14 21:53:12 +00004032 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 break;
4034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004035
4036 if (ret == NOTDONE)
4037 {
4038 /*
4039 * Must be a variable or function name.
4040 * Can also be a curly-braces kind of name: {expr}.
4041 */
4042 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004043 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 if (alias != NULL)
4045 s = alias;
4046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004047 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 ret = FAIL;
4049 else
4050 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004051 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4052
Bram Moolenaar4525a572022-02-13 11:57:33 +00004053 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004054 {
4055 emsg(_(e_cannot_use_underscore_here));
4056 ret = FAIL;
4057 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004058 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004059 && s[0] == 's' && s[1] == ':')
4060 {
4061 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4062 ret = FAIL;
4063 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004064 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004065 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004066 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004067 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004068 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004069 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004070 else if (flags & EVAL_CONSTANT)
4071 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004072 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004073 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004074 // get the value of "true", "false", etc. or a variable
4075 ret = FAIL;
4076 if (vim9script)
4077 ret = handle_predefined(s, len, rettv);
4078 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004079 {
4080 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004081 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004082 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004083 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004085 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004086 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004087 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004088 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004089 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004091 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004092 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004093 }
4094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4096 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004097 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004098 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 /*
4101 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4102 */
4103 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004104 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004105
4106 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004107 return ret;
4108}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004110/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004111 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004112 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004113 * Adjusts "end_leaderp" until it is at "start_leader".
4114 */
4115 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004116eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004117 typval_T *rettv,
4118 int numeric_only,
4119 char_u *start_leader,
4120 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004121{
4122 char_u *end_leader = *end_leaderp;
4123 int ret = OK;
4124 int error = FALSE;
4125 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004126 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004127 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004128#ifdef FEAT_FLOAT
4129 float_T f = 0.0;
4130
4131 if (rettv->v_type == VAR_FLOAT)
4132 f = rettv->vval.v_float;
4133 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004134#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004135 {
4136 while (VIM_ISWHITE(end_leader[-1]))
4137 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004138 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004139 val = tv2bool(rettv);
4140 else
4141 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004142 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004143 if (error)
4144 {
4145 clear_tv(rettv);
4146 ret = FAIL;
4147 }
4148 else
4149 {
4150 while (end_leader > start_leader)
4151 {
4152 --end_leader;
4153 if (*end_leader == '!')
4154 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004155 if (numeric_only)
4156 {
4157 ++end_leader;
4158 break;
4159 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004160#ifdef FEAT_FLOAT
4161 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004162 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004163 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004164 {
4165 rettv->v_type = VAR_BOOL;
4166 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4167 }
4168 else
4169 f = !f;
4170 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004171 else
4172#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004173 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004174 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004175 type = VAR_BOOL;
4176 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004177 }
4178 else if (*end_leader == '-')
4179 {
4180#ifdef FEAT_FLOAT
4181 if (rettv->v_type == VAR_FLOAT)
4182 f = -f;
4183 else
4184#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004185 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004186 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004187 type = VAR_NUMBER;
4188 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004189 }
4190 }
4191#ifdef FEAT_FLOAT
4192 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004195 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004198#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004200 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004201 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004202 rettv->v_type = type;
4203 else
4204 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004205 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004208 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return ret;
4210}
4211
4212/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004213 * Call the function referred to in "rettv".
4214 */
4215 static int
4216call_func_rettv(
4217 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004218 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004219 typval_T *rettv,
4220 int evaluate,
4221 dict_T *selfdict,
4222 typval_T *basetv)
4223{
4224 partial_T *pt = NULL;
4225 funcexe_T funcexe;
4226 typval_T functv;
4227 char_u *s;
4228 int ret;
4229
4230 // need to copy the funcref so that we can clear rettv
4231 if (evaluate)
4232 {
4233 functv = *rettv;
4234 rettv->v_type = VAR_UNKNOWN;
4235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004236 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004237 if (functv.v_type == VAR_PARTIAL)
4238 {
4239 pt = functv.vval.v_partial;
4240 s = partial_name(pt);
4241 }
4242 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004243 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004244 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004245 if (s == NULL || *s == NUL)
4246 {
4247 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004248 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004249 goto theend;
4250 }
4251 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252 }
4253 else
4254 s = (char_u *)"";
4255
Bram Moolenaara80faa82020-04-12 19:37:17 +02004256 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004257 funcexe.fe_firstline = curwin->w_cursor.lnum;
4258 funcexe.fe_lastline = curwin->w_cursor.lnum;
4259 funcexe.fe_evaluate = evaluate;
4260 funcexe.fe_partial = pt;
4261 funcexe.fe_selfdict = selfdict;
4262 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004263 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004264
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004265theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 // Clear the funcref afterwards, so that deleting it while
4267 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004268 if (evaluate)
4269 clear_tv(&functv);
4270
4271 return ret;
4272}
4273
4274/*
4275 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004276 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004277 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4278 */
4279 static int
4280eval_lambda(
4281 char_u **arg,
4282 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004283 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004285{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004286 int evaluate = evalarg != NULL
4287 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004288 typval_T base = *rettv;
4289 int ret;
4290
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004291 rettv->v_type = VAR_UNKNOWN;
4292
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004293 if (**arg == '{')
4294 {
4295 // ->{lambda}()
4296 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4297 }
4298 else
4299 {
4300 // ->(lambda)()
4301 ++*arg;
4302 ret = eval1(arg, rettv, evalarg);
4303 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004304 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004305 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004306 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004307 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004308 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004309 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4310 && rettv->v_type != VAR_PARTIAL)
4311 {
4312 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4313 return FAIL;
4314 }
4315 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004316 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004317 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004318 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004319
4320 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004321 {
4322 if (verbose)
4323 {
4324 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004325 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004326 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004327 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004328 }
4329 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004330 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004331 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004332 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004333 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004334
4335 // Clear the funcref afterwards, so that deleting it while
4336 // evaluating the arguments is possible (see test55).
4337 if (evaluate)
4338 clear_tv(&base);
4339
4340 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004341}
4342
4343/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004344 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004345 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004346 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4347 */
4348 static int
4349eval_method(
4350 char_u **arg,
4351 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004352 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004353 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004354{
4355 char_u *name;
4356 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004357 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004358 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004359 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004360 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004361 int evaluate = evalarg != NULL
4362 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004363
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004364 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004365
Bram Moolenaarac92e252019-08-03 21:58:38 +02004366 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004367 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004368 if (alias != NULL)
4369 name = alias;
4370
4371 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004372 {
4373 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004374 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004375 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004376 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004377 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004378 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004379 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004380
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004381 // If there is no "(" immediately following, but there is further on,
4382 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4383 // Does not handle anything where "(" is part of the expression.
4384 *arg = skipwhite(*arg);
4385
4386 if (**arg != '(' && alias == NULL
4387 && (paren = vim_strchr(*arg, '(')) != NULL)
4388 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004389 char_u *deref;
4390
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004391 *arg = name;
4392 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004393 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4394 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004395 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004396 *arg = name + len;
4397 ret = FAIL;
4398 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004399 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004400 {
4401 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004402 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004403 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004404 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004405 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004406
4407 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004408 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004409 *arg = skipwhite(*arg);
4410
4411 if (**arg != '(')
4412 {
4413 if (verbose)
4414 semsg(_(e_missing_parenthesis_str), name);
4415 ret = FAIL;
4416 }
4417 else if (VIM_ISWHITE((*arg)[-1]))
4418 {
4419 if (verbose)
4420 emsg(_(e_no_white_space_allowed_before_parenthesis));
4421 ret = FAIL;
4422 }
4423 else
4424 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004425 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004426 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004427 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004428
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004429 // Clear the funcref afterwards, so that deleting it while
4430 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004431 if (evaluate)
4432 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004433 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004434
Bram Moolenaarac92e252019-08-03 21:58:38 +02004435 return ret;
4436}
4437
4438/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004439 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4440 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4442 */
4443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004444eval_index(
4445 char_u **arg,
4446 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004447 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004450 int evaluate = evalarg != NULL
4451 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004453 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004456 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004457 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004459 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4460 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004462 init_tv(&var1);
4463 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004464 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 /*
4467 * dict.name
4468 */
4469 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004470 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004471 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004472 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004474 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 }
4476 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004478 /*
4479 * something[idx]
4480 *
4481 * Get the (first) variable from inside the [].
4482 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004483 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 if (**arg == ':')
4485 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004486 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004487 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004488 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004489 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004490 semsg(_(e_white_space_required_before_and_after_str_at_str),
4491 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004492 clear_tv(&var1);
4493 return FAIL;
4494 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004495 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004496 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004497 int error = FALSE;
4498
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004499#ifdef FEAT_FLOAT
4500 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004501 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004502 && var1.v_type == VAR_FLOAT)
4503 {
4504 var1.vval.v_string = typval_tostring(&var1, TRUE);
4505 var1.v_type = VAR_STRING;
4506 }
4507#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004508 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004509 tv_get_number_chk(&var1, &error);
4510 else
4511 error = tv_get_string_chk(&var1) == NULL;
4512 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004513 {
4514 // not a number or string
4515 clear_tv(&var1);
4516 return FAIL;
4517 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004519
4520 /*
4521 * Get the second variable from inside the [:].
4522 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004523 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 if (**arg == ':')
4525 {
4526 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004527 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004528 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004529 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004530 semsg(_(e_white_space_required_before_and_after_str_at_str),
4531 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004532 if (!empty1)
4533 clear_tv(&var1);
4534 return FAIL;
4535 }
4536 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537 if (**arg == ']')
4538 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004539 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004541 if (!empty1)
4542 clear_tv(&var1);
4543 return FAIL;
4544 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004545 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004548 if (!empty1)
4549 clear_tv(&var1);
4550 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 return FAIL;
4552 }
4553 }
4554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004556 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 if (**arg != ']')
4558 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004559 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004560 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 clear_tv(&var1);
4562 if (range)
4563 clear_tv(&var2);
4564 return FAIL;
4565 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004566 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004567 }
4568
4569 if (evaluate)
4570 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004571 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004572 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004573 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004574
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004575 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004577 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004578 clear_tv(&var2);
4579 return res;
4580 }
4581 return OK;
4582}
4583
4584/*
4585 * Check if "rettv" can have an [index] or [sli:ce]
4586 */
4587 int
4588check_can_index(typval_T *rettv, int evaluate, int verbose)
4589{
4590 switch (rettv->v_type)
4591 {
4592 case VAR_FUNC:
4593 case VAR_PARTIAL:
4594 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004595 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004596 return FAIL;
4597 case VAR_FLOAT:
4598#ifdef FEAT_FLOAT
4599 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004600 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004601 return FAIL;
4602#endif
4603 case VAR_BOOL:
4604 case VAR_SPECIAL:
4605 case VAR_JOB:
4606 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004607 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004608 if (verbose)
4609 emsg(_(e_cannot_index_special_variable));
4610 return FAIL;
4611 case VAR_UNKNOWN:
4612 case VAR_ANY:
4613 case VAR_VOID:
4614 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004616 emsg(_(e_cannot_index_special_variable));
4617 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004619 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004621 case VAR_STRING:
4622 case VAR_LIST:
4623 case VAR_DICT:
4624 case VAR_BLOB:
4625 break;
4626 case VAR_NUMBER:
4627 if (in_vim9script())
4628 emsg(_(e_cannot_index_number));
4629 break;
4630 }
4631 return OK;
4632}
4633
4634/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004635 * slice() function
4636 */
4637 void
4638f_slice(typval_T *argvars, typval_T *rettv)
4639{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004640 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004641 && ((argvars[0].v_type != VAR_STRING
4642 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004643 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004644 && check_for_list_arg(argvars, 0) == FAIL)
4645 || check_for_number_arg(argvars, 1) == FAIL
4646 || check_for_opt_number_arg(argvars, 2) == FAIL))
4647 return;
4648
Bram Moolenaar6601b622021-01-13 21:47:15 +01004649 if (check_can_index(argvars, TRUE, FALSE) == OK)
4650 {
4651 copy_tv(argvars, rettv);
4652 eval_index_inner(rettv, TRUE, argvars + 1,
4653 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4654 TRUE, NULL, 0, FALSE);
4655 }
4656}
4657
4658/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004659 * Apply index or range to "rettv".
4660 * "var1" is the first index, NULL for [:expr].
4661 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004662 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4663 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004664 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4665 */
4666 int
4667eval_index_inner(
4668 typval_T *rettv,
4669 int is_range,
4670 typval_T *var1,
4671 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004672 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673 char_u *key,
4674 int keylen,
4675 int verbose)
4676{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004677 varnumber_T n1, n2 = 0;
4678 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679
4680 n1 = 0;
4681 if (var1 != NULL && rettv->v_type != VAR_DICT)
4682 n1 = tv_get_number(var1);
4683
4684 if (is_range)
4685 {
4686 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004688 if (verbose)
4689 emsg(_(e_cannot_slice_dictionary));
4690 return FAIL;
4691 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004692 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004693 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004694 else
4695 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004696 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004697
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004698 switch (rettv->v_type)
4699 {
4700 case VAR_UNKNOWN:
4701 case VAR_ANY:
4702 case VAR_VOID:
4703 case VAR_FUNC:
4704 case VAR_PARTIAL:
4705 case VAR_FLOAT:
4706 case VAR_BOOL:
4707 case VAR_SPECIAL:
4708 case VAR_JOB:
4709 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004710 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004711 break; // not evaluating, skipping over subscript
4712
4713 case VAR_NUMBER:
4714 case VAR_STRING:
4715 {
4716 char_u *s = tv_get_string(rettv);
4717
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004719 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004720 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004721 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004722 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004723 else
4724 s = char_from_string(s, n1);
4725 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004726 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // The resulting variable is a substring. If the indexes
4729 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 if (n1 < 0)
4731 {
4732 n1 = len + n1;
4733 if (n1 < 0)
4734 n1 = 0;
4735 }
4736 if (n2 < 0)
4737 n2 = len + n2;
4738 else if (n2 >= len)
4739 n2 = len;
4740 if (n1 >= len || n2 < 0 || n1 > n2)
4741 s = NULL;
4742 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004743 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 }
4745 else
4746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 // The resulting variable is a string of a single
4748 // character. If the index is too big or negative the
4749 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 if (n1 >= len || n1 < 0)
4751 s = NULL;
4752 else
4753 s = vim_strnsave(s + n1, 1);
4754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
4756 rettv->v_type = VAR_STRING;
4757 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004758 }
4759 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004761 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004762 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4763 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004764 break;
4765
4766 case VAR_LIST:
4767 if (var1 == NULL)
4768 n1 = 0;
4769 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004770 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004771 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004772 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 return FAIL;
4774 break;
4775
4776 case VAR_DICT:
4777 {
4778 dictitem_T *item;
4779 typval_T tmp;
4780
4781 if (key == NULL)
4782 {
4783 key = tv_get_string_chk(var1);
4784 if (key == NULL)
4785 return FAIL;
4786 }
4787
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004788 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004789
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004790 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004791 {
4792 if (verbose)
4793 {
4794 if (keylen > 0)
4795 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004796 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004797 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004798 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004799 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004800
4801 copy_tv(&item->di_tv, &tmp);
4802 clear_tv(rettv);
4803 *rettv = tmp;
4804 }
4805 break;
4806 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 return OK;
4808}
4809
4810/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004811 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004812 */
4813 char_u *
4814partial_name(partial_T *pt)
4815{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004816 if (pt != NULL)
4817 {
4818 if (pt->pt_name != NULL)
4819 return pt->pt_name;
4820 if (pt->pt_func != NULL)
4821 return pt->pt_func->uf_name;
4822 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004823 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004824}
4825
Bram Moolenaarddecc252016-04-06 22:59:37 +02004826 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004827partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004828{
4829 int i;
4830
4831 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004832 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004833 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004834 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004835 if (pt->pt_name != NULL)
4836 {
4837 func_unref(pt->pt_name);
4838 vim_free(pt->pt_name);
4839 }
4840 else
4841 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004842
Bram Moolenaar54656012021-06-09 20:50:46 +02004843 // "out_up" is no longer used, decrement refcount on partial that owns it.
4844 partial_unref(pt->pt_outer.out_up_partial);
4845
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004846 // Using pt_outer from another partial.
4847 partial_unref(pt->pt_outer_partial);
4848
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004849 // Decrease the reference count for the context of a closure. If down
4850 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004851 if (pt->pt_funcstack != NULL)
4852 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004853 --pt->pt_funcstack->fs_refcount;
4854 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004855 }
4856
Bram Moolenaarddecc252016-04-06 22:59:37 +02004857 vim_free(pt);
4858}
4859
4860/*
4861 * Unreference a closure: decrement the reference count and free it when it
4862 * becomes zero.
4863 */
4864 void
4865partial_unref(partial_T *pt)
4866{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004867 if (pt != NULL)
4868 {
4869 if (--pt->pt_refcount <= 0)
4870 partial_free(pt);
4871
4872 // If the reference count goes down to one, the funcstack may be the
4873 // only reference and can be freed if no other partials reference it.
4874 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4875 funcstack_check_refcount(pt->pt_funcstack);
4876 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004877}
4878
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004879/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004880 * Return the next (unique) copy ID.
4881 * Used for serializing nested structures.
4882 */
4883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004884get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004885{
4886 current_copyID += COPYID_INC;
4887 return current_copyID;
4888}
4889
4890/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004891 * Garbage collection for lists and dictionaries.
4892 *
4893 * We use reference counts to be able to free most items right away when they
4894 * are no longer used. But for composite items it's possible that it becomes
4895 * unused while the reference count is > 0: When there is a recursive
4896 * reference. Example:
4897 * :let l = [1, 2, 3]
4898 * :let d = {9: l}
4899 * :let l[1] = d
4900 *
4901 * Since this is quite unusual we handle this with garbage collection: every
4902 * once in a while find out which lists and dicts are not referenced from any
4903 * variable.
4904 *
4905 * Here is a good reference text about garbage collection (refers to Python
4906 * but it applies to all reference-counting mechanisms):
4907 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004908 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004909
4910/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004911 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004912 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004913 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004914 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004915 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004916garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004917{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004918 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004919 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004920 buf_T *buf;
4921 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004922 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004923 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004924
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004925 if (!testing)
4926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004928 want_garbage_collect = FALSE;
4929 may_garbage_collect = FALSE;
4930 garbage_collect_at_exit = FALSE;
4931 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004932
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004933 // The execution stack can grow big, limit the size.
4934 if (exestack.ga_maxlen - exestack.ga_len > 500)
4935 {
4936 size_t new_len;
4937 char_u *pp;
4938 int n;
4939
4940 // Keep 150% of the current size, with a minimum of the growth size.
4941 n = exestack.ga_len / 2;
4942 if (n < exestack.ga_growsize)
4943 n = exestack.ga_growsize;
4944
4945 // Don't make it bigger though.
4946 if (exestack.ga_len + n < exestack.ga_maxlen)
4947 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004948 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004949 pp = vim_realloc(exestack.ga_data, new_len);
4950 if (pp == NULL)
4951 return FAIL;
4952 exestack.ga_maxlen = exestack.ga_len + n;
4953 exestack.ga_data = pp;
4954 }
4955 }
4956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // We advance by two because we add one for items referenced through
4958 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004959 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004960
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004961 /*
4962 * 1. Go through all accessible variables and mark all lists and dicts
4963 * with copyID.
4964 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 // Don't free variables in the previous_funccal list unless they are only
4967 // referenced through previous_funccal. This must be first, because if
4968 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004972 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004975 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004976 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4977 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004979 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004980 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004981 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4982 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004983 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004984 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4985 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004986#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004987 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004988 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4989 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004990 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004991 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004992 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4993 NULL, NULL);
4994#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004997 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004998 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4999 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005001 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005004 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005007 abort = abort || set_ref_in_functions(copyID);
5008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005011
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005012 // funcstacks keep variables for closures
5013 abort = abort || set_ref_in_funcstacks(copyID);
5014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005016 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005017
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005018 // callbacks in buffers
5019 abort = abort || set_ref_in_buffers(copyID);
5020
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005021 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5022 abort = abort || set_ref_in_insexpand_funcs(copyID);
5023
5024 // 'operatorfunc' callback
5025 abort = abort || set_ref_in_opfunc(copyID);
5026
5027 // 'tagfunc' callback
5028 abort = abort || set_ref_in_tagfunc(copyID);
5029
5030 // 'imactivatefunc' and 'imstatusfunc' callbacks
5031 abort = abort || set_ref_in_im_funcs(copyID);
5032
Bram Moolenaar1dced572012-04-05 16:54:08 +02005033#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005034 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005035#endif
5036
Bram Moolenaardb913952012-06-29 12:54:53 +02005037#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005038 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005039#endif
5040
5041#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005042 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005043#endif
5044
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005045#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005046 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005047 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005048#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005049#ifdef FEAT_NETBEANS_INTG
5050 abort = abort || set_ref_in_nb_channel(copyID);
5051#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005052
Bram Moolenaare3188e22016-05-31 21:13:04 +02005053#ifdef FEAT_TIMERS
5054 abort = abort || set_ref_in_timer(copyID);
5055#endif
5056
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005057#ifdef FEAT_QUICKFIX
5058 abort = abort || set_ref_in_quickfix(copyID);
5059#endif
5060
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005061#ifdef FEAT_TERMINAL
5062 abort = abort || set_ref_in_term(copyID);
5063#endif
5064
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005065#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005066 abort = abort || set_ref_in_popups(copyID);
5067#endif
5068
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005069 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005070 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005071 /*
5072 * 2. Free lists and dictionaries that are not referenced.
5073 */
5074 did_free = free_unref_items(copyID);
5075
5076 /*
5077 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005078 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005079 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005081 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 else if (p_verbose > 0)
5083 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005084 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005085 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005086
5087 return did_free;
5088}
5089
5090/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005091 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005092 */
5093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005094free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005095{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005096 int did_free = FALSE;
5097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // Let all "free" functions know that we are here. This means no
5099 // dictionaries, lists, channels or jobs are to be freed, because we will
5100 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005101 in_free_unref_items = TRUE;
5102
5103 /*
5104 * PASS 1: free the contents of the items. We don't free the items
5105 * themselves yet, so that it is possible to decrement refcount counters
5106 */
5107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005109 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005110
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005112 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005113
5114#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 // Go through the list of jobs and free items without the copyID. This
5116 // must happen before doing channels, because jobs refer to channels, but
5117 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005118 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005121 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5122#endif
5123
5124 /*
5125 * PASS 2: free the items themselves.
5126 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005127 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005128 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005129
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005130#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // Go through the list of jobs and free items without the copyID. This
5132 // must happen before doing channels, because jobs refer to channels, but
5133 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005134 free_unused_jobs(copyID, COPYID_MASK);
5135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005136 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005137 free_unused_channels(copyID, COPYID_MASK);
5138#endif
5139
5140 in_free_unref_items = FALSE;
5141
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005142 return did_free;
5143}
5144
5145/*
5146 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005147 * "list_stack" is used to add lists to be marked. Can be NULL.
5148 *
5149 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005150 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005152set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153{
5154 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005155 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005156 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 hashtab_T *cur_ht;
5158 ht_stack_T *ht_stack = NULL;
5159 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005160
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005161 cur_ht = ht;
5162 for (;;)
5163 {
5164 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 // Mark each item in the hashtab. If the item contains a hashtab
5167 // it is added to ht_stack, if it contains a list it is added to
5168 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005169 todo = (int)cur_ht->ht_used;
5170 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5171 if (!HASHITEM_EMPTY(hi))
5172 {
5173 --todo;
5174 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5175 &ht_stack, list_stack);
5176 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005178
5179 if (ht_stack == NULL)
5180 break;
5181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005182 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005183 cur_ht = ht_stack->ht;
5184 tempitem = ht_stack;
5185 ht_stack = ht_stack->prev;
5186 free(tempitem);
5187 }
5188
5189 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005190}
5191
Dominique Pelle748b3082022-01-08 12:41:16 +00005192#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5193 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005194/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005195 * Mark a dict and its items with "copyID".
5196 * Returns TRUE if setting references failed somehow.
5197 */
5198 int
5199set_ref_in_dict(dict_T *d, int copyID)
5200{
5201 if (d != NULL && d->dv_copyID != copyID)
5202 {
5203 d->dv_copyID = copyID;
5204 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5205 }
5206 return FALSE;
5207}
Dominique Pelle748b3082022-01-08 12:41:16 +00005208#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005209
5210/*
5211 * Mark a list and its items with "copyID".
5212 * Returns TRUE if setting references failed somehow.
5213 */
5214 int
5215set_ref_in_list(list_T *ll, int copyID)
5216{
5217 if (ll != NULL && ll->lv_copyID != copyID)
5218 {
5219 ll->lv_copyID = copyID;
5220 return set_ref_in_list_items(ll, copyID, NULL);
5221 }
5222 return FALSE;
5223}
5224
5225/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005227 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5228 *
5229 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005232set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005233{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005234 listitem_T *li;
5235 int abort = FALSE;
5236 list_T *cur_l;
5237 list_stack_T *list_stack = NULL;
5238 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005239
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005240 cur_l = l;
5241 for (;;)
5242 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005243 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005244 // Mark each item in the list. If the item contains a hashtab
5245 // it is added to ht_stack, if it contains a list it is added to
5246 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005247 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5248 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5249 ht_stack, &list_stack);
5250 if (list_stack == NULL)
5251 break;
5252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005254 cur_l = list_stack->list;
5255 tempitem = list_stack;
5256 list_stack = list_stack->prev;
5257 free(tempitem);
5258 }
5259
5260 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005261}
5262
5263/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005264 * Mark the partial in callback 'cb' with "copyID".
5265 */
5266 int
5267set_ref_in_callback(callback_T *cb, int copyID)
5268{
5269 typval_T tv;
5270
5271 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5272 return FALSE;
5273
5274 tv.v_type = VAR_PARTIAL;
5275 tv.vval.v_partial = cb->cb_partial;
5276 return set_ref_in_item(&tv, copyID, NULL, NULL);
5277}
5278
5279/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005280 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 * "list_stack" is used to add lists to be marked. Can be NULL.
5282 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5283 *
5284 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005285 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005287set_ref_in_item(
5288 typval_T *tv,
5289 int copyID,
5290 ht_stack_T **ht_stack,
5291 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005292{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005294
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005295 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005296 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005297 dict_T *dd = tv->vval.v_dict;
5298
Bram Moolenaara03f2332016-02-06 18:09:59 +01005299 if (dd != NULL && dd->dv_copyID != copyID)
5300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005302 dd->dv_copyID = copyID;
5303 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005304 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005305 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5306 }
5307 else
5308 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005309 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5310
Bram Moolenaara03f2332016-02-06 18:09:59 +01005311 if (newitem == NULL)
5312 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005313 else
5314 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005315 newitem->ht = &dd->dv_hashtab;
5316 newitem->prev = *ht_stack;
5317 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005319 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 }
5321 }
5322 else if (tv->v_type == VAR_LIST)
5323 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005324 list_T *ll = tv->vval.v_list;
5325
Bram Moolenaara03f2332016-02-06 18:09:59 +01005326 if (ll != NULL && ll->lv_copyID != copyID)
5327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005329 ll->lv_copyID = copyID;
5330 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005332 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005333 }
5334 else
5335 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005336 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5337
Bram Moolenaara03f2332016-02-06 18:09:59 +01005338 if (newitem == NULL)
5339 abort = TRUE;
5340 else
5341 {
5342 newitem->list = ll;
5343 newitem->prev = *list_stack;
5344 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005345 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005346 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005347 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005348 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005349 else if (tv->v_type == VAR_FUNC)
5350 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005351 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005352 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005353 else if (tv->v_type == VAR_PARTIAL)
5354 {
5355 partial_T *pt = tv->vval.v_partial;
5356 int i;
5357
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005358 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005359 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005360 // Didn't see this partial yet.
5361 pt->pt_copyID = copyID;
5362
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005363 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005364
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 if (pt->pt_dict != NULL)
5366 {
5367 typval_T dtv;
5368
5369 dtv.v_type = VAR_DICT;
5370 dtv.vval.v_dict = pt->pt_dict;
5371 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5372 }
5373
5374 for (i = 0; i < pt->pt_argc; ++i)
5375 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5376 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005377 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005378 }
5379 }
5380#ifdef FEAT_JOB_CHANNEL
5381 else if (tv->v_type == VAR_JOB)
5382 {
5383 job_T *job = tv->vval.v_job;
5384 typval_T dtv;
5385
5386 if (job != NULL && job->jv_copyID != copyID)
5387 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005388 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 if (job->jv_channel != NULL)
5390 {
5391 dtv.v_type = VAR_CHANNEL;
5392 dtv.vval.v_channel = job->jv_channel;
5393 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5394 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005395 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005396 {
5397 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005398 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005399 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5400 }
5401 }
5402 }
5403 else if (tv->v_type == VAR_CHANNEL)
5404 {
5405 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005406 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005407 typval_T dtv;
5408 jsonq_T *jq;
5409 cbq_T *cq;
5410
5411 if (ch != NULL && ch->ch_copyID != copyID)
5412 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005413 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005414 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 {
5416 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5417 jq = jq->jq_next)
5418 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5419 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5420 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005421 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005422 {
5423 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005424 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5426 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005427 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005428 {
5429 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005430 dtv.vval.v_partial =
5431 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5433 }
5434 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005435 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005436 {
5437 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005438 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005439 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5440 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005441 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005442 {
5443 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005444 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005445 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5446 }
5447 }
5448 }
5449#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005450 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005451}
5452
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 * Return a string with the string representation of a variable.
5455 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005456 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005457 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005458 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005459 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005460 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005461 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5462 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005463 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005465 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005466echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005467 typval_T *tv,
5468 char_u **tofree,
5469 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005470 int copyID,
5471 int echo_style,
5472 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005473 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005475 static int recurse = 0;
5476 char_u *r = NULL;
5477
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005479 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005480 if (!did_echo_string_emsg)
5481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 // Only give this message once for a recursive call to avoid
5483 // flooding the user with errors. And stop iterating over lists
5484 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005485 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005486 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005488 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005489 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005490 }
5491 ++recurse;
5492
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 switch (tv->v_type)
5494 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005495 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005496 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005497 {
5498 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005499 r = tv->vval.v_string;
5500 if (r == NULL)
5501 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005502 }
5503 else
5504 {
5505 *tofree = string_quote(tv->vval.v_string, FALSE);
5506 r = *tofree;
5507 }
5508 break;
5509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005511 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005512 char_u buf[MAX_FUNC_NAME_LEN];
5513
5514 if (echo_style)
5515 {
LemonBoya5d35902022-04-29 21:15:02 +01005516 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5517 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005518 buf, MAX_FUNC_NAME_LEN);
5519 if (r == buf)
5520 {
5521 r = vim_strsave(buf);
5522 *tofree = r;
5523 }
5524 else
5525 *tofree = NULL;
5526 }
5527 else
5528 {
5529 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5530 : make_ufunc_name_readable(
5531 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5532 TRUE);
5533 r = *tofree;
5534 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005536 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005537
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005538 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005539 {
5540 partial_T *pt = tv->vval.v_partial;
5541 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005542 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005543 garray_T ga;
5544 int i;
5545 char_u *tf;
5546
5547 ga_init2(&ga, 1, 100);
5548 ga_concat(&ga, (char_u *)"function(");
5549 if (fname != NULL)
5550 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005551 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005552 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005553 && vim_isupper(fname[1]))
5554 {
5555 ga_concat(&ga, (char_u *)"'g:");
5556 ga_concat(&ga, fname + 1);
5557 }
5558 else
5559 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005560 vim_free(fname);
5561 }
5562 if (pt != NULL && pt->pt_argc > 0)
5563 {
5564 ga_concat(&ga, (char_u *)", [");
5565 for (i = 0; i < pt->pt_argc; ++i)
5566 {
5567 if (i > 0)
5568 ga_concat(&ga, (char_u *)", ");
5569 ga_concat(&ga,
5570 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5571 vim_free(tf);
5572 }
5573 ga_concat(&ga, (char_u *)"]");
5574 }
5575 if (pt != NULL && pt->pt_dict != NULL)
5576 {
5577 typval_T dtv;
5578
5579 ga_concat(&ga, (char_u *)", ");
5580 dtv.v_type = VAR_DICT;
5581 dtv.vval.v_dict = pt->pt_dict;
5582 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5583 vim_free(tf);
5584 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005585 // terminate with ')' and a NUL
5586 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005587
5588 *tofree = ga.ga_data;
5589 r = *tofree;
5590 break;
5591 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005592
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005593 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005594 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005595 break;
5596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598 if (tv->vval.v_list == NULL)
5599 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005600 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005601 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005602 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005603 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005604 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5605 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005606 {
5607 *tofree = NULL;
5608 r = (char_u *)"[...]";
5609 }
5610 else
5611 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005612 int old_copyID = tv->vval.v_list->lv_copyID;
5613
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005614 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005615 *tofree = list2string(tv, copyID, restore_copyID);
5616 if (restore_copyID)
5617 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005618 r = *tofree;
5619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005620 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005621
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005623 if (tv->vval.v_dict == NULL)
5624 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005625 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005626 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005627 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005628 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005629 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5630 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005631 {
5632 *tofree = NULL;
5633 r = (char_u *)"{...}";
5634 }
5635 else
5636 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005638
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005640 *tofree = dict2string(tv, copyID, restore_copyID);
5641 if (restore_copyID)
5642 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643 r = *tofree;
5644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005648 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005649 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005650 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005651 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005652 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005653 break;
5654
Bram Moolenaar835dc632016-02-07 14:27:38 +01005655 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005656 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005657#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005658 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005659 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5660 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005661 if (composite_val)
5662 {
5663 *tofree = string_quote(r, FALSE);
5664 r = *tofree;
5665 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005666#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005668
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005669 case VAR_INSTR:
5670 *tofree = NULL;
5671 r = (char_u *)"instructions";
5672 break;
5673
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005674 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005675#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005676 *tofree = NULL;
5677 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5678 r = numbuf;
5679 break;
5680#endif
5681
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005682 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005683 case VAR_SPECIAL:
5684 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005685 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005686 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005688
Bram Moolenaar8502c702014-06-17 12:51:16 +02005689 if (--recurse == 0)
5690 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005691 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692}
5693
5694/*
5695 * Return a string with the string representation of a variable.
5696 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5697 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005698 * Does not put quotes around strings, as ":echo" displays values.
5699 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5700 * May return NULL.
5701 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005703echo_string(
5704 typval_T *tv,
5705 char_u **tofree,
5706 char_u *numbuf,
5707 int copyID)
5708{
5709 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5710}
5711
5712/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005713 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5714 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005715 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005716 */
5717 int
5718buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5719{
5720 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005721 char_u *t;
5722 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005723
5724 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5725 return -1;
5726
5727 if (lnum > buf->b_ml.ml_line_count)
5728 lnum = buf->b_ml.ml_line_count;
5729
5730 str = ml_get_buf(buf, lnum, FALSE);
5731 if (str == NULL)
5732 return -1;
5733
5734 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005735 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005736
Bram Moolenaar91458462021-01-13 20:08:38 +01005737 // count the number of characters
5738 t = str;
5739 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5740 t += mb_ptr2len(t);
5741
5742 // In insert mode, when the cursor is at the end of a non-empty line,
5743 // byteidx points to the NUL character immediately past the end of the
5744 // string. In this case, add one to the character count.
5745 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5746 count++;
5747
5748 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005749}
5750
5751/*
5752 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005753 * byte index. Works only for loaded buffers. Returns -1 on failure.
5754 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005755 */
5756 int
5757buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5758{
5759 char_u *str;
5760 char_u *t;
5761
5762 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5763 return -1;
5764
5765 if (lnum > buf->b_ml.ml_line_count)
5766 lnum = buf->b_ml.ml_line_count;
5767
5768 str = ml_get_buf(buf, lnum, FALSE);
5769 if (str == NULL)
5770 return -1;
5771
5772 // Convert the character offset to a byte offset
5773 t = str;
5774 while (*t != NUL && --charidx > 0)
5775 t += mb_ptr2len(t);
5776
Bram Moolenaar91458462021-01-13 20:08:38 +01005777 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005778}
5779
5780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005782 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785var2fpos(
5786 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005787 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005788 int *fnum, // set to fnum for '0, 'A, etc.
5789 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005791 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005793 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005795 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005796 if (varp->v_type == VAR_LIST)
5797 {
5798 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005799 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005800 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005801 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005802
5803 l = varp->vval.v_list;
5804 if (l == NULL)
5805 return NULL;
5806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005808 pos.lnum = list_find_nr(l, 0L, &error);
5809 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005810 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005811 if (charcol)
5812 len = (long)mb_charlen(ml_get(pos.lnum));
5813 else
5814 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005815
Bram Moolenaarec65d772020-08-20 22:29:12 +02005816 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005818 li = list_find(l, 1L);
5819 if (li != NULL && li->li_tv.v_type == VAR_STRING
5820 && li->li_tv.vval.v_string != NULL
5821 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005822 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005823 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005824 }
5825 else
5826 {
5827 pos.col = list_find_nr(l, 1L, &error);
5828 if (error)
5829 return NULL;
5830 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005833 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005834 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005835 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005837 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005838 pos.coladd = list_find_nr(l, 2L, &error);
5839 if (error)
5840 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005841
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005842 return &pos;
5843 }
5844
Bram Moolenaarc5809432021-03-27 21:23:30 +01005845 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5846 return NULL;
5847
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005848 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005849 if (name == NULL)
5850 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005851
5852 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005853 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005854 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005855 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005856 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005857 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005858 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005859 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005860 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005861 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005862 pos = VIsual;
5863 else
5864 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005865 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005866 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005867 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005869 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005870 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5872 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005873 pos = *pp;
5874 }
5875 if (pos.lnum != 0)
5876 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005877 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005878 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5879 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005881
Bram Moolenaara5525202006-03-02 22:52:09 +00005882 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005883
Bram Moolenaar477933c2007-07-17 14:32:23 +00005884 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005885 {
5886 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005888 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005889 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005890 // In silent Ex mode topline is zero, but that's not a valid line
5891 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005892 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005893 return &pos;
5894 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005896 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005899 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005900 return &pos;
5901 }
5902 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005903 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005905 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
5907 pos.lnum = curbuf->b_ml.ml_line_count;
5908 pos.col = 0;
5909 }
5910 else
5911 {
5912 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005913 if (charcol)
5914 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5915 else
5916 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
5918 return &pos;
5919 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005920 if (in_vim9script())
5921 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 return NULL;
5923}
5924
5925/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005926 * Convert list in "arg" into a position and optional file number.
5927 * When "fnump" is NULL there is no file number, only 3 items.
5928 * Note that the column is passed on as-is, the caller may want to decrement
5929 * it to use 1 for the first column.
5930 * Return FAIL when conversion is not possible, doesn't check the position for
5931 * validity.
5932 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934list2fpos(
5935 typval_T *arg,
5936 pos_T *posp,
5937 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005938 colnr_T *curswantp,
5939 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005940{
5941 list_T *l = arg->vval.v_list;
5942 long i = 0;
5943 long n;
5944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5946 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005947 if (arg->v_type != VAR_LIST
5948 || l == NULL
5949 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005950 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005951 return FAIL;
5952
5953 if (fnump != NULL)
5954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005955 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005956 if (n < 0)
5957 return FAIL;
5958 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005960 *fnump = n;
5961 }
5962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005964 if (n < 0)
5965 return FAIL;
5966 posp->lnum = n;
5967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005968 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005969 if (n < 0)
5970 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005971 // If character position is specified, then convert to byte position
5972 if (charcol)
5973 {
5974 buf_T *buf;
5975
5976 // Get the text for the specified line in a loaded buffer
5977 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5978 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5979 return FAIL;
5980
Bram Moolenaar91458462021-01-13 20:08:38 +01005981 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005982 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005983 posp->col = n;
5984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005985 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005986 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005987 posp->coladd = 0;
5988 else
5989 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005990
Bram Moolenaar493c1782014-05-28 14:34:46 +02005991 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005992 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005993
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005994 return OK;
5995}
5996
5997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 * Get the length of an environment variable name.
5999 * Advance "arg" to the first character after the name.
6000 * Return 0 for error.
6001 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 char_u *p;
6006 int len;
6007
6008 for (p = *arg; vim_isIDc(*p); ++p)
6009 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006010 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return 0;
6012
6013 len = (int)(p - *arg);
6014 *arg = p;
6015 return len;
6016}
6017
6018/*
6019 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006020 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 * Return 0 if something is wrong.
6022 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 char_u *p;
6027 int len;
6028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006029 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006031 {
6032 if (*p == ':')
6033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006034 // "s:" is start of "s:var", but "n:" is not and can be used in
6035 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006036 len = (int)(p - *arg);
6037 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6038 || len > 1)
6039 break;
6040 }
6041 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006042 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 return 0;
6044
6045 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006046 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 return len;
6049}
6050
6051/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006052 * Get the length of the name of a variable or function.
6053 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006055 * Return -1 if curly braces expansion failed.
6056 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 * If the name contains 'magic' {}'s, expand them and return the
6058 * expanded name in an allocated string via 'alias' - caller must free.
6059 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061get_name_len(
6062 char_u **arg,
6063 char_u **alias,
6064 int evaluate,
6065 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 char_u *p;
6069 char_u *expr_start;
6070 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6075 && (*arg)[2] == (int)KE_SNR)
6076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006077 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 *arg += 3;
6079 return get_id_len(arg) + 3;
6080 }
6081 len = eval_fname_script(*arg);
6082 if (len > 0)
6083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006084 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 *arg += len;
6086 }
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006089 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006091 p = find_name_end(*arg, &expr_start, &expr_end,
6092 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (expr_start != NULL)
6094 {
6095 char_u *temp_string;
6096
6097 if (!evaluate)
6098 {
6099 len += (int)(p - *arg);
6100 *arg = skipwhite(p);
6101 return len;
6102 }
6103
6104 /*
6105 * Include any <SID> etc in the expanded string:
6106 * Thus the -len here.
6107 */
6108 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6109 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006110 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 *alias = temp_string;
6112 *arg = skipwhite(p);
6113 return (int)STRLEN(temp_string);
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006117 // Only give an error when there is something, otherwise it will be
6118 // reported at a higher level.
6119 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006120 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 return len;
6123}
6124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006125/*
6126 * Find the end of a variable or function name, taking care of magic braces.
6127 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6128 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006129 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006130 * Return a pointer to just after the name. Equal to "arg" if there is no
6131 * valid name.
6132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006133 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134find_name_end(
6135 char_u *arg,
6136 char_u **expr_start,
6137 char_u **expr_end,
6138 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006140 int mb_nest = 0;
6141 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006143 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006144 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006148 *expr_start = NULL;
6149 *expr_end = NULL;
6150 }
6151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006153 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6154 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006155 return arg;
6156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 for (p = arg; *p != NUL
6158 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006159 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006160 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006161 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006162 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006163 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006164 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006165 if (*p == '\'')
6166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006167 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006168 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006169 ;
6170 if (*p == NUL)
6171 break;
6172 }
6173 else if (*p == '"')
6174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006176 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006177 if (*p == '\\' && p[1] != NUL)
6178 ++p;
6179 if (*p == NUL)
6180 break;
6181 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006182 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006184 // "s:" is start of "s:var", but "n:" is not and can be used in
6185 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006186 len = (int)(p - arg);
6187 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006188 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006189 break;
6190 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 if (*p == '[')
6195 ++br_nest;
6196 else if (*p == ']')
6197 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006199
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006200 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006202 if (*p == '{')
6203 {
6204 mb_nest++;
6205 if (expr_start != NULL && *expr_start == NULL)
6206 *expr_start = p;
6207 }
6208 else if (*p == '}')
6209 {
6210 mb_nest--;
6211 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6212 *expr_end = p;
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 }
6216
6217 return p;
6218}
6219
6220/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006221 * Expands out the 'magic' {}'s in a variable/function name.
6222 * Note that this can call itself recursively, to deal with
6223 * constructs like foo{bar}{baz}{bam}
6224 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6225 * "in_start" ^
6226 * "expr_start" ^
6227 * "expr_end" ^
6228 * "in_end" ^
6229 *
6230 * Returns a new allocated string, which the caller must free.
6231 * Returns NULL for failure.
6232 */
6233 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234make_expanded_name(
6235 char_u *in_start,
6236 char_u *expr_start,
6237 char_u *expr_end,
6238 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006239{
6240 char_u c1;
6241 char_u *retval = NULL;
6242 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006243
6244 if (expr_end == NULL || in_end == NULL)
6245 return NULL;
6246 *expr_start = NUL;
6247 *expr_end = NUL;
6248 c1 = *in_end;
6249 *in_end = NUL;
6250
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006251 temp_result = eval_to_string(expr_start + 1, FALSE);
6252 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006253 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006254 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6255 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006256 if (retval != NULL)
6257 {
6258 STRCPY(retval, in_start);
6259 STRCAT(retval, temp_result);
6260 STRCAT(retval, expr_end + 1);
6261 }
6262 }
6263 vim_free(temp_result);
6264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006266 *expr_start = '{';
6267 *expr_end = '}';
6268
6269 if (retval != NULL)
6270 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006271 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006272 if (expr_start != NULL)
6273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006274 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006275 temp_result = make_expanded_name(retval, expr_start,
6276 expr_end, temp_result);
6277 vim_free(retval);
6278 retval = temp_result;
6279 }
6280 }
6281
6282 return retval;
6283}
6284
6285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006292 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006293}
6294
6295/*
6296 * Return TRUE if character "c" can be used as the first character in a
6297 * variable or function name (excluding '{' and '}').
6298 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006299 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006301{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006302 return ASCII_ISALPHA(c) || c == '_';
6303}
6304
6305/*
6306 * Return TRUE if character "c" can be used as the first character of a
6307 * dictionary key.
6308 */
6309 int
6310eval_isdictc(int c)
6311{
6312 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313}
6314
6315/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006316 * Handle:
6317 * - expr[expr], expr[expr:expr] subscript
6318 * - ".name" lookup
6319 * - function call with Funcref variable: func(expr)
6320 * - method call: var->method()
6321 *
6322 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006323 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006324 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006326handle_subscript(
6327 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006328 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006329 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006330 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006331 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006332{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006333 int evaluate = evalarg != NULL
6334 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006335 int ret = OK;
6336 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006337 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006338 int getnext;
6339 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006340
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006341 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006342 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006343 // When at the end of the line and ".name" or "->{" or "->X" follows in
6344 // the next line then consume the line break.
6345 p = eval_next_non_blank(*arg, evalarg, &getnext);
6346 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006347 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006348 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6349 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6350 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006351 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006352 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006353 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006354 check_white = FALSE;
6355 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006356
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006357 if (rettv->v_type == VAR_ANY)
6358 {
6359 char_u *exp_name;
6360 int cc;
6361 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006362 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006363 type_T *type;
6364
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006365 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006366 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006367 if (**arg != '.')
6368 {
6369 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006370 semsg(_(e_expected_dot_after_name_str),
6371 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006372 ret = FAIL;
6373 break;
6374 }
6375 ++*arg;
6376 if (IS_WHITE_OR_NUL(**arg))
6377 {
6378 if (verbose)
6379 emsg(_(e_no_white_space_allowed_after_dot));
6380 ret = FAIL;
6381 break;
6382 }
6383
6384 // isolate the name
6385 exp_name = *arg;
6386 while (eval_isnamec(**arg))
6387 ++*arg;
6388 cc = **arg;
6389 **arg = NUL;
6390
6391 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006392 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006393 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006394
6395 if (idx < 0 && ufunc == NULL)
6396 {
6397 ret = FAIL;
6398 break;
6399 }
6400 if (idx >= 0)
6401 {
6402 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6403 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6404
6405 copy_tv(sv->sv_tv, rettv);
6406 }
6407 else
6408 {
6409 rettv->v_type = VAR_FUNC;
6410 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6411 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006412 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006413 }
6414
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006415 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6416 || rettv->v_type == VAR_PARTIAL))
6417 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006418 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006419 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6420 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006421
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006422 // Stop the expression evaluation when immediately aborting on
6423 // error, or when an interrupt occurred or an exception was thrown
6424 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006425 if (aborting())
6426 {
6427 if (ret == OK)
6428 clear_tv(rettv);
6429 ret = FAIL;
6430 }
6431 dict_unref(selfdict);
6432 selfdict = NULL;
6433 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006434 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006435 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006436 if (in_vim9script())
6437 *arg = skipwhite(p + 2);
6438 else
6439 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006440 if (ret == OK)
6441 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006442 if (VIM_ISWHITE(**arg))
6443 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006444 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006445 ret = FAIL;
6446 }
6447 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006448 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006449 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006450 else
6451 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006452 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006453 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006454 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006455 // "." is ".name" lookup when we found a dict or when evaluating and
6456 // scriptversion is at least 2, where string concatenation is "..".
6457 else if (**arg == '['
6458 || (**arg == '.' && (rettv->v_type == VAR_DICT
6459 || (!evaluate
6460 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006461 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006462 {
6463 dict_unref(selfdict);
6464 if (rettv->v_type == VAR_DICT)
6465 {
6466 selfdict = rettv->vval.v_dict;
6467 if (selfdict != NULL)
6468 ++selfdict->dv_refcount;
6469 }
6470 else
6471 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006472 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006473 {
6474 clear_tv(rettv);
6475 ret = FAIL;
6476 }
6477 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006478 else
6479 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006480 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6483 // Don't do this when "Func" is already a partial that was bound
6484 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006485 if (selfdict != NULL
6486 && (rettv->v_type == VAR_FUNC
6487 || (rettv->v_type == VAR_PARTIAL
6488 && (rettv->vval.v_partial->pt_auto
6489 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006490 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006491
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006492 dict_unref(selfdict);
6493 return ret;
6494}
6495
6496/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006497 * Make a copy of an item.
6498 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006499 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6501 * reference to an already copied list/dict can be used.
6502 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505item_copy(
6506 typval_T *from,
6507 typval_T *to,
6508 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006509 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511{
6512 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006517 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 }
6520 ++recurse;
6521
6522 switch (from->v_type)
6523 {
6524 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006525 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 case VAR_STRING:
6527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006528 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006529 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006530 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006531 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006532 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006533 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 copy_tv(from, to);
6535 break;
6536 case VAR_LIST:
6537 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006538 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 if (from->vval.v_list == NULL)
6540 to->vval.v_list = NULL;
6541 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 to->vval.v_list = from->vval.v_list->lv_copylist;
6545 ++to->vval.v_list->lv_refcount;
6546 }
6547 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006548 to->vval.v_list = list_copy(from->vval.v_list,
6549 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006550 if (to->vval.v_list == NULL)
6551 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006553 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006555 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 case VAR_DICT:
6557 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006558 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006559 if (from->vval.v_dict == NULL)
6560 to->vval.v_dict = NULL;
6561 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6562 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6565 ++to->vval.v_dict->dv_refcount;
6566 }
6567 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006568 to->vval.v_dict = dict_copy(from->vval.v_dict,
6569 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006570 if (to->vval.v_dict == NULL)
6571 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006573 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006574 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006576 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 }
6579 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581}
6582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583 void
6584echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6585{
6586 char_u *tofree;
6587 char_u numbuf[NUMBUFLEN];
6588 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6589
6590 if (*atstart)
6591 {
6592 *atstart = FALSE;
6593 // Call msg_start() after eval1(), evaluating the expression
6594 // may cause a message to appear.
6595 if (with_space)
6596 {
6597 // Mark the saved text as finishing the line, so that what
6598 // follows is displayed on a new line when scrolling back
6599 // at the more prompt.
6600 msg_sb_eol();
6601 msg_start();
6602 }
6603 }
6604 else if (with_space)
6605 msg_puts_attr(" ", echo_attr);
6606
6607 if (p != NULL)
6608 for ( ; *p != NUL && !got_int; ++p)
6609 {
6610 if (*p == '\n' || *p == '\r' || *p == TAB)
6611 {
6612 if (*p != TAB && *needclr)
6613 {
6614 // remove any text still there from the command
6615 msg_clr_eos();
6616 *needclr = FALSE;
6617 }
6618 msg_putchar_attr(*p, echo_attr);
6619 }
6620 else
6621 {
6622 if (has_mbyte)
6623 {
6624 int i = (*mb_ptr2len)(p);
6625
6626 (void)msg_outtrans_len_attr(p, i, echo_attr);
6627 p += i - 1;
6628 }
6629 else
6630 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6631 }
6632 }
6633 vim_free(tofree);
6634}
6635
Bram Moolenaare9a41262005-01-15 22:18:47 +00006636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 * ":echo expr1 ..." print each argument separated with a space, add a
6638 * newline at the end.
6639 * ":echon expr1 ..." print each argument plain.
6640 */
6641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006646 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int needclr = TRUE;
6648 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006649 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006650 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006651 evalarg_T evalarg;
6652
Bram Moolenaare707c882020-07-01 13:07:37 +02006653 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 if (eap->skip)
6656 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006657 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006659 // If eval1() causes an error message the text from the command may
6660 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006661 need_clr_eos = needclr;
6662
Bram Moolenaar68db9962021-05-09 23:19:22 +02006663 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006664 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 /*
6667 * Report the invalid expression unless the expression evaluation
6668 * has been cancelled due to an aborting error, an interrupt, or an
6669 * exception.
6670 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006671 if (!aborting() && did_emsg == did_emsg_before
6672 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006673 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006674 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 break;
6676 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006677 need_clr_eos = FALSE;
6678
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006680 {
6681 if (rettv.v_type == VAR_VOID)
6682 {
6683 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6684 break;
6685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006686 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006687 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 arg = skipwhite(arg);
6691 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006692 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006693 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695 if (eap->skip)
6696 --emsg_skip;
6697 else
6698 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (needclr)
6701 msg_clr_eos();
6702 if (eap->cmdidx == CMD_echo)
6703 msg_end();
6704 }
6705}
6706
6707/*
6708 * ":echohl {name}".
6709 */
6710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006711ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006713 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714}
6715
6716/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006717 * Returns the :echo attribute
6718 */
6719 int
6720get_echo_attr(void)
6721{
6722 return echo_attr;
6723}
6724
6725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 * ":execute expr1 ..." execute the result of an expression.
6727 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006728 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006730 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 * Each gets spaces around each argument and a newline at the end for
6732 * echo commands
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 int ret = OK;
6740 char_u *p;
6741 garray_T ga;
6742 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006743 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745 ga_init2(&ga, 1, 80);
6746
6747 if (eap->skip)
6748 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006749 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006751 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006752 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 if (!eap->skip)
6756 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006757 char_u buf[NUMBUFLEN];
6758
6759 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006760 {
6761 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6762 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006763 semsg(_(e_using_invalid_value_as_string_str),
6764 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006765 p = NULL;
6766 }
6767 else
6768 p = tv_get_string_buf(&rettv, buf);
6769 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006770 else
6771 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006772 if (p == NULL)
6773 {
6774 clear_tv(&rettv);
6775 ret = FAIL;
6776 break;
6777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 len = (int)STRLEN(p);
6779 if (ga_grow(&ga, len + 2) == FAIL)
6780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006781 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 ret = FAIL;
6783 break;
6784 }
6785 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 ga.ga_len += len;
6789 }
6790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 arg = skipwhite(arg);
6793 }
6794
6795 if (ret != FAIL && ga.ga_data != NULL)
6796 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006797 // use the first line of continuation lines for messages
6798 SOURCING_LNUM = start_lnum;
6799
Bram Moolenaar37fef162022-08-29 18:16:32 +01006800 if (eap->cmdidx == CMD_echomsg
6801 || eap->cmdidx == CMD_echowindow
6802 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006803 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006804 // Mark the already saved text as finishing the line, so that what
6805 // follows is displayed on a new line when scrolling back at the
6806 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006807 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006808 }
6809
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006810 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006811 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006812 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006813 out_flush();
6814 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006815 else if (eap->cmdidx == CMD_echowindow)
6816 {
6817#ifdef HAS_MESSAGE_WINDOW
6818 start_echowindow();
6819#endif
6820 msg_attr(ga.ga_data, echo_attr);
6821#ifdef HAS_MESSAGE_WINDOW
6822 end_echowindow();
6823#endif
6824 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006825 else if (eap->cmdidx == CMD_echoconsole)
6826 {
6827 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6828 ui_write((char_u *)"\r\n", 2, TRUE);
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 else if (eap->cmdidx == CMD_echoerr)
6831 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006832 int save_did_emsg = did_emsg;
6833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006834 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006835 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (!force_abort)
6837 did_emsg = save_did_emsg;
6838 }
6839 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006840 {
6841 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6842
6843 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6844 sticky_cmdmod_flags = cmdmod.cmod_flags
6845 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 do_cmdline((char_u *)ga.ga_data,
6847 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006848 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851
6852 ga_clear(&ga);
6853
6854 if (eap->skip)
6855 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006856 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857}
6858
6859/*
6860 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6861 * "arg" points to the "&" or '+' when called, to "option" when returning.
6862 * Returns NULL when no option name found. Otherwise pointer to the char
6863 * after the option name.
6864 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006865 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006866find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
6868 char_u *p = *arg;
6869
6870 ++p;
6871 if (*p == 'g' && p[1] == ':')
6872 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006873 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 p += 2;
6875 }
6876 else if (*p == 'l' && p[1] == ':')
6877 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006878 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 p += 2;
6880 }
6881 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006882 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 if (!ASCII_ISALPHA(*p))
6885 return NULL;
6886 *arg = p;
6887
6888 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006889 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 else
6891 while (ASCII_ISALPHA(*p))
6892 ++p;
6893 return p;
6894}
6895
6896/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006897 * Display script name where an item was last set.
6898 * Should only be invoked when 'verbose' is non-zero.
6899 */
6900 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006901last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006902{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006903 char_u *p;
6904
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006905 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006906 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006907 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006908 if (p != NULL)
6909 {
6910 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006911 msg_puts(_("\n\tLast set from "));
6912 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006913 if (script_ctx.sc_lnum > 0)
6914 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006915 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006916 msg_outnum((long)script_ctx.sc_lnum);
6917 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006918 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006919 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006920 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006921 }
6922}
6923
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006924#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926/*
6927 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006928 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 * "flags" can be "g" to do a global substitute.
6930 * Returns an allocated string, NULL for error.
6931 */
6932 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006933do_string_sub(
6934 char_u *str,
6935 char_u *pat,
6936 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006937 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006938 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 int sublen;
6941 regmatch_T regmatch;
6942 int i;
6943 int do_all;
6944 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006945 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 garray_T ga;
6947 char_u *ret;
6948 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006949 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006951 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006953 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 ga_init2(&ga, 1, 200);
6956
6957 do_all = (flags[0] == 'g');
6958
6959 regmatch.rm_ic = p_ic;
6960 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6961 if (regmatch.regprog != NULL)
6962 {
6963 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006964 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006967 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006968 if (regmatch.startp[0] == regmatch.endp[0])
6969 {
6970 if (zero_width == regmatch.startp[0])
6971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006973 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006974 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6975 (size_t)i);
6976 ga.ga_len += i;
6977 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006978 continue;
6979 }
6980 zero_width = regmatch.startp[0];
6981 }
6982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 /*
6984 * Get some space for a temporary buffer to do the substitution
6985 * into. It will contain:
6986 * - The text up to where the match is.
6987 * - The substituted text.
6988 * - The text after the match.
6989 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006990 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006991 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6993 {
6994 ga_clear(&ga);
6995 break;
6996 }
6997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006998 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 i = (int)(regmatch.startp[0] - tail);
7000 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007001 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007002 (void)vim_regsub(&regmatch, sub, expr,
7003 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7004 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007006 tail = regmatch.endp[0];
7007 if (*tail == NUL)
7008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (!do_all)
7010 break;
7011 }
7012
7013 if (ga.ga_data != NULL)
7014 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7015
Bram Moolenaar473de612013-06-08 18:19:48 +02007016 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018
7019 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7020 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007021 if (p_cpo == empty_option)
7022 p_cpo = save_cpo;
7023 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007025 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007026 // If it's still empty it was changed and restored, need to restore in
7027 // the complicated way.
7028 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007029 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007030 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 return ret;
7034}