blob: c4d3781a2d3e5f4eda14acb86ffdf67da3703f9d [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 Moolenaarc9c967d2022-09-07 16:48:46 +0100266 // Shortcut to call a compiled function without overhead.
Bram Moolenaar58779852022-09-06 18:31:14 +0100267 // FIXME: should create a funccal and link it in current_funccal.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200268 if (call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +0100269 DEF_USE_PT_ARGV, partial, NULL, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 return FAIL;
271 }
272 else
273 {
274 s = partial_name(partial);
275 if (s == NULL || *s == NUL)
276 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200277 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000278 funcexe.fe_evaluate = TRUE;
279 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
281 return FAIL;
282 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200284 else if (expr->v_type == VAR_INSTR)
285 {
286 return exe_typval_instr(expr, rettv);
287 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 else
289 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000290 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100291 if (s == NULL)
292 return FAIL;
293 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200294 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200296 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200298 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200299 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 return FAIL;
301 }
302 }
303 return OK;
304}
305
306/*
307 * Like eval_to_bool() but using a typval_T instead of a string.
308 * Works for string, funcref and partial.
309 */
310 int
311eval_expr_to_bool(typval_T *expr, int *error)
312{
313 typval_T rettv;
314 int res;
315
316 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
317 {
318 *error = TRUE;
319 return FALSE;
320 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200321 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100322 clear_tv(&rettv);
323 return res;
324}
325
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326/*
327 * Top level evaluation function, returning a string. If "skip" is TRUE,
328 * only parsing to "nextcmd" is done, without reporting errors. Return
329 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
330 */
331 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100332eval_to_string_skip(
333 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200334 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100335 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
Bram Moolenaar33570922005-01-25 22:26:29 +0000337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200339 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
Bram Moolenaar37c83712020-06-30 21:18:36 +0200341 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 if (skip)
343 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 retval = NULL;
346 else
347 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100348 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000349 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351 if (skip)
352 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200353 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
355 return retval;
356}
357
358/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100359 * Initialize "evalarg" for use.
360 */
361 void
362init_evalarg(evalarg_T *evalarg)
363{
364 CLEAR_POINTER(evalarg);
365 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
366}
367
368/*
369 * If "evalarg->eval_tofree" is not NULL free it later.
370 * Caller is expected to overwrite "evalarg->eval_tofree" next.
371 */
372 static void
373free_eval_tofree_later(evalarg_T *evalarg)
374{
375 if (evalarg->eval_tofree != NULL)
376 {
377 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
378 ((char_u **)evalarg->eval_tofree_ga.ga_data)
379 [evalarg->eval_tofree_ga.ga_len++]
380 = evalarg->eval_tofree;
381 else
382 vim_free(evalarg->eval_tofree);
383 }
384}
385
386/*
387 * After using "evalarg" filled from "eap": free the memory.
388 */
389 void
390clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
391{
392 if (evalarg != NULL)
393 {
394 if (evalarg->eval_tofree != NULL)
395 {
396 if (eap != NULL)
397 {
398 // We may need to keep the original command line, e.g. for
399 // ":let" it has the variable names. But we may also need the
400 // new one, "nextcmd" points into it. Keep both.
401 vim_free(eap->cmdline_tofree);
402 eap->cmdline_tofree = *eap->cmdlinep;
403 *eap->cmdlinep = evalarg->eval_tofree;
404 }
405 else
406 vim_free(evalarg->eval_tofree);
407 evalarg->eval_tofree = NULL;
408 }
409
410 ga_clear_strings(&evalarg->eval_tofree_ga);
411 VIM_CLEAR(evalarg->eval_tofree_lambda);
412 }
413}
414
415/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000416 * Skip over an expression at "*pp".
417 * Return FAIL for an error, OK otherwise.
418 */
419 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200420skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000421{
Bram Moolenaar33570922005-01-25 22:26:29 +0000422 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000423
424 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200425 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000426}
427
428/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200429 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200430 * If in Vim9 script and line breaks are encountered, the lines are
431 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 * "arg" is advanced to just after the expression.
433 * "start" is set to the start of the expression, "end" to just after the end.
434 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435 * Return FAIL for an error, OK otherwise.
436 */
437 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200438skip_expr_concatenate(
439 char_u **arg,
440 char_u **start,
441 char_u **end,
442 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200443{
444 typval_T rettv;
445 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200446 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200447 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200448 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200449 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200450 int evaluate = evalarg == NULL
451 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200452
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200453 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200454 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200455 {
456 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200458 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200459 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200460 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200462 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200463
464 // Don't evaluate the expression.
465 if (evalarg != NULL)
466 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200467 *arg = skipwhite(*arg);
468 res = eval1(arg, &rettv, evalarg);
469 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200470 if (evalarg != NULL)
471 evalarg->eval_flags = save_flags;
472
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200473 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200475 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200476 if (evalarg->eval_ga.ga_len == 1)
477 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200478 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200479 ga_clear(gap);
480 gap->ga_itemsize = 0;
481 }
482 else
483 {
484 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200485 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200486
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200487 // Line breaks encountered, concatenate all the lines.
488 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200489 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200490
491 // free the lines only when using getsourceline()
492 if (evalarg->eval_cookie != NULL)
493 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200494 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200495 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200496 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100497 // later. Also free "eval_tofree" later if needed.
498 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200499 evalarg->eval_tofree =
500 ((char_u **)gap->ga_data)[gap->ga_len - 1];
501 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200502 ga_clear_strings(gap);
503 }
504 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200507
508 // free lines that were explicitly marked for freeing
509 ga_clear_strings(freegap);
510 }
511
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200512 gap->ga_itemsize = 0;
513 if (p == NULL)
514 return FAIL;
515 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 vim_free(evalarg->eval_tofree_lambda);
517 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200518 // Compute "end" relative to the end.
519 *end = *start + STRLEN(*start) - endoff;
520 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200521 }
522
523 return res;
524}
525
526/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100527 * Convert "tv" to a string.
528 * When "convert" is TRUE convert a List into a sequence of lines and convert
529 * a Float to a String.
530 * Returns an allocated string (NULL when out of memory).
531 */
532 char_u *
533typval2string(typval_T *tv, int convert)
534{
535 garray_T ga;
536 char_u *retval;
537#ifdef FEAT_FLOAT
538 char_u numbuf[NUMBUFLEN];
539#endif
540
541 if (convert && tv->v_type == VAR_LIST)
542 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000543 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100544 if (tv->vval.v_list != NULL)
545 {
546 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
547 if (tv->vval.v_list->lv_len > 0)
548 ga_append(&ga, NL);
549 }
550 ga_append(&ga, NUL);
551 retval = (char_u *)ga.ga_data;
552 }
553#ifdef FEAT_FLOAT
554 else if (convert && tv->v_type == VAR_FLOAT)
555 {
556 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
557 retval = vim_strsave(numbuf);
558 }
559#endif
560 else
561 retval = vim_strsave(tv_get_string(tv));
562 return retval;
563}
564
565/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200566 * Top level evaluation function, returning a string. Does not handle line
567 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000568 * When "convert" is TRUE convert a List into a sequence of lines and convert
569 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 * Return pointer to allocated memory, or NULL for failure.
571 */
572 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100574 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100575 int convert,
576 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
Bram Moolenaar33570922005-01-25 22:26:29 +0000578 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100582 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
583 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 retval = NULL;
585 else
586 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100587 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000588 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100590 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 return retval;
593}
594
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100595 char_u *
596eval_to_string(
597 char_u *arg,
598 int convert)
599{
600 return eval_to_string_eap(arg, convert, NULL);
601}
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000604 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100605 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200606 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 */
608 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100609eval_to_string_safe(
610 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000611 int use_sandbox,
612 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
614 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200615 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200616 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200617 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
Bram Moolenaar9530b582022-01-22 13:39:08 +0000619 if (!keep_script_version)
620 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200621 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000622 if (use_sandbox)
623 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100624 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200625 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200626 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000627 if (use_sandbox)
628 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100629 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200630 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200631 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200632 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return retval;
634}
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
637 * Top level evaluation function, returning a number.
638 * Evaluates "expr" silently.
639 * Returns -1 for an error.
640 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200641 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100642eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
Bram Moolenaar33570922005-01-25 22:26:29 +0000644 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200645 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000646 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 ++emsg_off;
649
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200650 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 retval = -1;
652 else
653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100654 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000655 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 --emsg_off;
658
659 return retval;
660}
661
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000662/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000663 * Top level evaluation function.
664 * Returns an allocated typval_T with the result.
665 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000666 */
667 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200668eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000669{
670 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200671 evalarg_T evalarg;
672
673 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000674
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200675 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200676 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100677 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000678
Bram Moolenaar37c83712020-06-30 21:18:36 +0200679 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000680 return tv;
681}
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000684 * "*arg" points to what can be a function name in the form of "import.Name" or
685 * "Funcref". Return the name of the function. Set "tofree" to something that
686 * was allocated.
687 * If "verbose" is FALSE no errors are given.
688 * Return NULL for any failure.
689 */
690 static char_u *
691deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000692 char_u **arg,
693 char_u **tofree,
694 evalarg_T *evalarg,
695 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000696{
697 typval_T ref;
698 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100699 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000700
701 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100702 if (evalarg != NULL)
703 {
704 // need to evaluate this to get an import, like in "a.Func"
705 save_flags = evalarg->eval_flags;
706 evalarg->eval_flags |= EVAL_EVALUATE;
707 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100708 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100709 {
710 dictitem_T *v;
711
712 // If <SID>VarName was used it would not be found, try another way.
713 v = find_var_also_in_script(name, NULL, FALSE);
714 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100715 {
716 name = NULL;
717 goto theend;
718 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100719 copy_tv(&v->di_tv, &ref);
720 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000721 if (*skipwhite(*arg) != NUL)
722 {
723 if (verbose)
724 semsg(_(e_trailing_characters_str), *arg);
725 name = NULL;
726 }
727 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
728 {
729 name = ref.vval.v_string;
730 ref.vval.v_string = NULL;
731 *tofree = name;
732 }
733 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
734 {
735 if (ref.vval.v_partial->pt_argc > 0
736 || ref.vval.v_partial->pt_dict != NULL)
737 {
738 if (verbose)
739 emsg(_(e_cannot_use_partial_here));
740 name = NULL;
741 }
742 else
743 {
744 name = vim_strsave(partial_name(ref.vval.v_partial));
745 *tofree = name;
746 }
747 }
748 else
749 {
750 if (verbose)
751 semsg(_(e_not_callable_type_str), name);
752 name = NULL;
753 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100754
755theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000756 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100757 if (evalarg != NULL)
758 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000759 return name;
760}
761
762/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100763 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200764 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
765 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100769call_vim_function(
770 char_u *func,
771 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200772 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200773 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000775 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200776 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000777 char_u *arg;
778 char_u *name;
779 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000780 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200783 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000784 funcexe.fe_firstline = curwin->w_cursor.lnum;
785 funcexe.fe_lastline = curwin->w_cursor.lnum;
786 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000788 // The name might be "import.Func" or "Funcref". We don't know, we need to
789 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000790 // autoload script has errors. Guess that when there is a dot in the name
791 // showing errors is the right choice.
792 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000793 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000794 if (ignore_errors)
795 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000796 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000797 if (ignore_errors)
798 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000799 if (name == NULL)
800 name = func;
801
802 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
803
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000804 if (ret == FAIL)
805 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000806 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000807
808 return ret;
809}
810
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100811/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100812 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000813 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
814 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000815 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000816 */
817 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100818call_func_retstr(
819 char_u *func,
820 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200821 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000822{
823 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000824 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000825
Bram Moolenaarded27a12018-08-01 19:06:03 +0200826 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000827 return NULL;
828
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100829 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 return retval;
832}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000833
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000834/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100835 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000836 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000837 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000838 */
839 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100840call_func_retlist(
841 char_u *func,
842 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200843 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000844{
845 typval_T rettv;
846
Bram Moolenaarded27a12018-08-01 19:06:03 +0200847 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000848 return NULL;
849
850 if (rettv.v_type != VAR_LIST)
851 {
852 clear_tv(&rettv);
853 return NULL;
854 }
855
856 return rettv.vval.v_list;
857}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaare70dd112022-01-21 16:31:11 +0000859#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100861 * Evaluate "arg", which is 'foldexpr'.
862 * Note: caller must set "curwin" to match "arg".
863 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
864 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 */
866 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000867eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000869 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200871 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000873 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000874 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
875 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaare70dd112022-01-21 16:31:11 +0000877 arg = wp->w_p_fde;
878 current_sctx = wp->w_p_script_ctx[WV_FDE];
879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000881 if (use_sandbox)
882 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100883 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200885 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 retval = 0;
887 else
888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100889 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (tv.v_type == VAR_NUMBER)
891 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000892 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 retval = 0;
894 else
895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100896 // If the result is a string, check if there is a non-digit before
897 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (!VIM_ISDIGIT(*s) && *s != '-')
900 *cp = *s++;
901 retval = atol((char *)s);
902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000903 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000906 if (use_sandbox)
907 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100908 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200909 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000910 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200912 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914#endif
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 * Get an lval: variable, Dict item or List item that can be assigned a value
918 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
919 * "name.key", "name.key[expr]" etc.
920 * Indexing only works if "name" is an existing List or Dictionary.
921 * "name" points to the start of the name.
922 * If "rettv" is not NULL it points to the value to be assigned.
923 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
924 * wrong; must end in space or cmd separator.
925 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100926 * flags:
927 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100928 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100929 * GLV_NO_AUTOLOAD: do not use script autoloading
930 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000932 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000934 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200935 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936get_lval(
937 char_u *name,
938 typval_T *rettv,
939 lval_T *lp,
940 int unlet,
941 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100942 int flags, // GLV_ values
943 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 char_u *expr_start, *expr_end;
947 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 dictitem_T *v;
949 typval_T var1;
950 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100954 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100955 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100956 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000957 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100959 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200960 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100962 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100964 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200966 lp->ll_name_end = find_name_end(name, NULL, NULL,
967 FNE_INCL_BR | fne_flags);
968 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 }
970
Bram Moolenaara749a422022-02-12 19:52:25 +0000971 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000972 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000973 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
974 {
975 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
976 return NULL;
977 }
978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000980 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (expr_start != NULL)
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100985 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 && *p != '[' && *p != '.')
987 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000988 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 return NULL;
990 }
991
992 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
993 if (lp->ll_exp_name == NULL)
994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // Report an invalid expression in braces, unless the
996 // expression evaluation has been cancelled due to an
997 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 if (!aborting() && !quiet)
999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001000 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001001 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 return NULL;
1003 }
1004 }
1005 lp->ll_name = lp->ll_exp_name;
1006 }
1007 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 lp->ll_name = name;
1010
Bram Moolenaar4525a572022-02-13 11:57:33 +00001011 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001013 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001014 // However, "g:[key]" is indexing a dictionary.
1015 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001016 {
1017 --p;
1018 lp->ll_name_end = p;
1019 }
1020 if (*p == ':')
1021 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001022 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001023
1024 if (tp == p + 1 && !quiet)
1025 {
1026 semsg(_(e_white_space_required_after_str_str), ":", p);
1027 return NULL;
1028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001030 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001031 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001032 semsg(_(e_using_type_not_in_script_context_str), p);
1033 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001034 }
1035
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001036 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001037 lp->ll_type = parse_type(&tp,
1038 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1039 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001040 if (lp->ll_type == NULL && !quiet)
1041 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001042 lp->ll_name_end = tp;
1043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 }
1045 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001046 if (lp->ll_name == NULL)
1047 return p;
1048
Bram Moolenaar62aec932022-01-29 21:45:34 +00001049 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001050 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001051 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001052
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001053 if (import != NULL)
1054 {
1055 ufunc_T *ufunc;
1056 type_T *type;
1057
Bram Moolenaar753885b2022-08-24 16:30:36 +01001058 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001059 lp->ll_sid = import->imp_sid;
1060 lp->ll_name = skipwhite(p + 1);
1061 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1062 lp->ll_name_end = p;
1063
1064 // check the item is exported
1065 cc = *p;
1066 *p = NUL;
1067 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001068 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001069 {
1070 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001071 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001072 }
1073 *p = cc;
1074 }
1075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001077 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001078 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 return p;
1080
Bram Moolenaar4525a572022-02-13 11:57:33 +00001081 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001082 {
1083 // using local variable
1084 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001085 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001086 }
1087 else
1088 {
1089 cc = *p;
1090 *p = NUL;
1091 // When we would write to the variable pass &ht and prevent autoload.
1092 writing = !(flags & GLV_READ_ONLY);
1093 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001094 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001095 if (v == NULL && !quiet)
1096 semsg(_(e_undefined_variable_str), lp->ll_name);
1097 *p = cc;
1098 if (v == NULL)
1099 return NULL;
1100 lp->ll_tv = &v->di_tv;
1101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102
Bram Moolenaar4525a572022-02-13 11:57:33 +00001103 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001104 {
1105 if (!quiet)
1106 semsg(_(e_variable_already_declared), lp->ll_name);
1107 return NULL;
1108 }
1109
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 /*
1111 * Loop until no more [idx] or .key is following.
1112 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001113 var1.v_type = VAR_UNKNOWN;
1114 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001115 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001117 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1118 {
1119 if (!quiet)
1120 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1121 return NULL;
1122 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001123 if (lp->ll_tv->v_type != VAR_LIST
1124 && lp->ll_tv->v_type != VAR_DICT
1125 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001128 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001131
1132 // a NULL list/blob works like an empty list/blob, allocate one now.
1133 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1134 rettv_list_alloc(lp->ll_tv);
1135 else if (lp->ll_tv->v_type == VAR_BLOB
1136 && lp->ll_tv->vval.v_blob == NULL)
1137 rettv_blob_alloc(lp->ll_tv);
1138
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001142 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001145
Bram Moolenaar4525a572022-02-13 11:57:33 +00001146 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001147 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001148 && lp->ll_tv == &v->di_tv
1149 && ht != NULL && ht == get_script_local_ht())
1150 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001151 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001152
1153 // Vim9 script local variable: get the type
1154 if (sv != NULL)
1155 lp->ll_valtype = sv->sv_type;
1156 }
1157
Bram Moolenaar8c711452005-01-14 21:53:12 +00001158 len = -1;
1159 if (*p == '.')
1160 {
1161 key = p + 1;
1162 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1163 ;
1164 if (len == 0)
1165 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001167 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 }
1170 p = key + len;
1171 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001172 else
1173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001174 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001175 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 if (*p == ':')
1177 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001178 else
1179 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001181 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001185 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001186 clear_tv(&var1);
1187 return NULL;
1188 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001189 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190 }
1191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001193 if (*p == ':')
1194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001198 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001199 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001201 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001202 if (rettv != NULL
1203 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001204 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001206 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001209 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001210 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001212 }
1213 p = skipwhite(p + 1);
1214 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 else
1217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001219 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001220 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001222 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001223 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001224 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001225 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001228 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001229 clear_tv(&var2);
1230 return NULL;
1231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001234 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001241 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
1243 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 }
1246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 ++p;
1249 }
1250
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 {
1253 if (len == -1)
1254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // "[key]": get key from "var1"
1256 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001257 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 }
1262 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001263 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001264
1265 // a NULL dict is equivalent with an empty dict
1266 if (lp->ll_tv->vval.v_dict == NULL)
1267 {
1268 lp->ll_tv->vval.v_dict = dict_alloc();
1269 if (lp->ll_tv->vval.v_dict == NULL)
1270 {
1271 clear_tv(&var1);
1272 return NULL;
1273 }
1274 ++lp->ll_tv->vval.v_dict->dv_refcount;
1275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001277
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001278 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001280 // When assigning to a scope dictionary check that a function and
1281 // variable name is valid (only variable name unless it is l: or
1282 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001283 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001284 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001285 int prevval;
1286 int wrong;
1287
1288 if (len != -1)
1289 {
1290 prevval = key[len];
1291 key[len] = NUL;
1292 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001293 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001294 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001295 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1296 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001297 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001298 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001299 if (len != -1)
1300 key[len] = prevval;
1301 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001302 {
1303 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001304 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001305 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001306 }
1307
Bram Moolenaar10c65862020-10-08 21:16:42 +02001308 if (lp->ll_valtype != NULL)
1309 // use the type of the member
1310 lp->ll_valtype = lp->ll_valtype->tt_member;
1311
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001314 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001315 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001316 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001317 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001318 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001319 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001320 return NULL;
1321 }
1322
Bram Moolenaar31b81602019-02-10 22:14:27 +01001323 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001327 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001328 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001330 }
1331 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001333 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001335 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337 p = NULL;
1338 break;
1339 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001340 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001341 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001342 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1343 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001344 {
1345 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001346 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001347 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001348
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001349 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001352 else if (lp->ll_tv->v_type == VAR_BLOB)
1353 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001354 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1355
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001356 /*
1357 * Get the number and item for the only or first index of the List.
1358 */
1359 if (empty1)
1360 lp->ll_n1 = 0;
1361 else
1362 // is number or string
1363 lp->ll_n1 = (long)tv_get_number(&var1);
1364 clear_tv(&var1);
1365
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001366 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001368 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001369 return NULL;
1370 }
1371 if (lp->ll_range && !lp->ll_empty2)
1372 {
1373 lp->ll_n2 = (long)tv_get_number(&var2);
1374 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001375 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1376 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001377 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001378 }
1379 lp->ll_blob = lp->ll_tv->vval.v_blob;
1380 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001381 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001383 else
1384 {
1385 /*
1386 * Get the number and item for the only or first index of the List.
1387 */
1388 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001391 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001393 clear_tv(&var1);
1394
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001397 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1398 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001399 if (lp->ll_li == NULL)
1400 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001401 clear_tv(&var2);
1402 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 }
1404
Bram Moolenaar10c65862020-10-08 21:16:42 +02001405 if (lp->ll_valtype != NULL)
1406 // use the type of the member
1407 lp->ll_valtype = lp->ll_valtype->tt_member;
1408
Bram Moolenaar8c711452005-01-14 21:53:12 +00001409 /*
1410 * May need to find the item or absolute index for the second
1411 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 * When no index given: "lp->ll_empty2" is TRUE.
1413 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001417 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001418 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001420 if (check_range_index_two(lp->ll_list,
1421 &lp->ll_n1, lp->ll_li,
1422 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 }
1425
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 }
1429
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001430 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001432 return p;
1433}
1434
1435/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001439clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440{
1441 vim_free(lp->ll_exp_name);
1442 vim_free(lp->ll_newkey);
1443}
1444
1445/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001446 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001448 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1449 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452set_var_lval(
1453 lval_T *lp,
1454 char_u *endp,
1455 typval_T *rettv,
1456 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001457 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1458 char_u *op,
1459 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001460{
1461 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001462 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463
1464 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001465 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001466 cc = *endp;
1467 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001468 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1469 return;
1470
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001471 if (lp->ll_blob != NULL)
1472 {
1473 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 if (op != NULL && *op != '=')
1476 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001477 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 return;
1479 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001480 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001481 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482
1483 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1484 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001485 if (lp->ll_empty2)
1486 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1487
Bram Moolenaar68452172021-04-12 21:21:02 +02001488 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1489 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001490 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001491 }
1492 else
1493 {
1494 val = (int)tv_get_number_chk(rettv, &error);
1495 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001496 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001497 }
1498 }
1499 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001501 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001503 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1504 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001505 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001506 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001507 *endp = cc;
1508 return;
1509 }
1510
Bram Moolenaarff697e62019-02-12 22:28:33 +01001511 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001512 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001513 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001514 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001515 {
1516 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001517 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1518 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001519 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001520 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001521 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001522 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001524 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001525 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001526 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001527 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1528 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001529 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001530 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001531 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001532 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001533 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001534 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001535 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001536 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001537 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001538 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001539 else if (lp->ll_range)
1540 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001541 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1542 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001543 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001544 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001545 return;
1546 }
1547
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001548 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1549 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001550 }
1551 else
1552 {
1553 /*
1554 * Assign to a List or Dictionary item.
1555 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001556 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1557 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001558 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001559 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001560 return;
1561 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001562
1563 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001564 && check_typval_arg_type(lp->ll_valtype, rettv,
1565 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001566 return;
1567
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001568 if (lp->ll_newkey != NULL)
1569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 if (op != NULL && *op != '=')
1571 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001572 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 return;
1574 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001575 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1576 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001577 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001579 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001580 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001581 if (di == NULL)
1582 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001583 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1584 {
1585 vim_free(di);
1586 return;
1587 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001588 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 else if (op != NULL && *op != '=')
1591 {
1592 tv_op(lp->ll_tv, rettv, op);
1593 return;
1594 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001596 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 /*
1599 * Assign the value to the variable or list item.
1600 */
1601 if (copy)
1602 copy_tv(rettv, lp->ll_tv);
1603 else
1604 {
1605 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001606 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 }
1609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610}
1611
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1614 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 * Returns OK or FAIL.
1616 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001620 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 char_u numbuf[NUMBUFLEN];
1622 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001623 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001625 // Can't do anything with a Funcref or Dict on the right.
1626 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001627 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001628 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1629 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 {
1631 switch (tv1->v_type)
1632 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001633 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001634 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 case VAR_DICT:
1637 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001638 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001639 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001640 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001641 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001642 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001643 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 break;
1645
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001646 case VAR_BLOB:
1647 if (*op != '+' || tv2->v_type != VAR_BLOB)
1648 break;
1649 // BLOB += BLOB
1650 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1651 {
1652 blob_T *b1 = tv1->vval.v_blob;
1653 blob_T *b2 = tv2->vval.v_blob;
1654 int i, len = blob_len(b2);
1655 for (i = 0; i < len; i++)
1656 ga_append(&b1->bv_ga, blob_get(b2, i));
1657 }
1658 return OK;
1659
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 case VAR_LIST:
1661 if (*op != '+' || tv2->v_type != VAR_LIST)
1662 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001663 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001664 if (tv2->vval.v_list != NULL)
1665 {
1666 if (tv1->vval.v_list == NULL)
1667 {
1668 tv1->vval.v_list = tv2->vval.v_list;
1669 ++tv1->vval.v_list->lv_refcount;
1670 }
1671 else
1672 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1673 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 return OK;
1675
1676 case VAR_NUMBER:
1677 case VAR_STRING:
1678 if (tv2->v_type == VAR_LIST)
1679 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001680 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001681 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001682 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001683 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001684#ifdef FEAT_FLOAT
1685 if (tv2->v_type == VAR_FLOAT)
1686 {
1687 float_T f = n;
1688
Bram Moolenaarff697e62019-02-12 22:28:33 +01001689 if (*op == '%')
1690 break;
1691 switch (*op)
1692 {
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 case '/': f /= tv2->vval.v_float; break;
1697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001698 clear_tv(tv1);
1699 tv1->v_type = VAR_FLOAT;
1700 tv1->vval.v_float = f;
1701 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001702 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001703#endif
1704 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001705 switch (*op)
1706 {
1707 case '+': n += tv_get_number(tv2); break;
1708 case '-': n -= tv_get_number(tv2); break;
1709 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001710 case '/': n = num_divide(n, tv_get_number(tv2),
1711 &failed); break;
1712 case '%': n = num_modulus(n, tv_get_number(tv2),
1713 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001714 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001715 clear_tv(tv1);
1716 tv1->v_type = VAR_NUMBER;
1717 tv1->vval.v_number = n;
1718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 }
1720 else
1721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001722 if (tv2->v_type == VAR_FLOAT)
1723 break;
1724
Bram Moolenaarff697e62019-02-12 22:28:33 +01001725 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001726 s = tv_get_string(tv1);
1727 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 clear_tv(tv1);
1729 tv1->v_type = VAR_STRING;
1730 tv1->vval.v_string = s;
1731 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001732 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001734 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001735#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001736 {
1737 float_T f;
1738
Bram Moolenaarff697e62019-02-12 22:28:33 +01001739 if (*op == '%' || *op == '.'
1740 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001741 && tv2->v_type != VAR_NUMBER
1742 && tv2->v_type != VAR_STRING))
1743 break;
1744 if (tv2->v_type == VAR_FLOAT)
1745 f = tv2->vval.v_float;
1746 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001747 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001748 switch (*op)
1749 {
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 case '/': tv1->vval.v_float /= f; break;
1754 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001755 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001756#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001757 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 }
1759 }
1760
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001761 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 return FAIL;
1763}
1764
1765/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 * Evaluate the expression used in a ":for var in expr" command.
1767 * "arg" points to "var".
1768 * Set "*errp" to TRUE for an error, FALSE otherwise;
1769 * Return a pointer that holds the info. Null when there is an error.
1770 */
1771 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772eval_for_line(
1773 char_u *arg,
1774 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001775 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001776 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001779 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 typval_T tv;
1782 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001783 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001785 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001787 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 if (fi == NULL)
1789 return NULL;
1790
Bram Moolenaar404557e2021-07-05 21:41:48 +02001791 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1792 &fi->fi_semicolon, FALSE);
1793 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return fi;
1795
Bram Moolenaar404557e2021-07-05 21:41:48 +02001796 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001797 if (expr[0] != 'i' || expr[1] != 'n'
1798 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001800 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1801 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1802 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001803 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 return fi;
1805 }
1806
1807 if (skip)
1808 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001809 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1810 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 {
1812 *errp = FALSE;
1813 if (!skip)
1814 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001815 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001816 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001817 l = tv.vval.v_list;
1818 if (l == NULL)
1819 {
1820 // a null list is like an empty list: do nothing
1821 clear_tv(&tv);
1822 }
1823 else
1824 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001826 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001828 // No need to increment the refcount, it's already set for
1829 // the list being used in "tv".
1830 fi->fi_list = l;
1831 list_add_watch(l, &fi->fi_lw);
1832 fi->fi_lw.lw_item = l->lv_first;
1833 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001834 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001835 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001836 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001837 fi->fi_bi = 0;
1838 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001839 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001840 typval_T btv;
1841
1842 // Make a copy, so that the iteration still works when the
1843 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001845 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001846 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001847 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001848 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001849 else if (tv.v_type == VAR_STRING)
1850 {
1851 fi->fi_byte_idx = 0;
1852 fi->fi_string = tv.vval.v_string;
1853 tv.vval.v_string = NULL;
1854 if (fi->fi_string == NULL)
1855 fi->fi_string = vim_strsave((char_u *)"");
1856 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 else
1858 {
Sean Dewar80d73952021-08-04 19:25:54 +02001859 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001860 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 }
1862 }
1863 }
1864 if (skip)
1865 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001866 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867
1868 return fi;
1869}
1870
1871/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001872 * Used when looping over a :for line, skip the "in expr" part.
1873 */
1874 void
1875skip_for_lines(void *fi_void, evalarg_T *evalarg)
1876{
1877 forinfo_T *fi = (forinfo_T *)fi_void;
1878 int i;
1879
1880 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001881 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001882}
1883
1884/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 * Use the first item in a ":for" list. Advance to the next.
1886 * Assign the values to the variable (list). "arg" points to the first one.
1887 * Return TRUE when a valid item was found, FALSE when at end of list or
1888 * something wrong.
1889 */
1890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001893 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001895 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001896 ? (ASSIGN_FINAL
1897 // first round: error if variable exists
1898 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1899 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001900 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001902 int skip_assign = in_vim9script() && arg[0] == '_'
1903 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001905 if (fi->fi_blob != NULL)
1906 {
1907 typval_T tv;
1908
1909 if (fi->fi_bi >= blob_len(fi->fi_blob))
1910 return FALSE;
1911 tv.v_type = VAR_NUMBER;
1912 tv.v_lock = VAR_FIXED;
1913 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1914 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001915 if (skip_assign)
1916 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001917 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001918 fi->fi_varcount, flag, NULL) == OK;
1919 }
1920
1921 if (fi->fi_string != NULL)
1922 {
1923 typval_T tv;
1924 int len;
1925
1926 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1927 if (len == 0)
1928 return FALSE;
1929 tv.v_type = VAR_STRING;
1930 tv.v_lock = VAR_FIXED;
1931 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1932 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001933 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001934 if (skip_assign)
1935 result = TRUE;
1936 else
1937 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001938 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001939 vim_free(tv.vval.v_string);
1940 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001941 }
1942
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 item = fi->fi_lw.lw_item;
1944 if (item == NULL)
1945 result = FALSE;
1946 else
1947 {
1948 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001949 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001950 if (skip_assign)
1951 result = TRUE;
1952 else
1953 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001954 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 }
1956 return result;
1957}
1958
1959/*
1960 * Free the structure used to store info used by ":for".
1961 */
1962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964{
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001967 if (fi == NULL)
1968 return;
1969 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001970 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001972 list_unref(fi->fi_list);
1973 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001974 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001975 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001976 else
1977 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 vim_free(fi);
1979}
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001982set_context_for_expression(
1983 expand_T *xp,
1984 char_u *arg,
1985 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001987 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001991 if (cmdidx == CMD_let || cmdidx == CMD_var
1992 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 {
1994 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001995 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001997 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002001 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002002 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 break;
2004 }
2005 return;
2006 }
2007 }
2008 else
2009 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2010 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 while ((xp->xp_pattern = vim_strpbrk(arg,
2012 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2013 {
2014 c = *xp->xp_pattern;
2015 if (c == '&')
2016 {
2017 c = xp->xp_pattern[1];
2018 if (c == '&')
2019 {
2020 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002021 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002026 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2027 xp->xp_pattern += 2;
2028
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 else if (c == '$')
2032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 xp->xp_context = EXPAND_ENV_VARS;
2035 }
2036 else if (c == '=')
2037 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002038 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 xp->xp_context = EXPAND_EXPRESSION;
2040 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002041 else if (c == '#'
2042 && xp->xp_context == EXPAND_EXPRESSION)
2043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002044 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002045 break;
2046 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002047 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 && xp->xp_context == EXPAND_FUNCTIONS
2049 && vim_strchr(xp->xp_pattern, '(') == NULL)
2050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 break;
2053 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002054 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002056 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2059 if (c == '\\' && xp->xp_pattern[1] != NUL)
2060 ++xp->xp_pattern;
2061 xp->xp_context = EXPAND_NOTHING;
2062 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002065 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2067 /* skip */ ;
2068 xp->xp_context = EXPAND_NOTHING;
2069 }
2070 else if (c == '|')
2071 {
2072 if (xp->xp_pattern[1] == '|')
2073 {
2074 ++xp->xp_pattern;
2075 xp->xp_context = EXPAND_EXPRESSION;
2076 }
2077 else
2078 xp->xp_context = EXPAND_COMMANDS;
2079 }
2080 else
2081 xp->xp_context = EXPAND_EXPRESSION;
2082 }
2083 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002084 // Doesn't look like something valid, expand as an expression
2085 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 arg = xp->xp_pattern;
2088 if (*arg != NUL)
2089 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2090 /* skip */ ;
2091 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002092
2093 // ":exe one two" completes "two"
2094 if ((cmdidx == CMD_execute
2095 || cmdidx == CMD_echo
2096 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002097 || cmdidx == CMD_echomsg
2098 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002099 && xp->xp_context == EXPAND_EXPRESSION)
2100 {
2101 for (;;)
2102 {
2103 char_u *n = skiptowhite(arg);
2104
2105 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2106 break;
2107 arg = skipwhite(n);
2108 }
2109 }
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 xp->xp_pattern = arg;
2112}
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002115 * Return TRUE if "pat" matches "text".
2116 * Does not use 'cpo' and always uses 'magic'.
2117 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002118 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002119pattern_match(char_u *pat, char_u *text, int ic)
2120{
2121 int matches = FALSE;
2122 char_u *save_cpo;
2123 regmatch_T regmatch;
2124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002126 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002127 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002128 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2129 if (regmatch.regprog != NULL)
2130 {
2131 regmatch.rm_ic = ic;
2132 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2133 vim_regfree(regmatch.regprog);
2134 }
2135 p_cpo = save_cpo;
2136 return matches;
2137}
2138
2139/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002140 * Handle a name followed by "(". Both for just "name(arg)" and for
2141 * "expr->name(arg)".
2142 * Returns OK or FAIL.
2143 */
2144 static int
2145eval_func(
2146 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002147 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002148 char_u *name,
2149 int name_len,
2150 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002151 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002152 typval_T *basetv) // "expr" for "expr->name(arg)"
2153{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002154 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002155 char_u *s = name;
2156 int len = name_len;
2157 partial_T *partial;
2158 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002159 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002160 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002161
2162 if (!evaluate)
2163 check_vars(s, len);
2164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002165 // If "s" is the name of a variable of type VAR_FUNC
2166 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002167 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002168 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002170 // Need to make a copy, in case evaluating the arguments makes
2171 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002172 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002173 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002174 ret = FAIL;
2175 else
2176 {
2177 funcexe_T funcexe;
2178
2179 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002180 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002181 funcexe.fe_firstline = curwin->w_cursor.lnum;
2182 funcexe.fe_lastline = curwin->w_cursor.lnum;
2183 funcexe.fe_evaluate = evaluate;
2184 funcexe.fe_partial = partial;
2185 funcexe.fe_basetv = basetv;
2186 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002187 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002188 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002189 }
2190 vim_free(s);
2191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002192 // If evaluate is FALSE rettv->v_type was not set in
2193 // get_func_tv, but it's needed in handle_subscript() to parse
2194 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002195 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2196 {
2197 rettv->vval.v_string = NULL;
2198 rettv->v_type = VAR_FUNC;
2199 }
2200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002201 // Stop the expression evaluation when immediately
2202 // aborting on error, or when an interrupt occurred or
2203 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002204 if (evaluate && aborting())
2205 {
2206 if (ret == OK)
2207 clear_tv(rettv);
2208 ret = FAIL;
2209 }
2210 return ret;
2211}
2212
2213/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002214 * After a NL, skip over empty lines and comment-only lines.
2215 */
2216 static char_u *
2217newline_skip_comments(char_u *arg)
2218{
2219 char_u *p = arg + 1;
2220
2221 for (;;)
2222 {
2223 p = skipwhite(p);
2224
2225 if (*p == NUL)
2226 break;
2227 if (vim9_comment_start(p))
2228 {
2229 char_u *nl = vim_strchr(p, NL);
2230
2231 if (nl == NULL)
2232 break;
2233 p = nl;
2234 }
2235 if (*p != NL)
2236 break;
2237 ++p; // skip another NL
2238 }
2239 return p;
2240}
2241
2242/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002243 * Get the next line source line without advancing. But do skip over comment
2244 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002245 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002246 */
2247 static char_u *
2248getline_peek_skip_comments(evalarg_T *evalarg)
2249{
2250 for (;;)
2251 {
2252 char_u *next = getline_peek(evalarg->eval_getline,
2253 evalarg->eval_cookie);
2254 char_u *p;
2255
2256 if (next == NULL)
2257 break;
2258 p = skipwhite(next);
2259 if (*p != NUL && !vim9_comment_start(p))
2260 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002261 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002262 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002263 }
2264 return NULL;
2265}
2266
2267/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002268 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2269 * comment) and there is a next line, return the next line (skipping blanks)
2270 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002271 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2272 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002273 * "arg" must point somewhere inside a line, not at the start.
2274 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002275 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002276eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2277{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002278 char_u *p = skipwhite(arg);
2279
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002281 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002283 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2284 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002285 && (*p == NUL || *p == NL
2286 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002288 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002289
Bram Moolenaare442d592022-05-05 12:20:28 +01002290 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002291 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002292 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002293 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002294 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002295 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296
Bram Moolenaar9d489562020-07-30 20:08:50 +02002297 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 {
2299 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002300 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 }
2302 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002303 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304}
2305
2306/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002307 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002308 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002309 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002310 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002311eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002312{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002313 garray_T *gap = &evalarg->eval_ga;
2314 char_u *line;
2315
Bram Moolenaar39be4982022-05-06 21:51:50 +01002316 if (arg != NULL)
2317 {
2318 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002319 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002320 // Truncate before a trailing comment, so that concatenating the lines
2321 // won't turn the rest into a comment.
2322 if (*skipwhite(arg) == '#')
2323 *arg = NUL;
2324 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002325
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002326 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002327 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2328 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002329 else
2330 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002331 if (line == NULL)
2332 return NULL;
2333
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002334 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002335 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2336 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002337 char_u *p = skipwhite(line);
2338
2339 // Going to concatenate the lines after parsing. For an empty or
2340 // comment line use an empty string.
2341 if (*p == NUL || vim9_comment_start(p))
2342 {
2343 vim_free(line);
2344 line = vim_strsave((char_u *)"");
2345 }
2346
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002347 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2348 ++gap->ga_len;
2349 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002350 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002351 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002352 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002353 evalarg->eval_tofree = line;
2354 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002355
2356 // Advanced to the next line, "arg" no longer points into the previous
2357 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002358 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002359 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002360}
2361
Bram Moolenaare6b53242020-07-01 17:28:33 +02002362/*
2363 * Call eval_next_non_blank() and get the next line if needed.
2364 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002365 char_u *
2366skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2367{
2368 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002369 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002370
Bram Moolenaar962d7212020-07-04 14:15:00 +02002371 if (evalarg == NULL)
2372 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002373 eval_next_non_blank(p, evalarg, &getnext);
2374 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002375 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002376 return p;
2377}
2378
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2383 */
2384
2385/*
2386 * Handle zero level expression.
2387 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002389 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002390 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 * Return OK or FAIL.
2392 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002394eval0(
2395 char_u *arg,
2396 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002397 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002400 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2401}
2402
2403/*
2404 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2405 * expression and don't check what comes after the expression.
2406 */
2407 int
2408eval0_retarg(
2409 char_u *arg,
2410 typval_T *rettv,
2411 exarg_T *eap,
2412 evalarg_T *evalarg,
2413 char_u **retarg)
2414{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 int ret;
2416 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002417 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002418 int did_emsg_before = did_emsg;
2419 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002421 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002422 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002426
Bram Moolenaar79481362022-06-27 20:15:10 +01002427 if (ret != FAIL)
2428 {
2429 expr_end = p;
2430 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002431
Bram Moolenaar79481362022-06-27 20:15:10 +01002432 // In Vim9 script a command block is not split at NL characters for
2433 // commands using an expression argument. Skip over a '#' comment to
2434 // check for a following NL. Require white space before the '#'.
2435 if (in_vim9script() && p > expr_end && retarg == NULL)
2436 while (*p == '#')
2437 {
2438 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002439
Bram Moolenaar79481362022-06-27 20:15:10 +01002440 if (nl == NULL)
2441 break;
2442 p = skipwhite(nl + 1);
2443 if (eap != NULL && *p != NUL)
2444 eap->nextcmd = p;
2445 check_for_end = FALSE;
2446 }
2447
2448 if (check_for_end)
2449 end_error = !ends_excmd2(arg, p);
2450 }
2451
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002452 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
2454 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /*
2457 * Report the invalid expression unless the expression evaluation has
2458 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002459 * exception, or we already gave a more specific error.
2460 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002462 if (!aborting()
2463 && did_emsg == did_emsg_before
2464 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002465 && (flags & EVAL_CONSTANT) == 0
2466 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002467 {
2468 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002469 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002470 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002471 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002472 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002473
2474 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002475 // a next command to avoid more errors, unless "|" is following, which
2476 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002477 if (eap != NULL && p != NULL
2478 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002479 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002482
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002483 if (retarg != NULL)
2484 *retarg = p;
2485 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002486 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 return ret;
2489}
2490
2491/*
2492 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002493 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002494 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 *
2496 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002497 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002499 * Note: "rettv.v_lock" is not set.
2500 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * Return OK or FAIL.
2502 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002503 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002504eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002506 char_u *p;
2507 int getnext;
2508
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002509 CLEAR_POINTER(rettv);
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * Get the first variable.
2513 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002514 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 return FAIL;
2516
Bram Moolenaar793648f2020-06-26 21:28:25 +02002517 p = eval_next_non_blank(*arg, evalarg, &getnext);
2518 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002520 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002521 int result;
2522 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 evalarg_T *evalarg_used = evalarg;
2524 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002526 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002527 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528
2529 if (evalarg == NULL)
2530 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002531 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002533 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002534 orig_flags = evalarg_used->eval_flags;
2535 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002538 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002539 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002540 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002541 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002542 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002543 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002544 clear_tv(rettv);
2545 return FAIL;
2546 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002547 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002553 int error = FALSE;
2554
Bram Moolenaar13106602020-10-04 16:06:05 +02002555 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002556 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002557 else if (vim9script)
2558 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002559 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002561 if (error || !op_falsy || !result)
2562 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002563 if (error)
2564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566
2567 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002568 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002570 if (op_falsy)
2571 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002572 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002573 {
mityu4ac198c2021-05-28 17:52:40 +02002574 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002575 clear_tv(rettv);
2576 return FAIL;
2577 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002579 evalarg_used->eval_flags = (op_falsy ? !result : result)
2580 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2581 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002582 {
2583 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002585 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002586 if (!op_falsy || !result)
2587 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002589 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002591 /*
2592 * Check for the ":".
2593 */
2594 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2595 if (*p != ':')
2596 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002597 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002598 if (evaluate && result)
2599 clear_tv(rettv);
2600 evalarg_used->eval_flags = orig_flags;
2601 return FAIL;
2602 }
2603 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002604 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002605 else
2606 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002607 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002608 {
2609 error_white_both(p, 1);
2610 clear_tv(rettv);
2611 evalarg_used->eval_flags = orig_flags;
2612 return FAIL;
2613 }
2614 *arg = p;
2615 }
2616
2617 /*
2618 * Get the third variable. Recursive!
2619 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002620 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002621 {
mityu4ac198c2021-05-28 17:52:40 +02002622 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002623 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002624 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002625 return FAIL;
2626 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002627 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2628 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002629 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002630 if (eval1(arg, &var2, evalarg_used) == FAIL)
2631 {
2632 if (evaluate && result)
2633 clear_tv(rettv);
2634 evalarg_used->eval_flags = orig_flags;
2635 return FAIL;
2636 }
2637 if (evaluate && !result)
2638 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640
2641 if (evalarg == NULL)
2642 clear_evalarg(&local_evalarg, NULL);
2643 else
2644 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646
2647 return OK;
2648}
2649
2650/*
2651 * Handle first level expression:
2652 * expr2 || expr2 || expr2 logical OR
2653 *
2654 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002655 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 *
2657 * Return OK or FAIL.
2658 */
2659 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002662 char_u *p;
2663 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002666 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002668 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return FAIL;
2670
2671 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002674 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677 evalarg_T *evalarg_used = evalarg;
2678 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679 int evaluate;
2680 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 long result = FALSE;
2682 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002684 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002685
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 if (evalarg == NULL)
2687 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002688 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 orig_flags = evalarg_used->eval_flags;
2692 evaluate = orig_flags & EVAL_EVALUATE;
2693 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002695 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002696 result = tv_get_bool_chk(rettv, &error);
2697 else if (tv_get_number_chk(rettv, &error) != 0)
2698 result = TRUE;
2699 clear_tv(rettv);
2700 if (error)
2701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703
2704 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707 while (p[0] == '|' && p[1] == '|')
2708 {
2709 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002710 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002711 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002712 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002713 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002714 {
2715 error_white_both(p, 2);
2716 clear_tv(rettv);
2717 return FAIL;
2718 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002719 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002720 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721
2722 /*
2723 * Get the second variable.
2724 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002725 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002726 {
mityu4ac198c2021-05-28 17:52:40 +02002727 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002728 clear_tv(rettv);
2729 return FAIL;
2730 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2732 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002733 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002736
2737 /*
2738 * Compute the result.
2739 */
2740 if (evaluate && !result)
2741 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002742 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002743 result = tv_get_bool_chk(&var2, &error);
2744 else if (tv_get_number_chk(&var2, &error) != 0)
2745 result = TRUE;
2746 clear_tv(&var2);
2747 if (error)
2748 return FAIL;
2749 }
2750 if (evaluate)
2751 {
2752 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002753 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002754 rettv->v_type = VAR_BOOL;
2755 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002756 }
2757 else
2758 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002759 rettv->v_type = VAR_NUMBER;
2760 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002761 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002763
2764 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002766
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767 if (evalarg == NULL)
2768 clear_evalarg(&local_evalarg, NULL);
2769 else
2770 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772
2773 return OK;
2774}
2775
2776/*
2777 * Handle second level expression:
2778 * expr3 && expr3 && expr3 logical AND
2779 *
2780 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002781 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 *
2783 * Return OK or FAIL.
2784 */
2785 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002788 char_u *p;
2789 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002792 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 return FAIL;
2796
2797 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002798 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002800 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002803 evalarg_T *evalarg_used = evalarg;
2804 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002805 int orig_flags;
2806 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002807 long result = TRUE;
2808 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002809 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002810 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002811
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 if (evalarg == NULL)
2813 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002814 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002817 orig_flags = evalarg_used->eval_flags;
2818 evaluate = orig_flags & EVAL_EVALUATE;
2819 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002820 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002821 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002822 result = tv_get_bool_chk(rettv, &error);
2823 else if (tv_get_number_chk(rettv, &error) == 0)
2824 result = FALSE;
2825 clear_tv(rettv);
2826 if (error)
2827 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829
2830 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002831 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002833 while (p[0] == '&' && p[1] == '&')
2834 {
2835 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002836 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002837 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002838 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002839 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002840 {
2841 error_white_both(p, 2);
2842 clear_tv(rettv);
2843 return FAIL;
2844 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002845 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002846 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847
2848 /*
2849 * Get the second variable.
2850 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002851 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002852 {
mityu4ac198c2021-05-28 17:52:40 +02002853 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002854 clear_tv(rettv);
2855 return FAIL;
2856 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002857 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2858 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002860 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002861 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002862 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002863
2864 /*
2865 * Compute the result.
2866 */
2867 if (evaluate && result)
2868 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002869 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002870 result = tv_get_bool_chk(&var2, &error);
2871 else if (tv_get_number_chk(&var2, &error) == 0)
2872 result = FALSE;
2873 clear_tv(&var2);
2874 if (error)
2875 return FAIL;
2876 }
2877 if (evaluate)
2878 {
2879 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002880 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002881 rettv->v_type = VAR_BOOL;
2882 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 }
2884 else
2885 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002886 rettv->v_type = VAR_NUMBER;
2887 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002888 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002889 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002890
2891 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002893
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002894 if (evalarg == NULL)
2895 clear_evalarg(&local_evalarg, NULL);
2896 else
2897 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899
2900 return OK;
2901}
2902
2903/*
2904 * Handle third level expression:
2905 * var1 == var2
2906 * var1 =~ var2
2907 * var1 != var2
2908 * var1 !~ var2
2909 * var1 > var2
2910 * var1 >= var2
2911 * var1 < var2
2912 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002913 * var1 is var2
2914 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 *
2916 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002917 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 *
2919 * Return OK or FAIL.
2920 */
2921 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002925 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002926 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002928 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002931 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 return FAIL;
2935
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002936 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002937
Bram Moolenaar696ba232020-07-29 21:20:41 +02002938 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
2940 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002941 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002943 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002945 typval_T var2;
2946 int ic;
2947 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002948 int evaluate = evalarg == NULL
2949 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002950 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002951
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002952 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002953 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002954 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002955 p = *arg;
2956 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002957 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2958 {
mityu4ac198c2021-05-28 17:52:40 +02002959 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002960 clear_tv(rettv);
2961 return FAIL;
2962 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002963
Bram Moolenaar696ba232020-07-29 21:20:41 +02002964 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2965 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002966 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002967 clear_tv(rettv);
2968 return FAIL;
2969 }
2970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002971 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (p[len] == '?')
2973 {
2974 ic = TRUE;
2975 ++len;
2976 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002977 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 else if (p[len] == '#')
2979 {
2980 ic = FALSE;
2981 ++len;
2982 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002983 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002985 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /*
2988 * Get the second variable.
2989 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002990 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2991 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002992 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002993 clear_tv(rettv);
2994 return FAIL;
2995 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002996 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 return FAIL;
3001 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003002 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003003 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003004 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003005
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003006 // use the line of the comparison for messages
3007 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003008 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003009 {
3010 ret = FAIL;
3011 clear_tv(rettv);
3012 }
3013 else
3014 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003015 clear_tv(&var2);
3016 return ret;
3017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019
3020 return OK;
3021}
3022
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003023/*
3024 * Make a copy of blob "tv1" and append blob "tv2".
3025 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 void
3027eval_addblob(typval_T *tv1, typval_T *tv2)
3028{
3029 blob_T *b1 = tv1->vval.v_blob;
3030 blob_T *b2 = tv2->vval.v_blob;
3031 blob_T *b = blob_alloc();
3032 int i;
3033
3034 if (b != NULL)
3035 {
3036 for (i = 0; i < blob_len(b1); i++)
3037 ga_append(&b->bv_ga, blob_get(b1, i));
3038 for (i = 0; i < blob_len(b2); i++)
3039 ga_append(&b->bv_ga, blob_get(b2, i));
3040
3041 clear_tv(tv1);
3042 rettv_blob_set(tv1, b);
3043 }
3044}
3045
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003046/*
3047 * Make a copy of list "tv1" and append list "tv2".
3048 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 int
3050eval_addlist(typval_T *tv1, typval_T *tv2)
3051{
3052 typval_T var3;
3053
3054 // concatenate Lists
3055 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3056 {
3057 clear_tv(tv1);
3058 clear_tv(tv2);
3059 return FAIL;
3060 }
3061 clear_tv(tv1);
3062 *tv1 = var3;
3063 return OK;
3064}
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003067 * Handle the bitwise left/right shift operator expression:
3068 * var1 << var2
3069 * var1 >> var2
3070 *
3071 * "arg" must point to the first non-white of the expression.
3072 * "arg" is advanced to just after the recognized expression.
3073 *
3074 * Return OK or FAIL.
3075 */
3076 static int
3077eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3078{
3079 /*
3080 * Get the first expression.
3081 */
3082 if (eval6(arg, rettv, evalarg) == FAIL)
3083 return FAIL;
3084
3085 /*
3086 * Repeat computing, until no '<<' or '>>' is following.
3087 */
3088 for (;;)
3089 {
3090 char_u *p;
3091 int getnext;
3092 exprtype_T type;
3093 int evaluate;
3094 typval_T var2;
3095 int vim9script;
3096
3097 p = eval_next_non_blank(*arg, evalarg, &getnext);
3098 if (p[0] == '<' && p[1] == '<')
3099 type = EXPR_LSHIFT;
3100 else if (p[0] == '>' && p[1] == '>')
3101 type = EXPR_RSHIFT;
3102 else
3103 return OK;
3104
3105 // Handle a bitwise left or right shift operator
3106 if (rettv->v_type != VAR_NUMBER)
3107 {
3108 // left operand should be a number
3109 emsg(_(e_bitshift_ops_must_be_number));
3110 clear_tv(rettv);
3111 return FAIL;
3112 }
3113
3114 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3115 vim9script = in_vim9script();
3116 if (getnext)
3117 {
3118 *arg = eval_next_line(*arg, evalarg);
3119 p = *arg;
3120 }
3121 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3122 {
3123 error_white_both(*arg, 2);
3124 clear_tv(rettv);
3125 return FAIL;
3126 }
3127
3128 /*
3129 * Get the second variable.
3130 */
3131 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3132 {
3133 error_white_both(p, 2);
3134 clear_tv(rettv);
3135 return FAIL;
3136 }
3137 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3138 if (eval6(arg, &var2, evalarg) == FAIL)
3139 {
3140 clear_tv(rettv);
3141 return FAIL;
3142 }
3143
3144 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3145 {
3146 // right operand should be a positive number
3147 if (var2.v_type != VAR_NUMBER)
3148 emsg(_(e_bitshift_ops_must_be_number));
3149 else
3150 emsg(_(e_bitshift_ops_must_be_postive));
3151 clear_tv(rettv);
3152 clear_tv(&var2);
3153 return FAIL;
3154 }
3155
3156 if (evaluate)
3157 {
3158 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3159 // shifting more bits than we have always results in zero
3160 rettv->vval.v_number = 0;
3161 else if (type == EXPR_LSHIFT)
3162 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003163 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003164 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003165 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003166 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003167 }
3168
3169 clear_tv(&var2);
3170 }
3171
3172 return OK;
3173}
3174
3175/*
3176 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003177 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003179 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003180 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 *
3182 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003183 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 *
3185 * Return OK or FAIL.
3186 */
3187 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003188eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003191 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003193 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return FAIL;
3195
3196 /*
3197 * Repeat computing, until no '+', '-' or '.' is following.
3198 */
3199 for (;;)
3200 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003201 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003202 int getnext;
3203 char_u *p;
3204 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003205 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003206 int concat;
3207 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003208 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003210 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003211 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003212 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 p = eval_next_non_blank(*arg, evalarg, &getnext);
3214 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003215 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003216 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3217 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003219 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3220 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003221
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003222 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003223 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003225 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003226 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003227 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003228 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003229 {
mityu4ac198c2021-05-28 17:52:40 +02003230 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003231 clear_tv(rettv);
3232 return FAIL;
3233 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003234 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003235 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003236 if ((op != '+' || (rettv->v_type != VAR_LIST
3237 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003238#ifdef FEAT_FLOAT
3239 && (op == '.' || rettv->v_type != VAR_FLOAT)
3240#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003241 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003242 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003243 int error = FALSE;
3244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003245 // For "list + ...", an illegal use of the first operand as
3246 // a number cannot be determined before evaluating the 2nd
3247 // operand: if this is also a list, all is ok.
3248 // For "something . ...", "something - ..." or "non-list + ...",
3249 // we know that the first operand needs to be a string or number
3250 // without evaluating the 2nd operand. So check before to avoid
3251 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003252 if (op != '.')
3253 tv_get_number_chk(rettv, &error);
3254 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003255 {
3256 clear_tv(rettv);
3257 return FAIL;
3258 }
3259 }
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 /*
3262 * Get the second variable.
3263 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003264 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003265 {
mityu89dcb4d2021-05-28 13:50:17 +02003266 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003267 clear_tv(rettv);
3268 return FAIL;
3269 }
3270 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003271 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 return FAIL;
3275 }
3276
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003277 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 /*
3280 * Compute the result.
3281 */
3282 if (op == '.')
3283 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3285 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003286 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003287
Bram Moolenaar2e086612020-08-25 22:37:48 +02003288 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003289 || var2.v_type == VAR_CHANNEL
3290 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003291 semsg(_(e_using_invalid_value_as_string_str),
3292 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003293#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003294 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003295 {
3296 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3297 var2.vval.v_float);
3298 s2 = buf2;
3299 }
3300#endif
3301 else
3302 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003303 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003304 {
3305 clear_tv(rettv);
3306 clear_tv(&var2);
3307 return FAIL;
3308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003309 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003310 clear_tv(rettv);
3311 rettv->v_type = VAR_STRING;
3312 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003314 else if (op == '+' && rettv->v_type == VAR_BLOB
3315 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003317 else if (op == '+' && rettv->v_type == VAR_LIST
3318 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003319 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003321 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else
3324 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003325 int error = FALSE;
3326 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328 float_T f1 = 0, f2 = 0;
3329
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003330 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003332 f1 = rettv->vval.v_float;
3333 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003335 else
3336#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003338 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 if (error)
3340 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003341 // This can only happen for "list + non-list" or
3342 // "blob + non-blob". For "non-list + ..." or
3343 // "something - ...", we returned before evaluating the
3344 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003346 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 return FAIL;
3348 }
3349#ifdef FEAT_FLOAT
3350 if (var2.v_type == VAR_FLOAT)
3351 f1 = n1;
3352#endif
3353 }
3354#ifdef FEAT_FLOAT
3355 if (var2.v_type == VAR_FLOAT)
3356 {
3357 f2 = var2.vval.v_float;
3358 n2 = 0;
3359 }
3360 else
3361#endif
3362 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003363 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364 if (error)
3365 {
3366 clear_tv(rettv);
3367 clear_tv(&var2);
3368 return FAIL;
3369 }
3370#ifdef FEAT_FLOAT
3371 if (rettv->v_type == VAR_FLOAT)
3372 f2 = n2;
3373#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003374 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003375 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003376
3377#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003378 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3380 {
3381 if (op == '+')
3382 f1 = f1 + f2;
3383 else
3384 f1 = f1 - f2;
3385 rettv->v_type = VAR_FLOAT;
3386 rettv->vval.v_float = f1;
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003389#endif
3390 {
3391 if (op == '+')
3392 n1 = n1 + n2;
3393 else
3394 n1 = n1 - n2;
3395 rettv->v_type = VAR_NUMBER;
3396 rettv->vval.v_number = n1;
3397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401 }
3402 return OK;
3403}
3404
3405/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003406 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 * * number multiplication
3408 * / number division
3409 * % number modulo
3410 *
3411 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003412 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 *
3414 * Return OK or FAIL.
3415 */
3416 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003417eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003418 char_u **arg,
3419 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003420 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003421 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003423#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003424 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003428 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003430 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return FAIL;
3432
3433 /*
3434 * Repeat computing, until no '*', '/' or '%' is following.
3435 */
3436 for (;;)
3437 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003438 int evaluate;
3439 int getnext;
3440 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003441 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003442 int op;
3443 varnumber_T n1, n2;
3444#ifdef FEAT_FLOAT
3445 float_T f1, f2;
3446#endif
3447 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003448
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003449 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003450 p = eval_next_non_blank(*arg, evalarg, &getnext);
3451 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003452 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003454
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003455 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003456 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003457 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003458 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003459 {
3460 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3461 {
mityu4ac198c2021-05-28 17:52:40 +02003462 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003463 clear_tv(rettv);
3464 return FAIL;
3465 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003466 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003469#ifdef FEAT_FLOAT
3470 f1 = 0;
3471 f2 = 0;
3472#endif
3473 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003474 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003476#ifdef FEAT_FLOAT
3477 if (rettv->v_type == VAR_FLOAT)
3478 {
3479 f1 = rettv->vval.v_float;
3480 use_float = TRUE;
3481 n1 = 0;
3482 }
3483 else
3484#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003485 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003487 if (error)
3488 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 else
3491 n1 = 0;
3492
3493 /*
3494 * Get the second variable.
3495 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003496 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3497 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003498 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003499 clear_tv(rettv);
3500 return FAIL;
3501 }
3502 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003503 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 return FAIL;
3505
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003506 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003508#ifdef FEAT_FLOAT
3509 if (var2.v_type == VAR_FLOAT)
3510 {
3511 if (!use_float)
3512 {
3513 f1 = n1;
3514 use_float = TRUE;
3515 }
3516 f2 = var2.vval.v_float;
3517 n2 = 0;
3518 }
3519 else
3520#endif
3521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003522 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 clear_tv(&var2);
3524 if (error)
3525 return FAIL;
3526#ifdef FEAT_FLOAT
3527 if (use_float)
3528 f2 = n2;
3529#endif
3530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 /*
3533 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003536#ifdef FEAT_FLOAT
3537 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539 if (op == '*')
3540 f1 = f1 * f2;
3541 else if (op == '/')
3542 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003543# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003544 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003545 if (f2 == 0.0)
3546 {
3547 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003548 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003549 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003550 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003551 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003552 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003553 }
3554 else
3555 f1 = f1 / f2;
3556# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 // We rely on the floating point library to handle divide
3558 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003560# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003564 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003565 return FAIL;
3566 }
3567 rettv->v_type = VAR_FLOAT;
3568 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003573 int failed = FALSE;
3574
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003575 if (op == '*')
3576 n1 = n1 * n2;
3577 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003578 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003580 n1 = num_modulus(n1, n2, &failed);
3581 if (failed)
3582 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003583
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003584 rettv->v_type = VAR_NUMBER;
3585 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 }
3589
3590 return OK;
3591}
3592
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003593/*
3594 * Handle a type cast before a base level expression.
3595 * "arg" must point to the first non-white of the expression.
3596 * "arg" is advanced to just after the recognized expression.
3597 * Return OK or FAIL.
3598 */
3599 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003600eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003601 char_u **arg,
3602 typval_T *rettv,
3603 evalarg_T *evalarg,
3604 int want_string) // after "." operator
3605{
3606 type_T *want_type = NULL;
3607 garray_T type_list; // list of pointers to allocated types
3608 int res;
3609 int evaluate = evalarg == NULL ? 0
3610 : (evalarg->eval_flags & EVAL_EVALUATE);
3611
3612 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003613 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3614 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003615 {
3616 ++*arg;
3617 ga_init2(&type_list, sizeof(type_T *), 10);
3618 want_type = parse_type(arg, &type_list, TRUE);
3619 if (want_type == NULL && (evaluate || **arg != '>'))
3620 {
3621 clear_type_list(&type_list);
3622 return FAIL;
3623 }
3624
3625 if (**arg != '>')
3626 {
3627 if (*skipwhite(*arg) == '>')
3628 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3629 else
3630 emsg(_(e_missing_gt));
3631 clear_type_list(&type_list);
3632 return FAIL;
3633 }
3634 ++*arg;
3635 *arg = skipwhite_and_linebreak(*arg, evalarg);
3636 }
3637
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003638 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003639
3640 if (want_type != NULL && evaluate)
3641 {
3642 if (res == OK)
3643 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003644 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3645 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003646
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003647 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003648 {
3649 if (want_type == &t_bool && actual != &t_bool
3650 && (actual->tt_flags & TTFLAG_BOOL_OK))
3651 {
3652 int n = tv2bool(rettv);
3653
3654 // can use "0" and "1" for boolean in some places
3655 clear_tv(rettv);
3656 rettv->v_type = VAR_BOOL;
3657 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3658 }
3659 else
3660 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003661 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003662
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003663 where.wt_variable = TRUE;
3664 res = check_type(want_type, actual, TRUE, where);
3665 }
3666 }
3667 }
3668 clear_type_list(&type_list);
3669 }
3670
3671 return res;
3672}
3673
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003674 int
3675eval_leader(char_u **arg, int vim9)
3676{
3677 char_u *s = *arg;
3678 char_u *p = *arg;
3679
3680 while (*p == '!' || *p == '-' || *p == '+')
3681 {
3682 char_u *n = skipwhite(p + 1);
3683
3684 // ++, --, -+ and +- are not accepted in Vim9 script
3685 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3686 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003687 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003688 return FAIL;
3689 }
3690 p = n;
3691 }
3692 *arg = p;
3693 return OK;
3694}
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003697 * Check for a predefined value "true", "false" and "null.*".
3698 * Return OK when recognized.
3699 */
3700 int
3701handle_predefined(char_u *s, int len, typval_T *rettv)
3702{
3703 switch (len)
3704 {
3705 case 4: if (STRNCMP(s, "true", 4) == 0)
3706 {
3707 rettv->v_type = VAR_BOOL;
3708 rettv->vval.v_number = VVAL_TRUE;
3709 return OK;
3710 }
3711 if (STRNCMP(s, "null", 4) == 0)
3712 {
3713 rettv->v_type = VAR_SPECIAL;
3714 rettv->vval.v_number = VVAL_NULL;
3715 return OK;
3716 }
3717 break;
3718 case 5: if (STRNCMP(s, "false", 5) == 0)
3719 {
3720 rettv->v_type = VAR_BOOL;
3721 rettv->vval.v_number = VVAL_FALSE;
3722 return OK;
3723 }
3724 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003725 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3726 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003728 rettv->v_type = VAR_JOB;
3729 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003730#else
3731 rettv->v_type = VAR_SPECIAL;
3732 rettv->vval.v_number = VVAL_NULL;
3733#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003734 return OK;
3735 }
3736 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003737 case 9:
3738 if (STRNCMP(s, "null_", 5) != 0)
3739 break;
3740 if (STRNCMP(s + 5, "list", 4) == 0)
3741 {
3742 rettv->v_type = VAR_LIST;
3743 rettv->vval.v_list = NULL;
3744 return OK;
3745 }
3746 if (STRNCMP(s + 5, "dict", 4) == 0)
3747 {
3748 rettv->v_type = VAR_DICT;
3749 rettv->vval.v_dict = NULL;
3750 return OK;
3751 }
3752 if (STRNCMP(s + 5, "blob", 4) == 0)
3753 {
3754 rettv->v_type = VAR_BLOB;
3755 rettv->vval.v_blob = NULL;
3756 return OK;
3757 }
3758 break;
3759 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3760 {
3761 rettv->v_type = VAR_STRING;
3762 rettv->vval.v_string = NULL;
3763 return OK;
3764 }
3765 break;
3766 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003767 if (STRNCMP(s, "null_channel", 12) == 0)
3768 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003770 rettv->v_type = VAR_CHANNEL;
3771 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003772#else
3773 rettv->v_type = VAR_SPECIAL;
3774 rettv->vval.v_number = VVAL_NULL;
3775#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003776 return OK;
3777 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003778 if (STRNCMP(s, "null_partial", 12) == 0)
3779 {
3780 rettv->v_type = VAR_PARTIAL;
3781 rettv->vval.v_partial = NULL;
3782 return OK;
3783 }
3784 break;
3785 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3786 {
3787 rettv->v_type = VAR_FUNC;
3788 rettv->vval.v_string = NULL;
3789 return OK;
3790 }
3791 break;
3792 }
3793 return FAIL;
3794}
3795
3796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 * Handle sixth level expression:
3798 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003799 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003800 * "string" string constant
3801 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 * &option-name option value
3803 * @r register contents
3804 * identifier variable value
3805 * function() function call
3806 * $VAR environment variable
3807 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003810 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003811 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 *
3813 * Also handle:
3814 * ! in front logical NOT
3815 * - in front unary minus
3816 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003817 * trailing [] subscript in String or List
3818 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003819 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 *
3821 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003822 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 *
3824 * Return OK or FAIL.
3825 */
3826 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003827eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003828 char_u **arg,
3829 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003830 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003833 int evaluate = evalarg != NULL
3834 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int len;
3836 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003837 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u *start_leader, *end_leader;
3839 int ret = OK;
3840 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003841 static int recurse = 0;
3842 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003845 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003851 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 */
3853 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003854 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 end_leader = *arg;
3857
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003858 if (**arg == '.' && (!isdigit(*(*arg + 1))
3859#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003860 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003861#endif
3862 ))
3863 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003864 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003865 ++*arg;
3866 return FAIL;
3867 }
3868
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003869 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003870 // and crash. With MSVC the stack is smaller.
3871 if (recurse ==
3872#ifdef _MSC_VER
3873 300
3874#else
3875 1000
3876#endif
3877 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003878 {
3879 semsg(_(e_expression_too_recursive_str), *arg);
3880 return FAIL;
3881 }
3882 ++recurse;
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 switch (**arg)
3885 {
3886 /*
3887 * Number constant.
3888 */
3889 case '0':
3890 case '1':
3891 case '2':
3892 case '3':
3893 case '4':
3894 case '5':
3895 case '6':
3896 case '7':
3897 case '8':
3898 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003899 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003900
3901 // Apply prefixed "-" and "+" now. Matters especially when
3902 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003903 if (ret == OK && evaluate && end_leader > start_leader
3904 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003905 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 break;
3907
3908 /*
3909 * String constant: "string".
3910 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003911 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 break;
3913
3914 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003915 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003917 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003918 break;
3919
3920 /*
3921 * List: [expr, expr]
3922 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003923 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 break;
3925
3926 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003927 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003928 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003929 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003930 {
3931 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3932 }
3933 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003934 {
3935 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003936 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003937 }
3938 else
3939 ret = NOTDONE;
3940 break;
3941
3942 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003943 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003944 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003946 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003947 ret = NOTDONE;
3948 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003949 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003950 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003951 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003952 break;
3953
3954 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003955 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003957 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 break;
3959
3960 /*
3961 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003962 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
LemonBoy2eaef102022-05-06 13:14:50 +01003964 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3965 ret = eval_interp_string(arg, rettv, evaluate);
3966 else
3967 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 break;
3969
3970 /*
3971 * Register contents: @r.
3972 */
3973 case '@': ++*arg;
3974 if (evaluate)
3975 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003976 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003977 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003978 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003979 emsg_invreg(**arg);
3980 else
3981 {
3982 rettv->v_type = VAR_STRING;
3983 rettv->vval.v_string = get_reg_contents(**arg,
3984 GREG_EXPR_SRC);
3985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 if (**arg != NUL)
3988 ++*arg;
3989 break;
3990
3991 /*
3992 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003993 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003995 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003996 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003997 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003998 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003999 if (ret == OK && evaluate)
4000 {
4001 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4002
Bram Moolenaara9931532021-06-12 15:58:16 +02004003 // Compile it here to get the return type. The return
4004 // type is optional, when it's missing use t_unknown.
4005 // This is recognized in compile_return().
4006 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4007 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004008 if (compile_def_function(ufunc, FALSE,
4009 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004010 {
4011 clear_tv(rettv);
4012 ret = FAIL;
4013 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004014 }
4015 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004016 if (ret == NOTDONE)
4017 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004018 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004019 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004020
Bram Moolenaar9215f012020-06-27 21:18:00 +02004021 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004022 if (**arg == ')')
4023 ++*arg;
4024 else if (ret == OK)
4025 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004026 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004027 clear_tv(rettv);
4028 ret = FAIL;
4029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 break;
4032
Bram Moolenaar8c711452005-01-14 21:53:12 +00004033 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 break;
4035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036
4037 if (ret == NOTDONE)
4038 {
4039 /*
4040 * Must be a variable or function name.
4041 * Can also be a curly-braces kind of name: {expr}.
4042 */
4043 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004044 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004045 if (alias != NULL)
4046 s = alias;
4047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004048 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049 ret = FAIL;
4050 else
4051 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004052 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4053
Bram Moolenaar4525a572022-02-13 11:57:33 +00004054 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004055 {
4056 emsg(_(e_cannot_use_underscore_here));
4057 ret = FAIL;
4058 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004059 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004060 && s[0] == 's' && s[1] == ':')
4061 {
4062 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4063 ret = FAIL;
4064 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004065 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004066 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004067 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004068 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004069 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004070 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004071 else if (flags & EVAL_CONSTANT)
4072 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004074 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004075 // get the value of "true", "false", etc. or a variable
4076 ret = FAIL;
4077 if (vim9script)
4078 ret = handle_predefined(s, len, rettv);
4079 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004080 {
4081 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004082 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004083 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004084 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004086 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004087 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004088 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004089 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004090 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004091 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004093 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004094 }
4095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004096 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4097 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004098 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004099 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 /*
4102 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4103 */
4104 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004105 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004106
4107 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004108 return ret;
4109}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004111/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004112 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004113 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004114 * Adjusts "end_leaderp" until it is at "start_leader".
4115 */
4116 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004117eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004118 typval_T *rettv,
4119 int numeric_only,
4120 char_u *start_leader,
4121 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004122{
4123 char_u *end_leader = *end_leaderp;
4124 int ret = OK;
4125 int error = FALSE;
4126 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004127 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004128 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004129#ifdef FEAT_FLOAT
4130 float_T f = 0.0;
4131
4132 if (rettv->v_type == VAR_FLOAT)
4133 f = rettv->vval.v_float;
4134 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004135#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004136 {
4137 while (VIM_ISWHITE(end_leader[-1]))
4138 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004139 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004140 val = tv2bool(rettv);
4141 else
4142 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004143 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004144 if (error)
4145 {
4146 clear_tv(rettv);
4147 ret = FAIL;
4148 }
4149 else
4150 {
4151 while (end_leader > start_leader)
4152 {
4153 --end_leader;
4154 if (*end_leader == '!')
4155 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004156 if (numeric_only)
4157 {
4158 ++end_leader;
4159 break;
4160 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004161#ifdef FEAT_FLOAT
4162 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004163 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004164 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004165 {
4166 rettv->v_type = VAR_BOOL;
4167 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4168 }
4169 else
4170 f = !f;
4171 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004172 else
4173#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004174 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004175 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004176 type = VAR_BOOL;
4177 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004178 }
4179 else if (*end_leader == '-')
4180 {
4181#ifdef FEAT_FLOAT
4182 if (rettv->v_type == VAR_FLOAT)
4183 f = -f;
4184 else
4185#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004186 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004187 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004188 type = VAR_NUMBER;
4189 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 }
4191 }
4192#ifdef FEAT_FLOAT
4193 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004196 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004199#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004201 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004202 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004203 rettv->v_type = type;
4204 else
4205 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004206 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004209 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return ret;
4211}
4212
4213/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004214 * Call the function referred to in "rettv".
4215 */
4216 static int
4217call_func_rettv(
4218 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004219 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004220 typval_T *rettv,
4221 int evaluate,
4222 dict_T *selfdict,
4223 typval_T *basetv)
4224{
4225 partial_T *pt = NULL;
4226 funcexe_T funcexe;
4227 typval_T functv;
4228 char_u *s;
4229 int ret;
4230
4231 // need to copy the funcref so that we can clear rettv
4232 if (evaluate)
4233 {
4234 functv = *rettv;
4235 rettv->v_type = VAR_UNKNOWN;
4236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004237 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004238 if (functv.v_type == VAR_PARTIAL)
4239 {
4240 pt = functv.vval.v_partial;
4241 s = partial_name(pt);
4242 }
4243 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004244 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004245 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004246 if (s == NULL || *s == NUL)
4247 {
4248 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004249 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004250 goto theend;
4251 }
4252 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004253 }
4254 else
4255 s = (char_u *)"";
4256
Bram Moolenaara80faa82020-04-12 19:37:17 +02004257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004258 funcexe.fe_firstline = curwin->w_cursor.lnum;
4259 funcexe.fe_lastline = curwin->w_cursor.lnum;
4260 funcexe.fe_evaluate = evaluate;
4261 funcexe.fe_partial = pt;
4262 funcexe.fe_selfdict = selfdict;
4263 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004264 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004265
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004266theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // Clear the funcref afterwards, so that deleting it while
4268 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004269 if (evaluate)
4270 clear_tv(&functv);
4271
4272 return ret;
4273}
4274
4275/*
4276 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004277 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004278 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4279 */
4280 static int
4281eval_lambda(
4282 char_u **arg,
4283 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004284 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004285 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004286{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004287 int evaluate = evalarg != NULL
4288 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004289 typval_T base = *rettv;
4290 int ret;
4291
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004292 rettv->v_type = VAR_UNKNOWN;
4293
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004294 if (**arg == '{')
4295 {
4296 // ->{lambda}()
4297 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4298 }
4299 else
4300 {
4301 // ->(lambda)()
4302 ++*arg;
4303 ret = eval1(arg, rettv, evalarg);
4304 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004305 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004306 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004307 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004308 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004309 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004310 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4311 && rettv->v_type != VAR_PARTIAL)
4312 {
4313 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4314 return FAIL;
4315 }
4316 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004317 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004318 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004319 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004320
4321 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004322 {
4323 if (verbose)
4324 {
4325 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004326 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004327 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004328 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004329 }
4330 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004331 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004332 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004333 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004334 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004335
4336 // Clear the funcref afterwards, so that deleting it while
4337 // evaluating the arguments is possible (see test55).
4338 if (evaluate)
4339 clear_tv(&base);
4340
4341 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004342}
4343
4344/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004345 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004346 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004347 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4348 */
4349 static int
4350eval_method(
4351 char_u **arg,
4352 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004353 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004354 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004355{
4356 char_u *name;
4357 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004358 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004359 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004360 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004361 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004362 int evaluate = evalarg != NULL
4363 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004364
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004365 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004366
Bram Moolenaarac92e252019-08-03 21:58:38 +02004367 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004368 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004369 if (alias != NULL)
4370 name = alias;
4371
4372 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004373 {
4374 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004375 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004377 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004378 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004379 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004380 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004381
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004382 // If there is no "(" immediately following, but there is further on,
4383 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4384 // Does not handle anything where "(" is part of the expression.
4385 *arg = skipwhite(*arg);
4386
4387 if (**arg != '(' && alias == NULL
4388 && (paren = vim_strchr(*arg, '(')) != NULL)
4389 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004390 char_u *deref;
4391
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004392 *arg = name;
4393 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004394 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4395 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004396 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004397 *arg = name + len;
4398 ret = FAIL;
4399 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004400 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004401 {
4402 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004403 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004404 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004405 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004406 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004407
4408 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004409 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004410 *arg = skipwhite(*arg);
4411
4412 if (**arg != '(')
4413 {
4414 if (verbose)
4415 semsg(_(e_missing_parenthesis_str), name);
4416 ret = FAIL;
4417 }
4418 else if (VIM_ISWHITE((*arg)[-1]))
4419 {
4420 if (verbose)
4421 emsg(_(e_no_white_space_allowed_before_parenthesis));
4422 ret = FAIL;
4423 }
4424 else
4425 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004426 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004427 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004428 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004429
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004430 // Clear the funcref afterwards, so that deleting it while
4431 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004432 if (evaluate)
4433 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004434 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004435
Bram Moolenaarac92e252019-08-03 21:58:38 +02004436 return ret;
4437}
4438
4439/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004440 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4441 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4443 */
4444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004445eval_index(
4446 char_u **arg,
4447 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004448 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004449 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004450{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004451 int evaluate = evalarg != NULL
4452 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004454 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004456 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004457 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004458 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004460 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4461 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004463 init_tv(&var1);
4464 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 /*
4468 * dict.name
4469 */
4470 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004472 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004473 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004475 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 }
4477 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004478 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 /*
4480 * something[idx]
4481 *
4482 * Get the (first) variable from inside the [].
4483 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004484 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 if (**arg == ':')
4486 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004487 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004488 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004489 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004490 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004491 semsg(_(e_white_space_required_before_and_after_str_at_str),
4492 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004493 clear_tv(&var1);
4494 return FAIL;
4495 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004496 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004498 int error = FALSE;
4499
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004500#ifdef FEAT_FLOAT
4501 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004502 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004503 && var1.v_type == VAR_FLOAT)
4504 {
4505 var1.vval.v_string = typval_tostring(&var1, TRUE);
4506 var1.v_type = VAR_STRING;
4507 }
4508#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004509 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004510 tv_get_number_chk(&var1, &error);
4511 else
4512 error = tv_get_string_chk(&var1) == NULL;
4513 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004514 {
4515 // not a number or string
4516 clear_tv(&var1);
4517 return FAIL;
4518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520
4521 /*
4522 * Get the second variable from inside the [:].
4523 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004524 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004525 if (**arg == ':')
4526 {
4527 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004528 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004529 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004530 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004531 semsg(_(e_white_space_required_before_and_after_str_at_str),
4532 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004533 if (!empty1)
4534 clear_tv(&var1);
4535 return FAIL;
4536 }
4537 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 if (**arg == ']')
4539 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004540 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542 if (!empty1)
4543 clear_tv(&var1);
4544 return FAIL;
4545 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004546 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004549 if (!empty1)
4550 clear_tv(&var1);
4551 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004552 return FAIL;
4553 }
4554 }
4555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004557 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 if (**arg != ']')
4559 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004560 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004561 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 clear_tv(&var1);
4563 if (range)
4564 clear_tv(&var2);
4565 return FAIL;
4566 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004567 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004568 }
4569
4570 if (evaluate)
4571 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004573 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004575
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004576 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004579 clear_tv(&var2);
4580 return res;
4581 }
4582 return OK;
4583}
4584
4585/*
4586 * Check if "rettv" can have an [index] or [sli:ce]
4587 */
4588 int
4589check_can_index(typval_T *rettv, int evaluate, int verbose)
4590{
4591 switch (rettv->v_type)
4592 {
4593 case VAR_FUNC:
4594 case VAR_PARTIAL:
4595 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004596 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004597 return FAIL;
4598 case VAR_FLOAT:
4599#ifdef FEAT_FLOAT
4600 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004601 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004602 return FAIL;
4603#endif
4604 case VAR_BOOL:
4605 case VAR_SPECIAL:
4606 case VAR_JOB:
4607 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004608 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004609 if (verbose)
4610 emsg(_(e_cannot_index_special_variable));
4611 return FAIL;
4612 case VAR_UNKNOWN:
4613 case VAR_ANY:
4614 case VAR_VOID:
4615 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004617 emsg(_(e_cannot_index_special_variable));
4618 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004620 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004622 case VAR_STRING:
4623 case VAR_LIST:
4624 case VAR_DICT:
4625 case VAR_BLOB:
4626 break;
4627 case VAR_NUMBER:
4628 if (in_vim9script())
4629 emsg(_(e_cannot_index_number));
4630 break;
4631 }
4632 return OK;
4633}
4634
4635/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004636 * slice() function
4637 */
4638 void
4639f_slice(typval_T *argvars, typval_T *rettv)
4640{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004641 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004642 && ((argvars[0].v_type != VAR_STRING
4643 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004644 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004645 && check_for_list_arg(argvars, 0) == FAIL)
4646 || check_for_number_arg(argvars, 1) == FAIL
4647 || check_for_opt_number_arg(argvars, 2) == FAIL))
4648 return;
4649
Bram Moolenaar6601b622021-01-13 21:47:15 +01004650 if (check_can_index(argvars, TRUE, FALSE) == OK)
4651 {
4652 copy_tv(argvars, rettv);
4653 eval_index_inner(rettv, TRUE, argvars + 1,
4654 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4655 TRUE, NULL, 0, FALSE);
4656 }
4657}
4658
4659/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004660 * Apply index or range to "rettv".
4661 * "var1" is the first index, NULL for [:expr].
4662 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004663 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4664 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004665 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4666 */
4667 int
4668eval_index_inner(
4669 typval_T *rettv,
4670 int is_range,
4671 typval_T *var1,
4672 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004673 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 char_u *key,
4675 int keylen,
4676 int verbose)
4677{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004678 varnumber_T n1, n2 = 0;
4679 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680
4681 n1 = 0;
4682 if (var1 != NULL && rettv->v_type != VAR_DICT)
4683 n1 = tv_get_number(var1);
4684
4685 if (is_range)
4686 {
4687 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004689 if (verbose)
4690 emsg(_(e_cannot_slice_dictionary));
4691 return FAIL;
4692 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004693 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004694 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004695 else
4696 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004697 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004698
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004699 switch (rettv->v_type)
4700 {
4701 case VAR_UNKNOWN:
4702 case VAR_ANY:
4703 case VAR_VOID:
4704 case VAR_FUNC:
4705 case VAR_PARTIAL:
4706 case VAR_FLOAT:
4707 case VAR_BOOL:
4708 case VAR_SPECIAL:
4709 case VAR_JOB:
4710 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004711 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004712 break; // not evaluating, skipping over subscript
4713
4714 case VAR_NUMBER:
4715 case VAR_STRING:
4716 {
4717 char_u *s = tv_get_string(rettv);
4718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004720 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004721 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004722 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004723 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004724 else
4725 s = char_from_string(s, n1);
4726 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004727 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004729 // The resulting variable is a substring. If the indexes
4730 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 if (n1 < 0)
4732 {
4733 n1 = len + n1;
4734 if (n1 < 0)
4735 n1 = 0;
4736 }
4737 if (n2 < 0)
4738 n2 = len + n2;
4739 else if (n2 >= len)
4740 n2 = len;
4741 if (n1 >= len || n2 < 0 || n1 > n2)
4742 s = NULL;
4743 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004744 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 }
4746 else
4747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // The resulting variable is a string of a single
4749 // character. If the index is too big or negative the
4750 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 if (n1 >= len || n1 < 0)
4752 s = NULL;
4753 else
4754 s = vim_strnsave(s + n1, 1);
4755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
4757 rettv->v_type = VAR_STRING;
4758 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004759 }
4760 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004762 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004763 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4764 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765 break;
4766
4767 case VAR_LIST:
4768 if (var1 == NULL)
4769 n1 = 0;
4770 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004771 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004773 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 return FAIL;
4775 break;
4776
4777 case VAR_DICT:
4778 {
4779 dictitem_T *item;
4780 typval_T tmp;
4781
4782 if (key == NULL)
4783 {
4784 key = tv_get_string_chk(var1);
4785 if (key == NULL)
4786 return FAIL;
4787 }
4788
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004789 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004790
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004792 {
4793 if (verbose)
4794 {
4795 if (keylen > 0)
4796 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004797 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004798 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004800 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801
4802 copy_tv(&item->di_tv, &tmp);
4803 clear_tv(rettv);
4804 *rettv = tmp;
4805 }
4806 break;
4807 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004808 return OK;
4809}
4810
4811/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004812 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004813 */
4814 char_u *
4815partial_name(partial_T *pt)
4816{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004817 if (pt != NULL)
4818 {
4819 if (pt->pt_name != NULL)
4820 return pt->pt_name;
4821 if (pt->pt_func != NULL)
4822 return pt->pt_func->uf_name;
4823 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004824 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004825}
4826
Bram Moolenaarddecc252016-04-06 22:59:37 +02004827 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004829{
4830 int i;
4831
4832 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004833 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004834 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004836 if (pt->pt_name != NULL)
4837 {
4838 func_unref(pt->pt_name);
4839 vim_free(pt->pt_name);
4840 }
4841 else
4842 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004843
Bram Moolenaar54656012021-06-09 20:50:46 +02004844 // "out_up" is no longer used, decrement refcount on partial that owns it.
4845 partial_unref(pt->pt_outer.out_up_partial);
4846
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004847 // Using pt_outer from another partial.
4848 partial_unref(pt->pt_outer_partial);
4849
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004850 // Decrease the reference count for the context of a closure. If down
4851 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004852 if (pt->pt_funcstack != NULL)
4853 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004854 --pt->pt_funcstack->fs_refcount;
4855 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004856 }
4857
Bram Moolenaarddecc252016-04-06 22:59:37 +02004858 vim_free(pt);
4859}
4860
4861/*
4862 * Unreference a closure: decrement the reference count and free it when it
4863 * becomes zero.
4864 */
4865 void
4866partial_unref(partial_T *pt)
4867{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004868 if (pt != NULL)
4869 {
4870 if (--pt->pt_refcount <= 0)
4871 partial_free(pt);
4872
4873 // If the reference count goes down to one, the funcstack may be the
4874 // only reference and can be freed if no other partials reference it.
4875 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4876 funcstack_check_refcount(pt->pt_funcstack);
4877 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004878}
4879
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004880/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004881 * Return the next (unique) copy ID.
4882 * Used for serializing nested structures.
4883 */
4884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004885get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004886{
4887 current_copyID += COPYID_INC;
4888 return current_copyID;
4889}
4890
4891/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004892 * Garbage collection for lists and dictionaries.
4893 *
4894 * We use reference counts to be able to free most items right away when they
4895 * are no longer used. But for composite items it's possible that it becomes
4896 * unused while the reference count is > 0: When there is a recursive
4897 * reference. Example:
4898 * :let l = [1, 2, 3]
4899 * :let d = {9: l}
4900 * :let l[1] = d
4901 *
4902 * Since this is quite unusual we handle this with garbage collection: every
4903 * once in a while find out which lists and dicts are not referenced from any
4904 * variable.
4905 *
4906 * Here is a good reference text about garbage collection (refers to Python
4907 * but it applies to all reference-counting mechanisms):
4908 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004909 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004910
4911/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004912 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004913 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004914 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004915 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004916 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004917garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004918{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004919 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004920 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004921 buf_T *buf;
4922 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004923 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004924 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004925
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004926 if (!testing)
4927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004929 want_garbage_collect = FALSE;
4930 may_garbage_collect = FALSE;
4931 garbage_collect_at_exit = FALSE;
4932 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004933
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004934 // The execution stack can grow big, limit the size.
4935 if (exestack.ga_maxlen - exestack.ga_len > 500)
4936 {
4937 size_t new_len;
4938 char_u *pp;
4939 int n;
4940
4941 // Keep 150% of the current size, with a minimum of the growth size.
4942 n = exestack.ga_len / 2;
4943 if (n < exestack.ga_growsize)
4944 n = exestack.ga_growsize;
4945
4946 // Don't make it bigger though.
4947 if (exestack.ga_len + n < exestack.ga_maxlen)
4948 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004949 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004950 pp = vim_realloc(exestack.ga_data, new_len);
4951 if (pp == NULL)
4952 return FAIL;
4953 exestack.ga_maxlen = exestack.ga_len + n;
4954 exestack.ga_data = pp;
4955 }
4956 }
4957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 // We advance by two because we add one for items referenced through
4959 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004960 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004961
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004962 /*
4963 * 1. Go through all accessible variables and mark all lists and dicts
4964 * with copyID.
4965 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 // Don't free variables in the previous_funccal list unless they are only
4968 // referenced through previous_funccal. This must be first, because if
4969 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004973 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004976 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004977 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4978 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004982 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4983 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004984 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004985 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4986 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004987#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004988 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004989 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4990 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004991 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004992 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004993 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4994 NULL, NULL);
4995#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004998 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5000 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005002 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005004 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005005 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005008 abort = abort || set_ref_in_functions(copyID);
5009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005012
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005013 // funcstacks keep variables for closures
5014 abort = abort || set_ref_in_funcstacks(copyID);
5015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005016 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005017 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005018
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005019 // callbacks in buffers
5020 abort = abort || set_ref_in_buffers(copyID);
5021
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005022 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5023 abort = abort || set_ref_in_insexpand_funcs(copyID);
5024
5025 // 'operatorfunc' callback
5026 abort = abort || set_ref_in_opfunc(copyID);
5027
5028 // 'tagfunc' callback
5029 abort = abort || set_ref_in_tagfunc(copyID);
5030
5031 // 'imactivatefunc' and 'imstatusfunc' callbacks
5032 abort = abort || set_ref_in_im_funcs(copyID);
5033
Bram Moolenaar1dced572012-04-05 16:54:08 +02005034#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005035 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005036#endif
5037
Bram Moolenaardb913952012-06-29 12:54:53 +02005038#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005039 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005040#endif
5041
5042#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005043 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005044#endif
5045
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005046#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005047 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005048 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005049#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005050#ifdef FEAT_NETBEANS_INTG
5051 abort = abort || set_ref_in_nb_channel(copyID);
5052#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005053
Bram Moolenaare3188e22016-05-31 21:13:04 +02005054#ifdef FEAT_TIMERS
5055 abort = abort || set_ref_in_timer(copyID);
5056#endif
5057
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005058#ifdef FEAT_QUICKFIX
5059 abort = abort || set_ref_in_quickfix(copyID);
5060#endif
5061
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005062#ifdef FEAT_TERMINAL
5063 abort = abort || set_ref_in_term(copyID);
5064#endif
5065
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005066#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005067 abort = abort || set_ref_in_popups(copyID);
5068#endif
5069
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005071 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005072 /*
5073 * 2. Free lists and dictionaries that are not referenced.
5074 */
5075 did_free = free_unref_items(copyID);
5076
5077 /*
5078 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005080 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005082 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005083 else if (p_verbose > 0)
5084 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005085 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005086 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005087
5088 return did_free;
5089}
5090
5091/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005092 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005093 */
5094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005096{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005097 int did_free = FALSE;
5098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // Let all "free" functions know that we are here. This means no
5100 // dictionaries, lists, channels or jobs are to be freed, because we will
5101 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 in_free_unref_items = TRUE;
5103
5104 /*
5105 * PASS 1: free the contents of the items. We don't free the items
5106 * themselves yet, so that it is possible to decrement refcount counters
5107 */
5108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005110 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005112 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005113 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005114
5115#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Go through the list of jobs and free items without the copyID. This
5117 // must happen before doing channels, because jobs refer to channels, but
5118 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5123#endif
5124
5125 /*
5126 * PASS 2: free the items themselves.
5127 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005128 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005129 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005130
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 // Go through the list of jobs and free items without the copyID. This
5133 // must happen before doing channels, because jobs refer to channels, but
5134 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005135 free_unused_jobs(copyID, COPYID_MASK);
5136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005138 free_unused_channels(copyID, COPYID_MASK);
5139#endif
5140
5141 in_free_unref_items = FALSE;
5142
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005143 return did_free;
5144}
5145
5146/*
5147 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005148 * "list_stack" is used to add lists to be marked. Can be NULL.
5149 *
5150 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005151 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005153set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154{
5155 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005156 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005157 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005158 hashtab_T *cur_ht;
5159 ht_stack_T *ht_stack = NULL;
5160 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005161
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005162 cur_ht = ht;
5163 for (;;)
5164 {
5165 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // Mark each item in the hashtab. If the item contains a hashtab
5168 // it is added to ht_stack, if it contains a list it is added to
5169 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 todo = (int)cur_ht->ht_used;
5171 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5172 if (!HASHITEM_EMPTY(hi))
5173 {
5174 --todo;
5175 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5176 &ht_stack, list_stack);
5177 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005178 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005179
5180 if (ht_stack == NULL)
5181 break;
5182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005184 cur_ht = ht_stack->ht;
5185 tempitem = ht_stack;
5186 ht_stack = ht_stack->prev;
5187 free(tempitem);
5188 }
5189
5190 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005191}
5192
Dominique Pelle748b3082022-01-08 12:41:16 +00005193#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5194 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005196 * Mark a dict and its items with "copyID".
5197 * Returns TRUE if setting references failed somehow.
5198 */
5199 int
5200set_ref_in_dict(dict_T *d, int copyID)
5201{
5202 if (d != NULL && d->dv_copyID != copyID)
5203 {
5204 d->dv_copyID = copyID;
5205 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5206 }
5207 return FALSE;
5208}
Dominique Pelle748b3082022-01-08 12:41:16 +00005209#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005210
5211/*
5212 * Mark a list and its items with "copyID".
5213 * Returns TRUE if setting references failed somehow.
5214 */
5215 int
5216set_ref_in_list(list_T *ll, int copyID)
5217{
5218 if (ll != NULL && ll->lv_copyID != copyID)
5219 {
5220 ll->lv_copyID = copyID;
5221 return set_ref_in_list_items(ll, copyID, NULL);
5222 }
5223 return FALSE;
5224}
5225
5226/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005228 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5229 *
5230 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005232 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005233set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005234{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005235 listitem_T *li;
5236 int abort = FALSE;
5237 list_T *cur_l;
5238 list_stack_T *list_stack = NULL;
5239 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005240
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005241 cur_l = l;
5242 for (;;)
5243 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005244 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // Mark each item in the list. If the item contains a hashtab
5246 // it is added to ht_stack, if it contains a list it is added to
5247 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5249 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5250 ht_stack, &list_stack);
5251 if (list_stack == NULL)
5252 break;
5253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005255 cur_l = list_stack->list;
5256 tempitem = list_stack;
5257 list_stack = list_stack->prev;
5258 free(tempitem);
5259 }
5260
5261 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262}
5263
5264/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005265 * Mark the partial in callback 'cb' with "copyID".
5266 */
5267 int
5268set_ref_in_callback(callback_T *cb, int copyID)
5269{
5270 typval_T tv;
5271
5272 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5273 return FALSE;
5274
5275 tv.v_type = VAR_PARTIAL;
5276 tv.vval.v_partial = cb->cb_partial;
5277 return set_ref_in_item(&tv, copyID, NULL, NULL);
5278}
5279
5280/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005281 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005282 * "list_stack" is used to add lists to be marked. Can be NULL.
5283 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5284 *
5285 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005286 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005288set_ref_in_item(
5289 typval_T *tv,
5290 int copyID,
5291 ht_stack_T **ht_stack,
5292 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005293{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005294 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005296 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005297 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005298 dict_T *dd = tv->vval.v_dict;
5299
Bram Moolenaara03f2332016-02-06 18:09:59 +01005300 if (dd != NULL && dd->dv_copyID != copyID)
5301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005303 dd->dv_copyID = copyID;
5304 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005305 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005306 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5307 }
5308 else
5309 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005310 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5311
Bram Moolenaara03f2332016-02-06 18:09:59 +01005312 if (newitem == NULL)
5313 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 else
5315 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005316 newitem->ht = &dd->dv_hashtab;
5317 newitem->prev = *ht_stack;
5318 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005319 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005320 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005321 }
5322 }
5323 else if (tv->v_type == VAR_LIST)
5324 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005325 list_T *ll = tv->vval.v_list;
5326
Bram Moolenaara03f2332016-02-06 18:09:59 +01005327 if (ll != NULL && ll->lv_copyID != copyID)
5328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005329 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005330 ll->lv_copyID = copyID;
5331 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005332 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005333 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005334 }
5335 else
5336 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005337 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5338
Bram Moolenaara03f2332016-02-06 18:09:59 +01005339 if (newitem == NULL)
5340 abort = TRUE;
5341 else
5342 {
5343 newitem->list = ll;
5344 newitem->prev = *list_stack;
5345 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005346 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005347 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005348 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005349 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005350 else if (tv->v_type == VAR_FUNC)
5351 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005352 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005353 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005354 else if (tv->v_type == VAR_PARTIAL)
5355 {
5356 partial_T *pt = tv->vval.v_partial;
5357 int i;
5358
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005359 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005360 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005361 // Didn't see this partial yet.
5362 pt->pt_copyID = copyID;
5363
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005364 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005365
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005366 if (pt->pt_dict != NULL)
5367 {
5368 typval_T dtv;
5369
5370 dtv.v_type = VAR_DICT;
5371 dtv.vval.v_dict = pt->pt_dict;
5372 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5373 }
5374
5375 for (i = 0; i < pt->pt_argc; ++i)
5376 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5377 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005378 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005379 }
5380 }
5381#ifdef FEAT_JOB_CHANNEL
5382 else if (tv->v_type == VAR_JOB)
5383 {
5384 job_T *job = tv->vval.v_job;
5385 typval_T dtv;
5386
5387 if (job != NULL && job->jv_copyID != copyID)
5388 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005389 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 if (job->jv_channel != NULL)
5391 {
5392 dtv.v_type = VAR_CHANNEL;
5393 dtv.vval.v_channel = job->jv_channel;
5394 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5395 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005396 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005397 {
5398 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005399 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005400 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5401 }
5402 }
5403 }
5404 else if (tv->v_type == VAR_CHANNEL)
5405 {
5406 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005407 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 typval_T dtv;
5409 jsonq_T *jq;
5410 cbq_T *cq;
5411
5412 if (ch != NULL && ch->ch_copyID != copyID)
5413 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005414 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005415 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005416 {
5417 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5418 jq = jq->jq_next)
5419 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5420 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5421 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005422 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 {
5424 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005425 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5427 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005428 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429 {
5430 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005431 dtv.vval.v_partial =
5432 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005433 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5434 }
5435 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005436 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 {
5438 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005439 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005440 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5441 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005442 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005443 {
5444 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005445 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005446 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5447 }
5448 }
5449 }
5450#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005451 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005452}
5453
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 * Return a string with the string representation of a variable.
5456 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005458 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005459 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005460 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005461 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005462 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5463 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005464 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005466 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005467echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005468 typval_T *tv,
5469 char_u **tofree,
5470 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005471 int copyID,
5472 int echo_style,
5473 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005474 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005476 static int recurse = 0;
5477 char_u *r = NULL;
5478
Bram Moolenaar33570922005-01-25 22:26:29 +00005479 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005480 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005481 if (!did_echo_string_emsg)
5482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 // Only give this message once for a recursive call to avoid
5484 // flooding the user with errors. And stop iterating over lists
5485 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005486 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005487 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005488 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005489 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005490 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005491 }
5492 ++recurse;
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 switch (tv->v_type)
5495 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005496 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005497 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005498 {
5499 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005500 r = tv->vval.v_string;
5501 if (r == NULL)
5502 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005503 }
5504 else
5505 {
5506 *tofree = string_quote(tv->vval.v_string, FALSE);
5507 r = *tofree;
5508 }
5509 break;
5510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005512 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005513 char_u buf[MAX_FUNC_NAME_LEN];
5514
5515 if (echo_style)
5516 {
LemonBoya5d35902022-04-29 21:15:02 +01005517 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5518 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005519 buf, MAX_FUNC_NAME_LEN);
5520 if (r == buf)
5521 {
5522 r = vim_strsave(buf);
5523 *tofree = r;
5524 }
5525 else
5526 *tofree = NULL;
5527 }
5528 else
5529 {
5530 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5531 : make_ufunc_name_readable(
5532 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5533 TRUE);
5534 r = *tofree;
5535 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005536 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005538
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005539 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005540 {
5541 partial_T *pt = tv->vval.v_partial;
5542 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005543 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005544 garray_T ga;
5545 int i;
5546 char_u *tf;
5547
5548 ga_init2(&ga, 1, 100);
5549 ga_concat(&ga, (char_u *)"function(");
5550 if (fname != NULL)
5551 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005552 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005553 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005554 && vim_isupper(fname[1]))
5555 {
5556 ga_concat(&ga, (char_u *)"'g:");
5557 ga_concat(&ga, fname + 1);
5558 }
5559 else
5560 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005561 vim_free(fname);
5562 }
5563 if (pt != NULL && pt->pt_argc > 0)
5564 {
5565 ga_concat(&ga, (char_u *)", [");
5566 for (i = 0; i < pt->pt_argc; ++i)
5567 {
5568 if (i > 0)
5569 ga_concat(&ga, (char_u *)", ");
5570 ga_concat(&ga,
5571 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5572 vim_free(tf);
5573 }
5574 ga_concat(&ga, (char_u *)"]");
5575 }
5576 if (pt != NULL && pt->pt_dict != NULL)
5577 {
5578 typval_T dtv;
5579
5580 ga_concat(&ga, (char_u *)", ");
5581 dtv.v_type = VAR_DICT;
5582 dtv.vval.v_dict = pt->pt_dict;
5583 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5584 vim_free(tf);
5585 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005586 // terminate with ')' and a NUL
5587 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005588
5589 *tofree = ga.ga_data;
5590 r = *tofree;
5591 break;
5592 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005593
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005594 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005595 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005596 break;
5597
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005599 if (tv->vval.v_list == NULL)
5600 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005601 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005603 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005604 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005605 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5606 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005607 {
5608 *tofree = NULL;
5609 r = (char_u *)"[...]";
5610 }
5611 else
5612 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005613 int old_copyID = tv->vval.v_list->lv_copyID;
5614
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005615 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005616 *tofree = list2string(tv, copyID, restore_copyID);
5617 if (restore_copyID)
5618 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005619 r = *tofree;
5620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005621 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005624 if (tv->vval.v_dict == NULL)
5625 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005626 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005628 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005629 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005630 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5631 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005632 {
5633 *tofree = NULL;
5634 r = (char_u *)"{...}";
5635 }
5636 else
5637 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005638 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005639
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005641 *tofree = dict2string(tv, copyID, restore_copyID);
5642 if (restore_copyID)
5643 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005644 r = *tofree;
5645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005646 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005649 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005650 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005652 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005653 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005654 break;
5655
Bram Moolenaar835dc632016-02-07 14:27:38 +01005656 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005657 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005658#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005659 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005660 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5661 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005662 if (composite_val)
5663 {
5664 *tofree = string_quote(r, FALSE);
5665 r = *tofree;
5666 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005667#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005668 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005669
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005670 case VAR_INSTR:
5671 *tofree = NULL;
5672 r = (char_u *)"instructions";
5673 break;
5674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005675 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005676#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005677 *tofree = NULL;
5678 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5679 r = numbuf;
5680 break;
5681#endif
5682
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005683 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005684 case VAR_SPECIAL:
5685 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005686 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005687 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005689
Bram Moolenaar8502c702014-06-17 12:51:16 +02005690 if (--recurse == 0)
5691 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005692 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693}
5694
5695/*
5696 * Return a string with the string representation of a variable.
5697 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5698 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005699 * Does not put quotes around strings, as ":echo" displays values.
5700 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5701 * May return NULL.
5702 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005704echo_string(
5705 typval_T *tv,
5706 char_u **tofree,
5707 char_u *numbuf,
5708 int copyID)
5709{
5710 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5711}
5712
5713/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005714 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5715 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005716 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005717 */
5718 int
5719buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5720{
5721 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005722 char_u *t;
5723 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005724
5725 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5726 return -1;
5727
5728 if (lnum > buf->b_ml.ml_line_count)
5729 lnum = buf->b_ml.ml_line_count;
5730
5731 str = ml_get_buf(buf, lnum, FALSE);
5732 if (str == NULL)
5733 return -1;
5734
5735 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005736 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005737
Bram Moolenaar91458462021-01-13 20:08:38 +01005738 // count the number of characters
5739 t = str;
5740 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5741 t += mb_ptr2len(t);
5742
5743 // In insert mode, when the cursor is at the end of a non-empty line,
5744 // byteidx points to the NUL character immediately past the end of the
5745 // string. In this case, add one to the character count.
5746 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5747 count++;
5748
5749 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005750}
5751
5752/*
5753 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005754 * byte index. Works only for loaded buffers. Returns -1 on failure.
5755 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005756 */
5757 int
5758buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5759{
5760 char_u *str;
5761 char_u *t;
5762
5763 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5764 return -1;
5765
5766 if (lnum > buf->b_ml.ml_line_count)
5767 lnum = buf->b_ml.ml_line_count;
5768
5769 str = ml_get_buf(buf, lnum, FALSE);
5770 if (str == NULL)
5771 return -1;
5772
5773 // Convert the character offset to a byte offset
5774 t = str;
5775 while (*t != NUL && --charidx > 0)
5776 t += mb_ptr2len(t);
5777
Bram Moolenaar91458462021-01-13 20:08:38 +01005778 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005779}
5780
5781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005783 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005785 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005786var2fpos(
5787 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005788 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005789 int *fnum, // set to fnum for '0, 'A, etc.
5790 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005792 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005794 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005797 if (varp->v_type == VAR_LIST)
5798 {
5799 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005800 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005801 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005802 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005803
5804 l = varp->vval.v_list;
5805 if (l == NULL)
5806 return NULL;
5807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005808 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005809 pos.lnum = list_find_nr(l, 0L, &error);
5810 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005812 if (charcol)
5813 len = (long)mb_charlen(ml_get(pos.lnum));
5814 else
5815 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005816
Bram Moolenaarec65d772020-08-20 22:29:12 +02005817 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005819 li = list_find(l, 1L);
5820 if (li != NULL && li->li_tv.v_type == VAR_STRING
5821 && li->li_tv.vval.v_string != NULL
5822 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005823 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005824 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005825 }
5826 else
5827 {
5828 pos.col = list_find_nr(l, 1L, &error);
5829 if (error)
5830 return NULL;
5831 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005834 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005835 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005836 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005838 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005839 pos.coladd = list_find_nr(l, 2L, &error);
5840 if (error)
5841 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005842
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005843 return &pos;
5844 }
5845
Bram Moolenaarc5809432021-03-27 21:23:30 +01005846 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5847 return NULL;
5848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005849 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005850 if (name == NULL)
5851 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005852
5853 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005854 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005855 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005856 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005857 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005858 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005859 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005860 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005861 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005862 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005863 pos = VIsual;
5864 else
5865 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005866 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005867 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005868 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005870 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005871 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5873 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005874 pos = *pp;
5875 }
5876 if (pos.lnum != 0)
5877 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005878 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005879 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5880 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005882
Bram Moolenaara5525202006-03-02 22:52:09 +00005883 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005884
Bram Moolenaar477933c2007-07-17 14:32:23 +00005885 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005886 {
5887 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005888 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005889 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005890 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005891 // In silent Ex mode topline is zero, but that's not a valid line
5892 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005893 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005894 return &pos;
5895 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005896 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005897 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005898 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005899 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005900 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005901 return &pos;
5902 }
5903 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005904 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005906 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {
5908 pos.lnum = curbuf->b_ml.ml_line_count;
5909 pos.col = 0;
5910 }
5911 else
5912 {
5913 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005914 if (charcol)
5915 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5916 else
5917 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 return &pos;
5920 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005921 if (in_vim9script())
5922 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 return NULL;
5924}
5925
5926/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005927 * Convert list in "arg" into a position and optional file number.
5928 * When "fnump" is NULL there is no file number, only 3 items.
5929 * Note that the column is passed on as-is, the caller may want to decrement
5930 * it to use 1 for the first column.
5931 * Return FAIL when conversion is not possible, doesn't check the position for
5932 * validity.
5933 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005935list2fpos(
5936 typval_T *arg,
5937 pos_T *posp,
5938 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005939 colnr_T *curswantp,
5940 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005941{
5942 list_T *l = arg->vval.v_list;
5943 long i = 0;
5944 long n;
5945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005946 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5947 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005948 if (arg->v_type != VAR_LIST
5949 || l == NULL
5950 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005951 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005952 return FAIL;
5953
5954 if (fnump != NULL)
5955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005956 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005957 if (n < 0)
5958 return FAIL;
5959 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005961 *fnump = n;
5962 }
5963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005965 if (n < 0)
5966 return FAIL;
5967 posp->lnum = n;
5968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005970 if (n < 0)
5971 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005972 // If character position is specified, then convert to byte position
5973 if (charcol)
5974 {
5975 buf_T *buf;
5976
5977 // Get the text for the specified line in a loaded buffer
5978 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5979 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5980 return FAIL;
5981
Bram Moolenaar91458462021-01-13 20:08:38 +01005982 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005983 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984 posp->col = n;
5985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005986 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005987 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005988 posp->coladd = 0;
5989 else
5990 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005991
Bram Moolenaar493c1782014-05-28 14:34:46 +02005992 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005994
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005995 return OK;
5996}
5997
5998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 * Get the length of an environment variable name.
6000 * Advance "arg" to the first character after the name.
6001 * Return 0 for error.
6002 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 char_u *p;
6007 int len;
6008
6009 for (p = *arg; vim_isIDc(*p); ++p)
6010 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006011 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 return 0;
6013
6014 len = (int)(p - *arg);
6015 *arg = p;
6016 return len;
6017}
6018
6019/*
6020 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006021 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 * Return 0 if something is wrong.
6023 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 char_u *p;
6028 int len;
6029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006030 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006032 {
6033 if (*p == ':')
6034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006035 // "s:" is start of "s:var", but "n:" is not and can be used in
6036 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006037 len = (int)(p - *arg);
6038 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6039 || len > 1)
6040 break;
6041 }
6042 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006043 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 return 0;
6045
6046 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006047 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049 return len;
6050}
6051
6052/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006053 * Get the length of the name of a variable or function.
6054 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006056 * Return -1 if curly braces expansion failed.
6057 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * If the name contains 'magic' {}'s, expand them and return the
6059 * expanded name in an allocated string via 'alias' - caller must free.
6060 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062get_name_len(
6063 char_u **arg,
6064 char_u **alias,
6065 int evaluate,
6066 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 char_u *p;
6070 char_u *expr_start;
6071 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6076 && (*arg)[2] == (int)KE_SNR)
6077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006078 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 *arg += 3;
6080 return get_id_len(arg) + 3;
6081 }
6082 len = eval_fname_script(*arg);
6083 if (len > 0)
6084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 *arg += len;
6087 }
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006090 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006092 p = find_name_end(*arg, &expr_start, &expr_end,
6093 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (expr_start != NULL)
6095 {
6096 char_u *temp_string;
6097
6098 if (!evaluate)
6099 {
6100 len += (int)(p - *arg);
6101 *arg = skipwhite(p);
6102 return len;
6103 }
6104
6105 /*
6106 * Include any <SID> etc in the expanded string:
6107 * Thus the -len here.
6108 */
6109 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6110 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006111 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 *alias = temp_string;
6113 *arg = skipwhite(p);
6114 return (int)STRLEN(temp_string);
6115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006118 // Only give an error when there is something, otherwise it will be
6119 // reported at a higher level.
6120 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006121 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123 return len;
6124}
6125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006126/*
6127 * Find the end of a variable or function name, taking care of magic braces.
6128 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6129 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006130 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006131 * Return a pointer to just after the name. Equal to "arg" if there is no
6132 * valid name.
6133 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006134 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135find_name_end(
6136 char_u *arg,
6137 char_u **expr_start,
6138 char_u **expr_end,
6139 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006141 int mb_nest = 0;
6142 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006144 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006145 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 *expr_start = NULL;
6150 *expr_end = NULL;
6151 }
6152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006153 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006154 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6155 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006156 return arg;
6157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 for (p = arg; *p != NUL
6159 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006160 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006161 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006162 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006164 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006165 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006166 if (*p == '\'')
6167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006168 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006169 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006170 ;
6171 if (*p == NUL)
6172 break;
6173 }
6174 else if (*p == '"')
6175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006177 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006178 if (*p == '\\' && p[1] != NUL)
6179 ++p;
6180 if (*p == NUL)
6181 break;
6182 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006183 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006185 // "s:" is start of "s:var", but "n:" is not and can be used in
6186 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006187 len = (int)(p - arg);
6188 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006189 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006190 break;
6191 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006192
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 if (*p == '[')
6196 ++br_nest;
6197 else if (*p == ']')
6198 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006200
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006201 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006203 if (*p == '{')
6204 {
6205 mb_nest++;
6206 if (expr_start != NULL && *expr_start == NULL)
6207 *expr_start = p;
6208 }
6209 else if (*p == '}')
6210 {
6211 mb_nest--;
6212 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6213 *expr_end = p;
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217
6218 return p;
6219}
6220
6221/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006222 * Expands out the 'magic' {}'s in a variable/function name.
6223 * Note that this can call itself recursively, to deal with
6224 * constructs like foo{bar}{baz}{bam}
6225 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6226 * "in_start" ^
6227 * "expr_start" ^
6228 * "expr_end" ^
6229 * "in_end" ^
6230 *
6231 * Returns a new allocated string, which the caller must free.
6232 * Returns NULL for failure.
6233 */
6234 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235make_expanded_name(
6236 char_u *in_start,
6237 char_u *expr_start,
6238 char_u *expr_end,
6239 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240{
6241 char_u c1;
6242 char_u *retval = NULL;
6243 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006244
6245 if (expr_end == NULL || in_end == NULL)
6246 return NULL;
6247 *expr_start = NUL;
6248 *expr_end = NUL;
6249 c1 = *in_end;
6250 *in_end = NUL;
6251
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006252 temp_result = eval_to_string(expr_start + 1, FALSE);
6253 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006254 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006255 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6256 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006257 if (retval != NULL)
6258 {
6259 STRCPY(retval, in_start);
6260 STRCAT(retval, temp_result);
6261 STRCAT(retval, expr_end + 1);
6262 }
6263 }
6264 vim_free(temp_result);
6265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006267 *expr_start = '{';
6268 *expr_end = '}';
6269
6270 if (retval != NULL)
6271 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006272 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006273 if (expr_start != NULL)
6274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006276 temp_result = make_expanded_name(retval, expr_start,
6277 expr_end, temp_result);
6278 vim_free(retval);
6279 retval = temp_result;
6280 }
6281 }
6282
6283 return retval;
6284}
6285
6286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006291eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006293 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006294}
6295
6296/*
6297 * Return TRUE if character "c" can be used as the first character in a
6298 * variable or function name (excluding '{' and '}').
6299 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006301eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006302{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006303 return ASCII_ISALPHA(c) || c == '_';
6304}
6305
6306/*
6307 * Return TRUE if character "c" can be used as the first character of a
6308 * dictionary key.
6309 */
6310 int
6311eval_isdictc(int c)
6312{
6313 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006317 * Handle:
6318 * - expr[expr], expr[expr:expr] subscript
6319 * - ".name" lookup
6320 * - function call with Funcref variable: func(expr)
6321 * - method call: var->method()
6322 *
6323 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006324 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006325 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006327handle_subscript(
6328 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006329 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006331 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006332 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006333{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006334 int evaluate = evalarg != NULL
6335 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006336 int ret = OK;
6337 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006338 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006339 int getnext;
6340 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006341
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006342 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006343 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006344 // When at the end of the line and ".name" or "->{" or "->X" follows in
6345 // the next line then consume the line break.
6346 p = eval_next_non_blank(*arg, evalarg, &getnext);
6347 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006348 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006349 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6350 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6351 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006352 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006353 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006354 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006355 check_white = FALSE;
6356 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006357
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006358 if (rettv->v_type == VAR_ANY)
6359 {
6360 char_u *exp_name;
6361 int cc;
6362 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006363 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006364 type_T *type;
6365
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006366 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006367 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006368 if (**arg != '.')
6369 {
6370 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006371 semsg(_(e_expected_dot_after_name_str),
6372 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006373 ret = FAIL;
6374 break;
6375 }
6376 ++*arg;
6377 if (IS_WHITE_OR_NUL(**arg))
6378 {
6379 if (verbose)
6380 emsg(_(e_no_white_space_allowed_after_dot));
6381 ret = FAIL;
6382 break;
6383 }
6384
6385 // isolate the name
6386 exp_name = *arg;
6387 while (eval_isnamec(**arg))
6388 ++*arg;
6389 cc = **arg;
6390 **arg = NUL;
6391
6392 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006393 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006394 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006395
6396 if (idx < 0 && ufunc == NULL)
6397 {
6398 ret = FAIL;
6399 break;
6400 }
6401 if (idx >= 0)
6402 {
6403 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6404 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6405
6406 copy_tv(sv->sv_tv, rettv);
6407 }
6408 else
6409 {
6410 rettv->v_type = VAR_FUNC;
6411 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6412 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006413 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006414 }
6415
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006416 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6417 || rettv->v_type == VAR_PARTIAL))
6418 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006419 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006420 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6421 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006422
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006423 // Stop the expression evaluation when immediately aborting on
6424 // error, or when an interrupt occurred or an exception was thrown
6425 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006426 if (aborting())
6427 {
6428 if (ret == OK)
6429 clear_tv(rettv);
6430 ret = FAIL;
6431 }
6432 dict_unref(selfdict);
6433 selfdict = NULL;
6434 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006435 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006436 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006437 if (in_vim9script())
6438 *arg = skipwhite(p + 2);
6439 else
6440 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006441 if (ret == OK)
6442 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006443 if (VIM_ISWHITE(**arg))
6444 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006445 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006446 ret = FAIL;
6447 }
6448 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006449 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006450 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006451 else
6452 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006453 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006454 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006455 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006456 // "." is ".name" lookup when we found a dict or when evaluating and
6457 // scriptversion is at least 2, where string concatenation is "..".
6458 else if (**arg == '['
6459 || (**arg == '.' && (rettv->v_type == VAR_DICT
6460 || (!evaluate
6461 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006462 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006463 {
6464 dict_unref(selfdict);
6465 if (rettv->v_type == VAR_DICT)
6466 {
6467 selfdict = rettv->vval.v_dict;
6468 if (selfdict != NULL)
6469 ++selfdict->dv_refcount;
6470 }
6471 else
6472 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006473 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006474 {
6475 clear_tv(rettv);
6476 ret = FAIL;
6477 }
6478 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006479 else
6480 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006481 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6484 // Don't do this when "Func" is already a partial that was bound
6485 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006486 if (selfdict != NULL
6487 && (rettv->v_type == VAR_FUNC
6488 || (rettv->v_type == VAR_PARTIAL
6489 && (rettv->vval.v_partial->pt_auto
6490 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006491 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006492
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006493 dict_unref(selfdict);
6494 return ret;
6495}
6496
6497/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006498 * Make a copy of an item.
6499 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006500 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6502 * reference to an already copied list/dict can be used.
6503 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006506item_copy(
6507 typval_T *from,
6508 typval_T *to,
6509 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006510 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512{
6513 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006518 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 }
6521 ++recurse;
6522
6523 switch (from->v_type)
6524 {
6525 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006526 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 case VAR_STRING:
6528 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006529 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006530 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006531 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006532 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006533 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006534 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 copy_tv(from, to);
6536 break;
6537 case VAR_LIST:
6538 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006539 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 if (from->vval.v_list == NULL)
6541 to->vval.v_list = NULL;
6542 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006544 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 to->vval.v_list = from->vval.v_list->lv_copylist;
6546 ++to->vval.v_list->lv_refcount;
6547 }
6548 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006549 to->vval.v_list = list_copy(from->vval.v_list,
6550 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006551 if (to->vval.v_list == NULL)
6552 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006554 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006556 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 case VAR_DICT:
6558 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006559 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 if (from->vval.v_dict == NULL)
6561 to->vval.v_dict = NULL;
6562 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6566 ++to->vval.v_dict->dv_refcount;
6567 }
6568 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006569 to->vval.v_dict = dict_copy(from->vval.v_dict,
6570 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 if (to->vval.v_dict == NULL)
6572 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006574 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006575 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006577 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579 }
6580 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582}
6583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006584 void
6585echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6586{
6587 char_u *tofree;
6588 char_u numbuf[NUMBUFLEN];
6589 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6590
6591 if (*atstart)
6592 {
6593 *atstart = FALSE;
6594 // Call msg_start() after eval1(), evaluating the expression
6595 // may cause a message to appear.
6596 if (with_space)
6597 {
6598 // Mark the saved text as finishing the line, so that what
6599 // follows is displayed on a new line when scrolling back
6600 // at the more prompt.
6601 msg_sb_eol();
6602 msg_start();
6603 }
6604 }
6605 else if (with_space)
6606 msg_puts_attr(" ", echo_attr);
6607
6608 if (p != NULL)
6609 for ( ; *p != NUL && !got_int; ++p)
6610 {
6611 if (*p == '\n' || *p == '\r' || *p == TAB)
6612 {
6613 if (*p != TAB && *needclr)
6614 {
6615 // remove any text still there from the command
6616 msg_clr_eos();
6617 *needclr = FALSE;
6618 }
6619 msg_putchar_attr(*p, echo_attr);
6620 }
6621 else
6622 {
6623 if (has_mbyte)
6624 {
6625 int i = (*mb_ptr2len)(p);
6626
6627 (void)msg_outtrans_len_attr(p, i, echo_attr);
6628 p += i - 1;
6629 }
6630 else
6631 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6632 }
6633 }
6634 vim_free(tofree);
6635}
6636
Bram Moolenaare9a41262005-01-15 22:18:47 +00006637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 * ":echo expr1 ..." print each argument separated with a space, add a
6639 * newline at the end.
6640 * ":echon expr1 ..." print each argument plain.
6641 */
6642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006643ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006647 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 int needclr = TRUE;
6649 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006650 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006651 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006652 evalarg_T evalarg;
6653
Bram Moolenaare707c882020-07-01 13:07:37 +02006654 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656 if (eap->skip)
6657 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006658 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006660 // If eval1() causes an error message the text from the command may
6661 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006662 need_clr_eos = needclr;
6663
Bram Moolenaar68db9962021-05-09 23:19:22 +02006664 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006665 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
6667 /*
6668 * Report the invalid expression unless the expression evaluation
6669 * has been cancelled due to an aborting error, an interrupt, or an
6670 * exception.
6671 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006672 if (!aborting() && did_emsg == did_emsg_before
6673 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006674 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006675 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 break;
6677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006678 need_clr_eos = FALSE;
6679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006681 {
6682 if (rettv.v_type == VAR_VOID)
6683 {
6684 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6685 break;
6686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006688 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006690 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 arg = skipwhite(arg);
6692 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006693 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006694 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696 if (eap->skip)
6697 --emsg_skip;
6698 else
6699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006700 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (needclr)
6702 msg_clr_eos();
6703 if (eap->cmdidx == CMD_echo)
6704 msg_end();
6705 }
6706}
6707
6708/*
6709 * ":echohl {name}".
6710 */
6711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006712ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006714 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715}
6716
6717/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006718 * Returns the :echo attribute
6719 */
6720 int
6721get_echo_attr(void)
6722{
6723 return echo_attr;
6724}
6725
6726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 * ":execute expr1 ..." execute the result of an expression.
6728 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006729 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006731 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Each gets spaces around each argument and a newline at the end for
6733 * echo commands
6734 */
6735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006736ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 int ret = OK;
6741 char_u *p;
6742 garray_T ga;
6743 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006744 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746 ga_init2(&ga, 1, 80);
6747
6748 if (eap->skip)
6749 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006750 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006752 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006753 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if (!eap->skip)
6757 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006758 char_u buf[NUMBUFLEN];
6759
6760 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006761 {
6762 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6763 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006764 semsg(_(e_using_invalid_value_as_string_str),
6765 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006766 p = NULL;
6767 }
6768 else
6769 p = tv_get_string_buf(&rettv, buf);
6770 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006771 else
6772 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006773 if (p == NULL)
6774 {
6775 clear_tv(&rettv);
6776 ret = FAIL;
6777 break;
6778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 len = (int)STRLEN(p);
6780 if (ga_grow(&ga, len + 2) == FAIL)
6781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006782 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 ret = FAIL;
6784 break;
6785 }
6786 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 ga.ga_len += len;
6790 }
6791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 arg = skipwhite(arg);
6794 }
6795
6796 if (ret != FAIL && ga.ga_data != NULL)
6797 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006798 // use the first line of continuation lines for messages
6799 SOURCING_LNUM = start_lnum;
6800
Bram Moolenaar37fef162022-08-29 18:16:32 +01006801 if (eap->cmdidx == CMD_echomsg
6802 || eap->cmdidx == CMD_echowindow
6803 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006805 // Mark the already saved text as finishing the line, so that what
6806 // follows is displayed on a new line when scrolling back at the
6807 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006808 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006809 }
6810
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006811 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006812 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006813 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006814 out_flush();
6815 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006816 else if (eap->cmdidx == CMD_echowindow)
6817 {
6818#ifdef HAS_MESSAGE_WINDOW
6819 start_echowindow();
6820#endif
6821 msg_attr(ga.ga_data, echo_attr);
6822#ifdef HAS_MESSAGE_WINDOW
6823 end_echowindow();
6824#endif
6825 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006826 else if (eap->cmdidx == CMD_echoconsole)
6827 {
6828 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6829 ui_write((char_u *)"\r\n", 2, TRUE);
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 else if (eap->cmdidx == CMD_echoerr)
6832 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006833 int save_did_emsg = did_emsg;
6834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006835 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006836 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 if (!force_abort)
6838 did_emsg = save_did_emsg;
6839 }
6840 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006841 {
6842 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6843
6844 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6845 sticky_cmdmod_flags = cmdmod.cmod_flags
6846 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 do_cmdline((char_u *)ga.ga_data,
6848 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006849 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
6852
6853 ga_clear(&ga);
6854
6855 if (eap->skip)
6856 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006857 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858}
6859
6860/*
6861 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6862 * "arg" points to the "&" or '+' when called, to "option" when returning.
6863 * Returns NULL when no option name found. Otherwise pointer to the char
6864 * after the option name.
6865 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006866 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006867find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 char_u *p = *arg;
6870
6871 ++p;
6872 if (*p == 'g' && p[1] == ':')
6873 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006874 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 p += 2;
6876 }
6877 else if (*p == 'l' && p[1] == ':')
6878 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006879 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 p += 2;
6881 }
6882 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006883 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
6885 if (!ASCII_ISALPHA(*p))
6886 return NULL;
6887 *arg = p;
6888
6889 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006890 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 else
6892 while (ASCII_ISALPHA(*p))
6893 ++p;
6894 return p;
6895}
6896
6897/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006898 * Display script name where an item was last set.
6899 * Should only be invoked when 'verbose' is non-zero.
6900 */
6901 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006902last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006903{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006904 char_u *p;
6905
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006906 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006907 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006908 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006909 if (p != NULL)
6910 {
6911 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006912 msg_puts(_("\n\tLast set from "));
6913 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006914 if (script_ctx.sc_lnum > 0)
6915 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006916 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006917 msg_outnum((long)script_ctx.sc_lnum);
6918 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006919 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006920 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006921 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006922 }
6923}
6924
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006925#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927/*
6928 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006929 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 * "flags" can be "g" to do a global substitute.
6931 * Returns an allocated string, NULL for error.
6932 */
6933 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006934do_string_sub(
6935 char_u *str,
6936 char_u *pat,
6937 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006938 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006939 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 int sublen;
6942 regmatch_T regmatch;
6943 int i;
6944 int do_all;
6945 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006946 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 garray_T ga;
6948 char_u *ret;
6949 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006950 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006952 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006954 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 ga_init2(&ga, 1, 200);
6957
6958 do_all = (flags[0] == 'g');
6959
6960 regmatch.rm_ic = p_ic;
6961 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6962 if (regmatch.regprog != NULL)
6963 {
6964 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006965 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006968 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006969 if (regmatch.startp[0] == regmatch.endp[0])
6970 {
6971 if (zero_width == regmatch.startp[0])
6972 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006973 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006974 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006975 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6976 (size_t)i);
6977 ga.ga_len += i;
6978 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006979 continue;
6980 }
6981 zero_width = regmatch.startp[0];
6982 }
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /*
6985 * Get some space for a temporary buffer to do the substitution
6986 * into. It will contain:
6987 * - The text up to where the match is.
6988 * - The substituted text.
6989 * - The text after the match.
6990 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006991 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006992 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6994 {
6995 ga_clear(&ga);
6996 break;
6997 }
6998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006999 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 i = (int)(regmatch.startp[0] - tail);
7001 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007002 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007003 (void)vim_regsub(&regmatch, sub, expr,
7004 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7005 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007007 tail = regmatch.endp[0];
7008 if (*tail == NUL)
7009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (!do_all)
7011 break;
7012 }
7013
7014 if (ga.ga_data != NULL)
7015 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7016
Bram Moolenaar473de612013-06-08 18:19:48 +02007017 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019
7020 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7021 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007022 if (p_cpo == empty_option)
7023 p_cpo = save_cpo;
7024 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007027 // If it's still empty it was changed and restored, need to restore in
7028 // the complicated way.
7029 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007030 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007031 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 return ret;
7035}