blob: 3d6d84c2bb4196b5d65818a019db743163edd288 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010052static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020053static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010054static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
55static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
56static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000115}
116
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117#if defined(EXITFREE) || defined(PROTO)
118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100119eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000120{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200121 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100122 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200123 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200125 // autoloaded script names
126 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000127
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200128 // unreferenced lists and dicts
129 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200130
131 // functions not garbage collected
132 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000133}
134#endif
135
Bram Moolenaare6b53242020-07-01 17:28:33 +0200136 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
138{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100139 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200141 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200142 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200143 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100144 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200145 {
146 evalarg->eval_getline = eap->getline;
147 evalarg->eval_cookie = eap->cookie;
148 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200149 }
150}
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Top level evaluation function, returning a boolean.
154 * Sets "error" to TRUE if there was an error.
155 * Return TRUE or FALSE.
156 */
157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100158eval_to_bool(
159 char_u *arg,
160 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200161 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100162 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
Bram Moolenaar33570922005-01-25 22:26:29 +0000164 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200165 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200166 evalarg_T evalarg;
167
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200179 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200180 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200181 else
182 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
185 }
186 if (skip)
187 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200197eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200205 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
206
207 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 if (ret == FAIL)
209 {
210 // Report the invalid expression unless the expression evaluation has
211 // been cancelled due to an aborting error, an interrupt, or an
212 // exception, or we already gave a more specific error.
213 // Also check called_emsg for when using assert_fails().
214 if (!aborting() && did_emsg == did_emsg_before
215 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200216 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100219 return ret;
220}
221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200223 * Return whether a typval is a valid expression to pass to eval_expr_typval()
224 * or eval_expr_to_bool(). An empty string returns FALSE;
225 */
226 int
227eval_expr_valid_arg(typval_T *tv)
228{
229 return tv->v_type != VAR_UNKNOWN
230 && (tv->v_type != VAR_STRING
231 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
232}
233
234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235 * Evaluate an expression, which can be a function, partial or string.
236 * Pass arguments "argv[argc]".
237 * Return the result in "rettv" and OK or FAIL.
238 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200239 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100240eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
241{
242 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100243 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100245
246 if (expr->v_type == VAR_FUNC)
247 {
248 s = expr->vval.v_string;
249 if (s == NULL || *s == NUL)
250 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200251 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000252 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else if (expr->v_type == VAR_PARTIAL)
257 {
258 partial_T *partial = expr->vval.v_partial;
259
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200260 if (partial == NULL)
261 return FAIL;
262
Bram Moolenaar822ba242020-05-24 23:00:18 +0200263 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200264 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200266 if (call_def_function(partial->pt_func, argc, argv,
267 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 return FAIL;
269 }
270 else
271 {
272 s = partial_name(partial);
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
277 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
279 return FAIL;
280 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200282 else if (expr->v_type == VAR_INSTR)
283 {
284 return exe_typval_instr(expr, rettv);
285 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 else
287 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000288 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 if (s == NULL)
290 return FAIL;
291 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200292 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200294 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200296 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200297 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 return FAIL;
299 }
300 }
301 return OK;
302}
303
304/*
305 * Like eval_to_bool() but using a typval_T instead of a string.
306 * Works for string, funcref and partial.
307 */
308 int
309eval_expr_to_bool(typval_T *expr, int *error)
310{
311 typval_T rettv;
312 int res;
313
314 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
315 {
316 *error = TRUE;
317 return FALSE;
318 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200319 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 clear_tv(&rettv);
321 return res;
322}
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/*
325 * Top level evaluation function, returning a string. If "skip" is TRUE,
326 * only parsing to "nextcmd" is done, without reporting errors. Return
327 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
328 */
329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_to_string_skip(
331 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200332 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100333 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200337 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
Bram Moolenaar37c83712020-06-30 21:18:36 +0200339 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 if (skip)
341 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200342 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 retval = NULL;
344 else
345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100346 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000347 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349 if (skip)
350 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
353 return retval;
354}
355
356/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100357 * Initialize "evalarg" for use.
358 */
359 void
360init_evalarg(evalarg_T *evalarg)
361{
362 CLEAR_POINTER(evalarg);
363 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
364}
365
366/*
367 * If "evalarg->eval_tofree" is not NULL free it later.
368 * Caller is expected to overwrite "evalarg->eval_tofree" next.
369 */
370 static void
371free_eval_tofree_later(evalarg_T *evalarg)
372{
373 if (evalarg->eval_tofree != NULL)
374 {
375 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
376 ((char_u **)evalarg->eval_tofree_ga.ga_data)
377 [evalarg->eval_tofree_ga.ga_len++]
378 = evalarg->eval_tofree;
379 else
380 vim_free(evalarg->eval_tofree);
381 }
382}
383
384/*
385 * After using "evalarg" filled from "eap": free the memory.
386 */
387 void
388clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
389{
390 if (evalarg != NULL)
391 {
392 if (evalarg->eval_tofree != NULL)
393 {
394 if (eap != NULL)
395 {
396 // We may need to keep the original command line, e.g. for
397 // ":let" it has the variable names. But we may also need the
398 // new one, "nextcmd" points into it. Keep both.
399 vim_free(eap->cmdline_tofree);
400 eap->cmdline_tofree = *eap->cmdlinep;
401 *eap->cmdlinep = evalarg->eval_tofree;
402 }
403 else
404 vim_free(evalarg->eval_tofree);
405 evalarg->eval_tofree = NULL;
406 }
407
408 ga_clear_strings(&evalarg->eval_tofree_ga);
409 VIM_CLEAR(evalarg->eval_tofree_lambda);
410 }
411}
412
413/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000414 * Skip over an expression at "*pp".
415 * Return FAIL for an error, OK otherwise.
416 */
417 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200418skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000419{
Bram Moolenaar33570922005-01-25 22:26:29 +0000420 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000421
422 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200423 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000424}
425
426/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200427 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200428 * If in Vim9 script and line breaks are encountered, the lines are
429 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 * "arg" is advanced to just after the expression.
431 * "start" is set to the start of the expression, "end" to just after the end.
432 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433 * Return FAIL for an error, OK otherwise.
434 */
435 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436skip_expr_concatenate(
437 char_u **arg,
438 char_u **start,
439 char_u **end,
440 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200441{
442 typval_T rettv;
443 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200444 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200445 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200446 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200447 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200448 int evaluate = evalarg == NULL
449 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200451 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200452 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200453 {
454 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200455 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200456 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200457 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200458 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200459 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461
462 // Don't evaluate the expression.
463 if (evalarg != NULL)
464 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 *arg = skipwhite(*arg);
466 res = eval1(arg, &rettv, evalarg);
467 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 if (evalarg != NULL)
469 evalarg->eval_flags = save_flags;
470
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200471 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200472 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200473 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 if (evalarg->eval_ga.ga_len == 1)
475 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200476 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200477 ga_clear(gap);
478 gap->ga_itemsize = 0;
479 }
480 else
481 {
482 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200483 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200485 // Line breaks encountered, concatenate all the lines.
486 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200487 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200488
489 // free the lines only when using getsourceline()
490 if (evalarg->eval_cookie != NULL)
491 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200492 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200493 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200494 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100495 // later. Also free "eval_tofree" later if needed.
496 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200497 evalarg->eval_tofree =
498 ((char_u **)gap->ga_data)[gap->ga_len - 1];
499 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200500 ga_clear_strings(gap);
501 }
502 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200503 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200504 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505
506 // free lines that were explicitly marked for freeing
507 ga_clear_strings(freegap);
508 }
509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200510 gap->ga_itemsize = 0;
511 if (p == NULL)
512 return FAIL;
513 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200514 vim_free(evalarg->eval_tofree_lambda);
515 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200516 // Compute "end" relative to the end.
517 *end = *start + STRLEN(*start) - endoff;
518 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 }
520
521 return res;
522}
523
524/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100525 * Convert "tv" to a string.
526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
528 * Returns an allocated string (NULL when out of memory).
529 */
530 char_u *
531typval2string(typval_T *tv, int convert)
532{
533 garray_T ga;
534 char_u *retval;
535#ifdef FEAT_FLOAT
536 char_u numbuf[NUMBUFLEN];
537#endif
538
539 if (convert && tv->v_type == VAR_LIST)
540 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000541 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100542 if (tv->vval.v_list != NULL)
543 {
544 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
545 if (tv->vval.v_list->lv_len > 0)
546 ga_append(&ga, NL);
547 }
548 ga_append(&ga, NUL);
549 retval = (char_u *)ga.ga_data;
550 }
551#ifdef FEAT_FLOAT
552 else if (convert && tv->v_type == VAR_FLOAT)
553 {
554 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
555 retval = vim_strsave(numbuf);
556 }
557#endif
558 else
559 retval = vim_strsave(tv_get_string(tv));
560 return retval;
561}
562
563/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200564 * Top level evaluation function, returning a string. Does not handle line
565 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000566 * When "convert" is TRUE convert a List into a sequence of lines and convert
567 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 * Return pointer to allocated memory, or NULL for failure.
569 */
570 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100571eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573 int convert,
574 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaar33570922005-01-25 22:26:29 +0000576 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100578 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100580 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
581 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = NULL;
583 else
584 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100588 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 return retval;
591}
592
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100593 char_u *
594eval_to_string(
595 char_u *arg,
596 int convert)
597{
598 return eval_to_string_eap(arg, convert, NULL);
599}
600
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100603 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200604 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 */
606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607eval_to_string_safe(
608 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000609 int use_sandbox,
610 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200613 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200614 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200615 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar9530b582022-01-22 13:39:08 +0000617 if (!keep_script_version)
618 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200619 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000620 if (use_sandbox)
621 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100622 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200623 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200624 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000625 if (use_sandbox)
626 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100627 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200628 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200629 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200630 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 return retval;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
635 * Top level evaluation function, returning a number.
636 * Evaluates "expr" silently.
637 * Returns -1 for an error.
638 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200639 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200643 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000644 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 ++emsg_off;
647
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200648 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 retval = -1;
650 else
651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 --emsg_off;
656
657 return retval;
658}
659
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 * Top level evaluation function.
662 * Returns an allocated typval_T with the result.
663 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000664 */
665 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200666eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667{
668 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200669 evalarg_T evalarg;
670
671 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000672
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200673 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200674 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100675 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000676
Bram Moolenaar37c83712020-06-30 21:18:36 +0200677 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000678 return tv;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000682 * "*arg" points to what can be a function name in the form of "import.Name" or
683 * "Funcref". Return the name of the function. Set "tofree" to something that
684 * was allocated.
685 * If "verbose" is FALSE no errors are given.
686 * Return NULL for any failure.
687 */
688 static char_u *
689deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000690 char_u **arg,
691 char_u **tofree,
692 evalarg_T *evalarg,
693 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000694{
695 typval_T ref;
696 char_u *name = *arg;
697
698 ref.v_type = VAR_UNKNOWN;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100699 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100700 {
701 dictitem_T *v;
702
703 // If <SID>VarName was used it would not be found, try another way.
704 v = find_var_also_in_script(name, NULL, FALSE);
705 if (v == NULL)
706 return NULL;
707 copy_tv(&v->di_tv, &ref);
708 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709 if (*skipwhite(*arg) != NUL)
710 {
711 if (verbose)
712 semsg(_(e_trailing_characters_str), *arg);
713 name = NULL;
714 }
715 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
716 {
717 name = ref.vval.v_string;
718 ref.vval.v_string = NULL;
719 *tofree = name;
720 }
721 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
722 {
723 if (ref.vval.v_partial->pt_argc > 0
724 || ref.vval.v_partial->pt_dict != NULL)
725 {
726 if (verbose)
727 emsg(_(e_cannot_use_partial_here));
728 name = NULL;
729 }
730 else
731 {
732 name = vim_strsave(partial_name(ref.vval.v_partial));
733 *tofree = name;
734 }
735 }
736 else
737 {
738 if (verbose)
739 semsg(_(e_not_callable_type_str), name);
740 name = NULL;
741 }
742 clear_tv(&ref);
743 return name;
744}
745
746/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100747 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200748 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
749 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000750 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753call_vim_function(
754 char_u *func,
755 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200756 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200757 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000759 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200760 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000761 char_u *arg;
762 char_u *name;
763 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000764 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200767 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000768 funcexe.fe_firstline = curwin->w_cursor.lnum;
769 funcexe.fe_lastline = curwin->w_cursor.lnum;
770 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000771
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000772 // The name might be "import.Func" or "Funcref". We don't know, we need to
773 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000774 // autoload script has errors. Guess that when there is a dot in the name
775 // showing errors is the right choice.
776 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000777 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000778 if (ignore_errors)
779 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000780 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000781 if (ignore_errors)
782 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000783 if (name == NULL)
784 name = func;
785
786 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
787
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000788 if (ret == FAIL)
789 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000791
792 return ret;
793}
794
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100795/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100796 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000797 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
798 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000799 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000800 */
801 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802call_func_retstr(
803 char_u *func,
804 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200805 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806{
807 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000808 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000809
Bram Moolenaarded27a12018-08-01 19:06:03 +0200810 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000811 return NULL;
812
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100813 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return retval;
816}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000817
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000818/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100819 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000820 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000821 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000822 */
823 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100824call_func_retlist(
825 char_u *func,
826 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200827 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828{
829 typval_T rettv;
830
Bram Moolenaarded27a12018-08-01 19:06:03 +0200831 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 return NULL;
833
834 if (rettv.v_type != VAR_LIST)
835 {
836 clear_tv(&rettv);
837 return NULL;
838 }
839
840 return rettv.vval.v_list;
841}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaare70dd112022-01-21 16:31:11 +0000843#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100845 * Evaluate "arg", which is 'foldexpr'.
846 * Note: caller must set "curwin" to match "arg".
847 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
848 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 */
850 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000851eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000853 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000854 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200855 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000857 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000858 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
859 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861 arg = wp->w_p_fde;
862 current_sctx = wp->w_p_script_ctx[WV_FDE];
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100867 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200869 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 retval = 0;
871 else
872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100873 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (tv.v_type == VAR_NUMBER)
875 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000876 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 retval = 0;
878 else
879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 // If the result is a string, check if there is a non-digit before
881 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (!VIM_ISDIGIT(*s) && *s != '-')
884 *cp = *s++;
885 retval = atol((char *)s);
886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000890 if (use_sandbox)
891 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100892 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200893 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000894 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200896 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898#endif
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 * Get an lval: variable, Dict item or List item that can be assigned a value
902 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
903 * "name.key", "name.key[expr]" etc.
904 * Indexing only works if "name" is an existing List or Dictionary.
905 * "name" points to the start of the name.
906 * If "rettv" is not NULL it points to the value to be assigned.
907 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
908 * wrong; must end in space or cmd separator.
909 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100910 * flags:
911 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100912 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100913 * GLV_NO_AUTOLOAD: do not use script autoloading
914 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920get_lval(
921 char_u *name,
922 typval_T *rettv,
923 lval_T *lp,
924 int unlet,
925 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100926 int flags, // GLV_ values
927 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 char_u *expr_start, *expr_end;
931 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000932 dictitem_T *v;
933 typval_T var1;
934 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100938 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100939 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100940 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000941 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200944 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100946 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100948 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200950 lp->ll_name_end = find_name_end(name, NULL, NULL,
951 FNE_INCL_BR | fne_flags);
952 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 }
954
Bram Moolenaara749a422022-02-12 19:52:25 +0000955 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000956 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000957 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
958 {
959 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
960 return NULL;
961 }
962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000964 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (expr_start != NULL)
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100969 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 && *p != '[' && *p != '.')
971 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000972 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 return NULL;
974 }
975
976 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
977 if (lp->ll_exp_name == NULL)
978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // Report an invalid expression in braces, unless the
980 // expression evaluation has been cancelled due to an
981 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (!aborting() && !quiet)
983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000984 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000985 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
987 }
988 }
989 lp->ll_name = lp->ll_exp_name;
990 }
991 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 lp->ll_name = name;
994
Bram Moolenaar4525a572022-02-13 11:57:33 +0000995 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200997 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +0000998 // However, "g:[key]" is indexing a dictionary.
999 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001000 {
1001 --p;
1002 lp->ll_name_end = p;
1003 }
1004 if (*p == ':')
1005 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001006 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001007
1008 if (tp == p + 1 && !quiet)
1009 {
1010 semsg(_(e_white_space_required_after_str_str), ":", p);
1011 return NULL;
1012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001014 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001015 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001016 semsg(_(e_using_type_not_in_script_context_str), p);
1017 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001018 }
1019
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001020 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001021 lp->ll_type = parse_type(&tp,
1022 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1023 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001024 if (lp->ll_type == NULL && !quiet)
1025 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001026 lp->ll_name_end = tp;
1027 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 }
1029 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001030 if (lp->ll_name == NULL)
1031 return p;
1032
Bram Moolenaar62aec932022-01-29 21:45:34 +00001033 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001034 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001035 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001036
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001037 if (import != NULL)
1038 {
1039 ufunc_T *ufunc;
1040 type_T *type;
1041
Bram Moolenaar753885b2022-08-24 16:30:36 +01001042 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001043 lp->ll_sid = import->imp_sid;
1044 lp->ll_name = skipwhite(p + 1);
1045 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1046 lp->ll_name_end = p;
1047
1048 // check the item is exported
1049 cc = *p;
1050 *p = NUL;
1051 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001052 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001053 {
1054 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001055 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001056 }
1057 *p = cc;
1058 }
1059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001061 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001062 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 return p;
1064
Bram Moolenaar4525a572022-02-13 11:57:33 +00001065 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001066 {
1067 // using local variable
1068 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001069 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001070 }
1071 else
1072 {
1073 cc = *p;
1074 *p = NUL;
1075 // When we would write to the variable pass &ht and prevent autoload.
1076 writing = !(flags & GLV_READ_ONLY);
1077 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001078 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001079 if (v == NULL && !quiet)
1080 semsg(_(e_undefined_variable_str), lp->ll_name);
1081 *p = cc;
1082 if (v == NULL)
1083 return NULL;
1084 lp->ll_tv = &v->di_tv;
1085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086
Bram Moolenaar4525a572022-02-13 11:57:33 +00001087 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001088 {
1089 if (!quiet)
1090 semsg(_(e_variable_already_declared), lp->ll_name);
1091 return NULL;
1092 }
1093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 /*
1095 * Loop until no more [idx] or .key is following.
1096 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001097 var1.v_type = VAR_UNKNOWN;
1098 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001099 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001101 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1102 {
1103 if (!quiet)
1104 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1105 return NULL;
1106 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001107 if (lp->ll_tv->v_type != VAR_LIST
1108 && lp->ll_tv->v_type != VAR_DICT
1109 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001112 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001115
1116 // a NULL list/blob works like an empty list/blob, allocate one now.
1117 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1118 rettv_list_alloc(lp->ll_tv);
1119 else if (lp->ll_tv->v_type == VAR_BLOB
1120 && lp->ll_tv->vval.v_blob == NULL)
1121 rettv_blob_alloc(lp->ll_tv);
1122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001126 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001129
Bram Moolenaar4525a572022-02-13 11:57:33 +00001130 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001131 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001132 && lp->ll_tv == &v->di_tv
1133 && ht != NULL && ht == get_script_local_ht())
1134 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001135 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001136
1137 // Vim9 script local variable: get the type
1138 if (sv != NULL)
1139 lp->ll_valtype = sv->sv_type;
1140 }
1141
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 len = -1;
1143 if (*p == '.')
1144 {
1145 key = p + 1;
1146 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1147 ;
1148 if (len == 0)
1149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001151 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 }
1154 p = key + len;
1155 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 else
1157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001159 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 if (*p == ':')
1161 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001162 else
1163 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001165 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001167 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001169 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001170 clear_tv(&var1);
1171 return NULL;
1172 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001173 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 }
1175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001176 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 if (*p == ':')
1178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001182 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001183 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001185 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 if (rettv != NULL
1187 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001190 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001193 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001196 }
1197 p = skipwhite(p + 1);
1198 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 else
1201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001203 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001204 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001206 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001209 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001213 clear_tv(&var2);
1214 return NULL;
1215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001221
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001223 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001225 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
1227 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 }
1230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 ++p;
1233 }
1234
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 {
1237 if (len == -1)
1238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // "[key]": get key from "var1"
1240 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001241 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 }
1246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001247 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001248
1249 // a NULL dict is equivalent with an empty dict
1250 if (lp->ll_tv->vval.v_dict == NULL)
1251 {
1252 lp->ll_tv->vval.v_dict = dict_alloc();
1253 if (lp->ll_tv->vval.v_dict == NULL)
1254 {
1255 clear_tv(&var1);
1256 return NULL;
1257 }
1258 ++lp->ll_tv->vval.v_dict->dv_refcount;
1259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001261
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001262 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 // When assigning to a scope dictionary check that a function and
1265 // variable name is valid (only variable name unless it is l: or
1266 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001267 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001268 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001269 int prevval;
1270 int wrong;
1271
1272 if (len != -1)
1273 {
1274 prevval = key[len];
1275 key[len] = NUL;
1276 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001277 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001279 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1280 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001281 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001282 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001283 if (len != -1)
1284 key[len] = prevval;
1285 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001286 {
1287 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001288 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001289 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001290 }
1291
Bram Moolenaar10c65862020-10-08 21:16:42 +02001292 if (lp->ll_valtype != NULL)
1293 // use the type of the member
1294 lp->ll_valtype = lp->ll_valtype->tt_member;
1295
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001298 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001299 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001300 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001301 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001302 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001303 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001304 return NULL;
1305 }
1306
Bram Moolenaar31b81602019-02-10 22:14:27 +01001307 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001311 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001314 }
1315 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001316 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001318 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001319 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 p = NULL;
1322 break;
1323 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001325 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001326 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1327 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001328 {
1329 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001330 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001331 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001332
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 else if (lp->ll_tv->v_type == VAR_BLOB)
1337 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001338 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1339
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001340 /*
1341 * Get the number and item for the only or first index of the List.
1342 */
1343 if (empty1)
1344 lp->ll_n1 = 0;
1345 else
1346 // is number or string
1347 lp->ll_n1 = (long)tv_get_number(&var1);
1348 clear_tv(&var1);
1349
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001350 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001351 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001353 return NULL;
1354 }
1355 if (lp->ll_range && !lp->ll_empty2)
1356 {
1357 lp->ll_n2 = (long)tv_get_number(&var2);
1358 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001359 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1360 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001361 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001362 }
1363 lp->ll_blob = lp->ll_tv->vval.v_blob;
1364 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001365 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001367 else
1368 {
1369 /*
1370 * Get the number and item for the only or first index of the List.
1371 */
1372 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001374 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001375 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001376 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001377 clear_tv(&var1);
1378
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001379 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001381 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1382 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001383 if (lp->ll_li == NULL)
1384 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001385 clear_tv(&var2);
1386 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001387 }
1388
Bram Moolenaar10c65862020-10-08 21:16:42 +02001389 if (lp->ll_valtype != NULL)
1390 // use the type of the member
1391 lp->ll_valtype = lp->ll_valtype->tt_member;
1392
Bram Moolenaar8c711452005-01-14 21:53:12 +00001393 /*
1394 * May need to find the item or absolute index for the second
1395 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 * When no index given: "lp->ll_empty2" is TRUE.
1397 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001398 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001401 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001404 if (check_range_index_two(lp->ll_list,
1405 &lp->ll_n1, lp->ll_li,
1406 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408 }
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 }
1413
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001414 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 return p;
1417}
1418
1419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001423clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424{
1425 vim_free(lp->ll_exp_name);
1426 vim_free(lp->ll_newkey);
1427}
1428
1429/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001430 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001432 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1433 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001436set_var_lval(
1437 lval_T *lp,
1438 char_u *endp,
1439 typval_T *rettv,
1440 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001441 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1442 char_u *op,
1443 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444{
1445 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001446 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447
1448 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001450 cc = *endp;
1451 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001452 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1453 return;
1454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 if (lp->ll_blob != NULL)
1456 {
1457 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001458
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 if (op != NULL && *op != '=')
1460 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001461 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001462 return;
1463 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001464 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001465 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466
1467 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1468 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001469 if (lp->ll_empty2)
1470 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1471
Bram Moolenaar68452172021-04-12 21:21:02 +02001472 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1473 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 }
1476 else
1477 {
1478 val = (int)tv_get_number_chk(rettv, &error);
1479 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001480 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 }
1482 }
1483 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001485 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001487 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1488 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001489 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001490 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001491 *endp = cc;
1492 return;
1493 }
1494
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001496 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001497 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001498 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001499 {
1500 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001501 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1502 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001503 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001504 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001505 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001506 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001509 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001510 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001511 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1512 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001513 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001514 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001515 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001516 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001517 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001518 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001519 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001520 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001521 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001522 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001523 else if (lp->ll_range)
1524 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001525 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1526 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001527 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001528 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001529 return;
1530 }
1531
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001532 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1533 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001534 }
1535 else
1536 {
1537 /*
1538 * Assign to a List or Dictionary item.
1539 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001540 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1541 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001542 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001544 return;
1545 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001546
1547 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001548 && check_typval_arg_type(lp->ll_valtype, rettv,
1549 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001550 return;
1551
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001552 if (lp->ll_newkey != NULL)
1553 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001554 if (op != NULL && *op != '=')
1555 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001556 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001557 return;
1558 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001559 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1560 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001561 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001563 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001564 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001565 if (di == NULL)
1566 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001567 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1568 {
1569 vim_free(di);
1570 return;
1571 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001572 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001573 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001574 else if (op != NULL && *op != '=')
1575 {
1576 tv_op(lp->ll_tv, rettv, op);
1577 return;
1578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001579 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001582 /*
1583 * Assign the value to the variable or list item.
1584 */
1585 if (copy)
1586 copy_tv(rettv, lp->ll_tv);
1587 else
1588 {
1589 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001590 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001591 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001592 }
1593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594}
1595
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1598 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 * Returns OK or FAIL.
1600 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001604 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001605 char_u numbuf[NUMBUFLEN];
1606 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001609 // Can't do anything with a Funcref or Dict on the right.
1610 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001611 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001612 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1613 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 {
1615 switch (tv1->v_type)
1616 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001617 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001618 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001620 case VAR_DICT:
1621 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001622 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001623 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001624 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001625 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001627 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 break;
1629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630 case VAR_BLOB:
1631 if (*op != '+' || tv2->v_type != VAR_BLOB)
1632 break;
1633 // BLOB += BLOB
1634 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1635 {
1636 blob_T *b1 = tv1->vval.v_blob;
1637 blob_T *b2 = tv2->vval.v_blob;
1638 int i, len = blob_len(b2);
1639 for (i = 0; i < len; i++)
1640 ga_append(&b1->bv_ga, blob_get(b2, i));
1641 }
1642 return OK;
1643
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 case VAR_LIST:
1645 if (*op != '+' || tv2->v_type != VAR_LIST)
1646 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001648 if (tv2->vval.v_list != NULL)
1649 {
1650 if (tv1->vval.v_list == NULL)
1651 {
1652 tv1->vval.v_list = tv2->vval.v_list;
1653 ++tv1->vval.v_list->lv_refcount;
1654 }
1655 else
1656 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1657 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 return OK;
1659
1660 case VAR_NUMBER:
1661 case VAR_STRING:
1662 if (tv2->v_type == VAR_LIST)
1663 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001664 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001666 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001667 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001668#ifdef FEAT_FLOAT
1669 if (tv2->v_type == VAR_FLOAT)
1670 {
1671 float_T f = n;
1672
Bram Moolenaarff697e62019-02-12 22:28:33 +01001673 if (*op == '%')
1674 break;
1675 switch (*op)
1676 {
1677 case '+': f += tv2->vval.v_float; break;
1678 case '-': f -= tv2->vval.v_float; break;
1679 case '*': f *= tv2->vval.v_float; break;
1680 case '/': f /= tv2->vval.v_float; break;
1681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682 clear_tv(tv1);
1683 tv1->v_type = VAR_FLOAT;
1684 tv1->vval.v_float = f;
1685 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001687#endif
1688 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001689 switch (*op)
1690 {
1691 case '+': n += tv_get_number(tv2); break;
1692 case '-': n -= tv_get_number(tv2); break;
1693 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001694 case '/': n = num_divide(n, tv_get_number(tv2),
1695 &failed); break;
1696 case '%': n = num_modulus(n, tv_get_number(tv2),
1697 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001698 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001699 clear_tv(tv1);
1700 tv1->v_type = VAR_NUMBER;
1701 tv1->vval.v_number = n;
1702 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001703 }
1704 else
1705 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001706 if (tv2->v_type == VAR_FLOAT)
1707 break;
1708
Bram Moolenaarff697e62019-02-12 22:28:33 +01001709 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001710 s = tv_get_string(tv1);
1711 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 clear_tv(tv1);
1713 tv1->v_type = VAR_STRING;
1714 tv1->vval.v_string = s;
1715 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001716 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001718 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001719#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001720 {
1721 float_T f;
1722
Bram Moolenaarff697e62019-02-12 22:28:33 +01001723 if (*op == '%' || *op == '.'
1724 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001725 && tv2->v_type != VAR_NUMBER
1726 && tv2->v_type != VAR_STRING))
1727 break;
1728 if (tv2->v_type == VAR_FLOAT)
1729 f = tv2->vval.v_float;
1730 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001731 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001732 switch (*op)
1733 {
1734 case '+': tv1->vval.v_float += f; break;
1735 case '-': tv1->vval.v_float -= f; break;
1736 case '*': tv1->vval.v_float *= f; break;
1737 case '/': tv1->vval.v_float /= f; break;
1738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001739 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001740#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001741 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001742 }
1743 }
1744
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001745 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 return FAIL;
1747}
1748
1749/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 * Evaluate the expression used in a ":for var in expr" command.
1751 * "arg" points to "var".
1752 * Set "*errp" to TRUE for an error, FALSE otherwise;
1753 * Return a pointer that holds the info. Null when there is an error.
1754 */
1755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756eval_for_line(
1757 char_u *arg,
1758 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001759 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001760 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
Bram Moolenaar33570922005-01-25 22:26:29 +00001762 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001763 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 typval_T tv;
1766 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001767 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001771 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 if (fi == NULL)
1773 return NULL;
1774
Bram Moolenaar404557e2021-07-05 21:41:48 +02001775 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1776 &fi->fi_semicolon, FALSE);
1777 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 return fi;
1779
Bram Moolenaar404557e2021-07-05 21:41:48 +02001780 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 if (expr[0] != 'i' || expr[1] != 'n'
1782 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001784 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1785 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1786 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001787 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 return fi;
1789 }
1790
1791 if (skip)
1792 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001793 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1794 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 {
1796 *errp = FALSE;
1797 if (!skip)
1798 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001799 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001800 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001801 l = tv.vval.v_list;
1802 if (l == NULL)
1803 {
1804 // a null list is like an empty list: do nothing
1805 clear_tv(&tv);
1806 }
1807 else
1808 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001810 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001812 // No need to increment the refcount, it's already set for
1813 // the list being used in "tv".
1814 fi->fi_list = l;
1815 list_add_watch(l, &fi->fi_lw);
1816 fi->fi_lw.lw_item = l->lv_first;
1817 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001818 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001819 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001820 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001821 fi->fi_bi = 0;
1822 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001823 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001824 typval_T btv;
1825
1826 // Make a copy, so that the iteration still works when the
1827 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001829 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001831 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001832 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001833 else if (tv.v_type == VAR_STRING)
1834 {
1835 fi->fi_byte_idx = 0;
1836 fi->fi_string = tv.vval.v_string;
1837 tv.vval.v_string = NULL;
1838 if (fi->fi_string == NULL)
1839 fi->fi_string = vim_strsave((char_u *)"");
1840 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 else
1842 {
Sean Dewar80d73952021-08-04 19:25:54 +02001843 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001844 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 }
1846 }
1847 }
1848 if (skip)
1849 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001850 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851
1852 return fi;
1853}
1854
1855/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001856 * Used when looping over a :for line, skip the "in expr" part.
1857 */
1858 void
1859skip_for_lines(void *fi_void, evalarg_T *evalarg)
1860{
1861 forinfo_T *fi = (forinfo_T *)fi_void;
1862 int i;
1863
1864 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001865 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001866}
1867
1868/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 * Use the first item in a ":for" list. Advance to the next.
1870 * Assign the values to the variable (list). "arg" points to the first one.
1871 * Return TRUE when a valid item was found, FALSE when at end of list or
1872 * something wrong.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001877 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001879 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001880 ? (ASSIGN_FINAL
1881 // first round: error if variable exists
1882 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1883 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001884 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001886 int skip_assign = in_vim9script() && arg[0] == '_'
1887 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001889 if (fi->fi_blob != NULL)
1890 {
1891 typval_T tv;
1892
1893 if (fi->fi_bi >= blob_len(fi->fi_blob))
1894 return FALSE;
1895 tv.v_type = VAR_NUMBER;
1896 tv.v_lock = VAR_FIXED;
1897 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1898 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001899 if (skip_assign)
1900 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001901 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001902 fi->fi_varcount, flag, NULL) == OK;
1903 }
1904
1905 if (fi->fi_string != NULL)
1906 {
1907 typval_T tv;
1908 int len;
1909
1910 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1911 if (len == 0)
1912 return FALSE;
1913 tv.v_type = VAR_STRING;
1914 tv.v_lock = VAR_FIXED;
1915 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1916 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001917 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001918 if (skip_assign)
1919 result = TRUE;
1920 else
1921 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001922 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001923 vim_free(tv.vval.v_string);
1924 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001925 }
1926
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 item = fi->fi_lw.lw_item;
1928 if (item == NULL)
1929 result = FALSE;
1930 else
1931 {
1932 fi->fi_lw.lw_item = item->li_next;
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, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001938 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 }
1940 return result;
1941}
1942
1943/*
1944 * Free the structure used to store info used by ":for".
1945 */
1946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001947free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948{
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001951 if (fi == NULL)
1952 return;
1953 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001956 list_unref(fi->fi_list);
1957 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001958 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001959 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001960 else
1961 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 vim_free(fi);
1963}
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001966set_context_for_expression(
1967 expand_T *xp,
1968 char_u *arg,
1969 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001971 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001975 if (cmdidx == CMD_let || cmdidx == CMD_var
1976 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 {
1984 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001985 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001986 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 break;
1988 }
1989 return;
1990 }
1991 }
1992 else
1993 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1994 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 while ((xp->xp_pattern = vim_strpbrk(arg,
1996 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1997 {
1998 c = *xp->xp_pattern;
1999 if (c == '&')
2000 {
2001 c = xp->xp_pattern[1];
2002 if (c == '&')
2003 {
2004 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002005 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002010 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2011 xp->xp_pattern += 2;
2012
2013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 else if (c == '$')
2016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 xp->xp_context = EXPAND_ENV_VARS;
2019 }
2020 else if (c == '=')
2021 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002022 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 xp->xp_context = EXPAND_EXPRESSION;
2024 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002025 else if (c == '#'
2026 && xp->xp_context == EXPAND_EXPRESSION)
2027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002029 break;
2030 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002031 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 && xp->xp_context == EXPAND_FUNCTIONS
2033 && vim_strchr(xp->xp_pattern, '(') == NULL)
2034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002035 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 break;
2037 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002038 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2043 if (c == '\\' && xp->xp_pattern[1] != NUL)
2044 ++xp->xp_pattern;
2045 xp->xp_context = EXPAND_NOTHING;
2046 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2051 /* skip */ ;
2052 xp->xp_context = EXPAND_NOTHING;
2053 }
2054 else if (c == '|')
2055 {
2056 if (xp->xp_pattern[1] == '|')
2057 {
2058 ++xp->xp_pattern;
2059 xp->xp_context = EXPAND_EXPRESSION;
2060 }
2061 else
2062 xp->xp_context = EXPAND_COMMANDS;
2063 }
2064 else
2065 xp->xp_context = EXPAND_EXPRESSION;
2066 }
2067 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002068 // Doesn't look like something valid, expand as an expression
2069 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 arg = xp->xp_pattern;
2072 if (*arg != NUL)
2073 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2074 /* skip */ ;
2075 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002076
2077 // ":exe one two" completes "two"
2078 if ((cmdidx == CMD_execute
2079 || cmdidx == CMD_echo
2080 || cmdidx == CMD_echon
2081 || cmdidx == CMD_echomsg)
2082 && xp->xp_context == EXPAND_EXPRESSION)
2083 {
2084 for (;;)
2085 {
2086 char_u *n = skiptowhite(arg);
2087
2088 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2089 break;
2090 arg = skipwhite(n);
2091 }
2092 }
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 xp->xp_pattern = arg;
2095}
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002098 * Return TRUE if "pat" matches "text".
2099 * Does not use 'cpo' and always uses 'magic'.
2100 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002101 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002102pattern_match(char_u *pat, char_u *text, int ic)
2103{
2104 int matches = FALSE;
2105 char_u *save_cpo;
2106 regmatch_T regmatch;
2107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002108 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002109 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002110 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002111 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2112 if (regmatch.regprog != NULL)
2113 {
2114 regmatch.rm_ic = ic;
2115 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2116 vim_regfree(regmatch.regprog);
2117 }
2118 p_cpo = save_cpo;
2119 return matches;
2120}
2121
2122/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002123 * Handle a name followed by "(". Both for just "name(arg)" and for
2124 * "expr->name(arg)".
2125 * Returns OK or FAIL.
2126 */
2127 static int
2128eval_func(
2129 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002130 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002131 char_u *name,
2132 int name_len,
2133 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002134 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002135 typval_T *basetv) // "expr" for "expr->name(arg)"
2136{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002137 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002138 char_u *s = name;
2139 int len = name_len;
2140 partial_T *partial;
2141 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002142 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002143 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002144
2145 if (!evaluate)
2146 check_vars(s, len);
2147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002148 // If "s" is the name of a variable of type VAR_FUNC
2149 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002150 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002151 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002153 // Need to make a copy, in case evaluating the arguments makes
2154 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002155 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002156 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002157 ret = FAIL;
2158 else
2159 {
2160 funcexe_T funcexe;
2161
2162 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002163 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002164 funcexe.fe_firstline = curwin->w_cursor.lnum;
2165 funcexe.fe_lastline = curwin->w_cursor.lnum;
2166 funcexe.fe_evaluate = evaluate;
2167 funcexe.fe_partial = partial;
2168 funcexe.fe_basetv = basetv;
2169 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002170 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002171 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002172 }
2173 vim_free(s);
2174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002175 // If evaluate is FALSE rettv->v_type was not set in
2176 // get_func_tv, but it's needed in handle_subscript() to parse
2177 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002178 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2179 {
2180 rettv->vval.v_string = NULL;
2181 rettv->v_type = VAR_FUNC;
2182 }
2183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002184 // Stop the expression evaluation when immediately
2185 // aborting on error, or when an interrupt occurred or
2186 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002187 if (evaluate && aborting())
2188 {
2189 if (ret == OK)
2190 clear_tv(rettv);
2191 ret = FAIL;
2192 }
2193 return ret;
2194}
2195
2196/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002197 * After a NL, skip over empty lines and comment-only lines.
2198 */
2199 static char_u *
2200newline_skip_comments(char_u *arg)
2201{
2202 char_u *p = arg + 1;
2203
2204 for (;;)
2205 {
2206 p = skipwhite(p);
2207
2208 if (*p == NUL)
2209 break;
2210 if (vim9_comment_start(p))
2211 {
2212 char_u *nl = vim_strchr(p, NL);
2213
2214 if (nl == NULL)
2215 break;
2216 p = nl;
2217 }
2218 if (*p != NL)
2219 break;
2220 ++p; // skip another NL
2221 }
2222 return p;
2223}
2224
2225/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002226 * Get the next line source line without advancing. But do skip over comment
2227 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002228 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002229 */
2230 static char_u *
2231getline_peek_skip_comments(evalarg_T *evalarg)
2232{
2233 for (;;)
2234 {
2235 char_u *next = getline_peek(evalarg->eval_getline,
2236 evalarg->eval_cookie);
2237 char_u *p;
2238
2239 if (next == NULL)
2240 break;
2241 p = skipwhite(next);
2242 if (*p != NUL && !vim9_comment_start(p))
2243 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002244 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002245 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002246 }
2247 return NULL;
2248}
2249
2250/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002251 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2252 * comment) and there is a next line, return the next line (skipping blanks)
2253 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002254 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2255 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 * "arg" must point somewhere inside a line, not at the start.
2257 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002258 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2260{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002261 char_u *p = skipwhite(arg);
2262
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002264 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002266 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2267 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002268 && (*p == NUL || *p == NL
2269 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002270 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002271 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002272
Bram Moolenaare442d592022-05-05 12:20:28 +01002273 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002274 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002275 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002276 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002277 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002278 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279
Bram Moolenaar9d489562020-07-30 20:08:50 +02002280 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002281 {
2282 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002283 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 }
2285 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002286 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287}
2288
2289/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002290 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002291 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002292 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002293 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002294eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002295{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002296 garray_T *gap = &evalarg->eval_ga;
2297 char_u *line;
2298
Bram Moolenaar39be4982022-05-06 21:51:50 +01002299 if (arg != NULL)
2300 {
2301 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002302 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002303 // Truncate before a trailing comment, so that concatenating the lines
2304 // won't turn the rest into a comment.
2305 if (*skipwhite(arg) == '#')
2306 *arg = NUL;
2307 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002308
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002309 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002310 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2311 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002312 else
2313 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002314 if (line == NULL)
2315 return NULL;
2316
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002317 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002318 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2319 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002320 char_u *p = skipwhite(line);
2321
2322 // Going to concatenate the lines after parsing. For an empty or
2323 // comment line use an empty string.
2324 if (*p == NUL || vim9_comment_start(p))
2325 {
2326 vim_free(line);
2327 line = vim_strsave((char_u *)"");
2328 }
2329
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002330 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2331 ++gap->ga_len;
2332 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002333 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002334 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002335 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002336 evalarg->eval_tofree = line;
2337 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002338
2339 // Advanced to the next line, "arg" no longer points into the previous
2340 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002341 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002342 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002343}
2344
Bram Moolenaare6b53242020-07-01 17:28:33 +02002345/*
2346 * Call eval_next_non_blank() and get the next line if needed.
2347 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002348 char_u *
2349skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2350{
2351 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002352 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002353
Bram Moolenaar962d7212020-07-04 14:15:00 +02002354 if (evalarg == NULL)
2355 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002356 eval_next_non_blank(p, evalarg, &getnext);
2357 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002358 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002359 return p;
2360}
2361
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2366 */
2367
2368/*
2369 * Handle zero level expression.
2370 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002372 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002373 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * Return OK or FAIL.
2375 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377eval0(
2378 char_u *arg,
2379 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002380 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002383 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2384}
2385
2386/*
2387 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2388 * expression and don't check what comes after the expression.
2389 */
2390 int
2391eval0_retarg(
2392 char_u *arg,
2393 typval_T *rettv,
2394 exarg_T *eap,
2395 evalarg_T *evalarg,
2396 char_u **retarg)
2397{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int ret;
2399 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002400 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002401 int did_emsg_before = did_emsg;
2402 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002403 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002404 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002405 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002409
Bram Moolenaar79481362022-06-27 20:15:10 +01002410 if (ret != FAIL)
2411 {
2412 expr_end = p;
2413 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002414
Bram Moolenaar79481362022-06-27 20:15:10 +01002415 // In Vim9 script a command block is not split at NL characters for
2416 // commands using an expression argument. Skip over a '#' comment to
2417 // check for a following NL. Require white space before the '#'.
2418 if (in_vim9script() && p > expr_end && retarg == NULL)
2419 while (*p == '#')
2420 {
2421 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002422
Bram Moolenaar79481362022-06-27 20:15:10 +01002423 if (nl == NULL)
2424 break;
2425 p = skipwhite(nl + 1);
2426 if (eap != NULL && *p != NUL)
2427 eap->nextcmd = p;
2428 check_for_end = FALSE;
2429 }
2430
2431 if (check_for_end)
2432 end_error = !ends_excmd2(arg, p);
2433 }
2434
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002435 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /*
2440 * Report the invalid expression unless the expression evaluation has
2441 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002442 * exception, or we already gave a more specific error.
2443 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002445 if (!aborting()
2446 && did_emsg == did_emsg_before
2447 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002448 && (flags & EVAL_CONSTANT) == 0
2449 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002450 {
2451 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002452 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002453 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002454 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002455 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002456
2457 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002458 // a next command to avoid more errors, unless "|" is following, which
2459 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002460 if (eap != NULL && p != NULL
2461 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002462 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002465
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002466 if (retarg != NULL)
2467 *retarg = p;
2468 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002469 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return ret;
2472}
2473
2474/*
2475 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002476 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002477 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 *
2479 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002480 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002482 * Note: "rettv.v_lock" is not set.
2483 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * Return OK or FAIL.
2485 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002486 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002489 char_u *p;
2490 int getnext;
2491
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002492 CLEAR_POINTER(rettv);
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 /*
2495 * Get the first variable.
2496 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return FAIL;
2499
Bram Moolenaar793648f2020-06-26 21:28:25 +02002500 p = eval_next_non_blank(*arg, evalarg, &getnext);
2501 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002503 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002504 int result;
2505 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506 evalarg_T *evalarg_used = evalarg;
2507 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002508 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002509 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002510 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511
2512 if (evalarg == NULL)
2513 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002514 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002515 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002516 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002517 orig_flags = evalarg_used->eval_flags;
2518 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002519
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002521 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002522 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002523 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002524 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002525 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002526 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002527 clear_tv(rettv);
2528 return FAIL;
2529 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002530 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002531 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002536 int error = FALSE;
2537
Bram Moolenaar13106602020-10-04 16:06:05 +02002538 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002539 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002540 else if (vim9script)
2541 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002542 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002544 if (error || !op_falsy || !result)
2545 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 if (error)
2547 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
2550 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002551 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002553 if (op_falsy)
2554 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002555 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002556 {
mityu4ac198c2021-05-28 17:52:40 +02002557 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002558 clear_tv(rettv);
2559 return FAIL;
2560 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002562 evalarg_used->eval_flags = (op_falsy ? !result : result)
2563 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2564 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002565 {
2566 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002568 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002569 if (!op_falsy || !result)
2570 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002572 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002574 /*
2575 * Check for the ":".
2576 */
2577 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2578 if (*p != ':')
2579 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002580 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002581 if (evaluate && result)
2582 clear_tv(rettv);
2583 evalarg_used->eval_flags = orig_flags;
2584 return FAIL;
2585 }
2586 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002587 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002588 else
2589 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002590 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002591 {
2592 error_white_both(p, 1);
2593 clear_tv(rettv);
2594 evalarg_used->eval_flags = orig_flags;
2595 return FAIL;
2596 }
2597 *arg = p;
2598 }
2599
2600 /*
2601 * Get the third variable. Recursive!
2602 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002603 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002604 {
mityu4ac198c2021-05-28 17:52:40 +02002605 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002606 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002607 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002608 return FAIL;
2609 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002610 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2611 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002612 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002613 if (eval1(arg, &var2, evalarg_used) == FAIL)
2614 {
2615 if (evaluate && result)
2616 clear_tv(rettv);
2617 evalarg_used->eval_flags = orig_flags;
2618 return FAIL;
2619 }
2620 if (evaluate && !result)
2621 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623
2624 if (evalarg == NULL)
2625 clear_evalarg(&local_evalarg, NULL);
2626 else
2627 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629
2630 return OK;
2631}
2632
2633/*
2634 * Handle first level expression:
2635 * expr2 || expr2 || expr2 logical OR
2636 *
2637 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002638 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *
2640 * Return OK or FAIL.
2641 */
2642 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002645 char_u *p;
2646 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002649 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return FAIL;
2653
2654 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002657 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 evalarg_T *evalarg_used = evalarg;
2661 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002662 int evaluate;
2663 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 long result = FALSE;
2665 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002666 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002668
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669 if (evalarg == NULL)
2670 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002671 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674 orig_flags = evalarg_used->eval_flags;
2675 evaluate = orig_flags & EVAL_EVALUATE;
2676 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002678 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002679 result = tv_get_bool_chk(rettv, &error);
2680 else if (tv_get_number_chk(rettv, &error) != 0)
2681 result = TRUE;
2682 clear_tv(rettv);
2683 if (error)
2684 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686
2687 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002690 while (p[0] == '|' && p[1] == '|')
2691 {
2692 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002693 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002694 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002695 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002696 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002697 {
2698 error_white_both(p, 2);
2699 clear_tv(rettv);
2700 return FAIL;
2701 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002702 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002703 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002704
2705 /*
2706 * Get the second variable.
2707 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002708 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002709 {
mityu4ac198c2021-05-28 17:52:40 +02002710 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002711 clear_tv(rettv);
2712 return FAIL;
2713 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2715 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002718 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719
2720 /*
2721 * Compute the result.
2722 */
2723 if (evaluate && !result)
2724 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002725 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002726 result = tv_get_bool_chk(&var2, &error);
2727 else if (tv_get_number_chk(&var2, &error) != 0)
2728 result = TRUE;
2729 clear_tv(&var2);
2730 if (error)
2731 return FAIL;
2732 }
2733 if (evaluate)
2734 {
2735 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002736 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002737 rettv->v_type = VAR_BOOL;
2738 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002739 }
2740 else
2741 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002742 rettv->v_type = VAR_NUMBER;
2743 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002744 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002745 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746
2747 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002749
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750 if (evalarg == NULL)
2751 clear_evalarg(&local_evalarg, NULL);
2752 else
2753 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755
2756 return OK;
2757}
2758
2759/*
2760 * Handle second level expression:
2761 * expr3 && expr3 && expr3 logical AND
2762 *
2763 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002764 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 *
2766 * Return OK or FAIL.
2767 */
2768 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002769eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002771 char_u *p;
2772 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002775 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002777 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 return FAIL;
2779
2780 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002781 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002783 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002784 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002786 evalarg_T *evalarg_used = evalarg;
2787 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002788 int orig_flags;
2789 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002790 long result = TRUE;
2791 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002792 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002793 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002794
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 if (evalarg == NULL)
2796 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002797 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002798 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002799 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002800 orig_flags = evalarg_used->eval_flags;
2801 evaluate = orig_flags & EVAL_EVALUATE;
2802 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002803 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002804 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002805 result = tv_get_bool_chk(rettv, &error);
2806 else if (tv_get_number_chk(rettv, &error) == 0)
2807 result = FALSE;
2808 clear_tv(rettv);
2809 if (error)
2810 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
2813 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002816 while (p[0] == '&' && p[1] == '&')
2817 {
2818 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002819 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002820 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002821 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002822 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002823 {
2824 error_white_both(p, 2);
2825 clear_tv(rettv);
2826 return FAIL;
2827 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002828 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002829 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002830
2831 /*
2832 * Get the second variable.
2833 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002834 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002835 {
mityu4ac198c2021-05-28 17:52:40 +02002836 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002837 clear_tv(rettv);
2838 return FAIL;
2839 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002840 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2841 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002842 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002843 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002844 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002845 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002846
2847 /*
2848 * Compute the result.
2849 */
2850 if (evaluate && result)
2851 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002852 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002853 result = tv_get_bool_chk(&var2, &error);
2854 else if (tv_get_number_chk(&var2, &error) == 0)
2855 result = FALSE;
2856 clear_tv(&var2);
2857 if (error)
2858 return FAIL;
2859 }
2860 if (evaluate)
2861 {
2862 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002863 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002864 rettv->v_type = VAR_BOOL;
2865 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002866 }
2867 else
2868 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002869 rettv->v_type = VAR_NUMBER;
2870 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002871 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002872 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002873
2874 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002876
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002877 if (evalarg == NULL)
2878 clear_evalarg(&local_evalarg, NULL);
2879 else
2880 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882
2883 return OK;
2884}
2885
2886/*
2887 * Handle third level expression:
2888 * var1 == var2
2889 * var1 =~ var2
2890 * var1 != var2
2891 * var1 !~ var2
2892 * var1 > var2
2893 * var1 >= var2
2894 * var1 < var2
2895 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002896 * var1 is var2
2897 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 *
2899 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002900 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 *
2902 * Return OK or FAIL.
2903 */
2904 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002905eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002908 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002909 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002911 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002914 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 return FAIL;
2918
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002919 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002920
Bram Moolenaar696ba232020-07-29 21:20:41 +02002921 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002924 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002926 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002928 typval_T var2;
2929 int ic;
2930 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002931 int evaluate = evalarg == NULL
2932 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002933 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002934
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002935 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002936 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002937 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002938 p = *arg;
2939 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002940 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2941 {
mityu4ac198c2021-05-28 17:52:40 +02002942 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002943 clear_tv(rettv);
2944 return FAIL;
2945 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002946
Bram Moolenaar696ba232020-07-29 21:20:41 +02002947 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2948 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002949 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002950 clear_tv(rettv);
2951 return FAIL;
2952 }
2953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002954 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (p[len] == '?')
2956 {
2957 ic = TRUE;
2958 ++len;
2959 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002960 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else if (p[len] == '#')
2962 {
2963 ic = FALSE;
2964 ++len;
2965 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002966 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002968 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 /*
2971 * Get the second variable.
2972 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002973 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2974 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002975 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002976 clear_tv(rettv);
2977 return FAIL;
2978 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002979 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return FAIL;
2984 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002985 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002986 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002987 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002988
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002989 // use the line of the comparison for messages
2990 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002991 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002992 {
2993 ret = FAIL;
2994 clear_tv(rettv);
2995 }
2996 else
2997 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002998 clear_tv(&var2);
2999 return ret;
3000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002
3003 return OK;
3004}
3005
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003006/*
3007 * Make a copy of blob "tv1" and append blob "tv2".
3008 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 void
3010eval_addblob(typval_T *tv1, typval_T *tv2)
3011{
3012 blob_T *b1 = tv1->vval.v_blob;
3013 blob_T *b2 = tv2->vval.v_blob;
3014 blob_T *b = blob_alloc();
3015 int i;
3016
3017 if (b != NULL)
3018 {
3019 for (i = 0; i < blob_len(b1); i++)
3020 ga_append(&b->bv_ga, blob_get(b1, i));
3021 for (i = 0; i < blob_len(b2); i++)
3022 ga_append(&b->bv_ga, blob_get(b2, i));
3023
3024 clear_tv(tv1);
3025 rettv_blob_set(tv1, b);
3026 }
3027}
3028
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003029/*
3030 * Make a copy of list "tv1" and append list "tv2".
3031 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 int
3033eval_addlist(typval_T *tv1, typval_T *tv2)
3034{
3035 typval_T var3;
3036
3037 // concatenate Lists
3038 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3039 {
3040 clear_tv(tv1);
3041 clear_tv(tv2);
3042 return FAIL;
3043 }
3044 clear_tv(tv1);
3045 *tv1 = var3;
3046 return OK;
3047}
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003050 * Handle the bitwise left/right shift operator expression:
3051 * var1 << var2
3052 * var1 >> var2
3053 *
3054 * "arg" must point to the first non-white of the expression.
3055 * "arg" is advanced to just after the recognized expression.
3056 *
3057 * Return OK or FAIL.
3058 */
3059 static int
3060eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3061{
3062 /*
3063 * Get the first expression.
3064 */
3065 if (eval6(arg, rettv, evalarg) == FAIL)
3066 return FAIL;
3067
3068 /*
3069 * Repeat computing, until no '<<' or '>>' is following.
3070 */
3071 for (;;)
3072 {
3073 char_u *p;
3074 int getnext;
3075 exprtype_T type;
3076 int evaluate;
3077 typval_T var2;
3078 int vim9script;
3079
3080 p = eval_next_non_blank(*arg, evalarg, &getnext);
3081 if (p[0] == '<' && p[1] == '<')
3082 type = EXPR_LSHIFT;
3083 else if (p[0] == '>' && p[1] == '>')
3084 type = EXPR_RSHIFT;
3085 else
3086 return OK;
3087
3088 // Handle a bitwise left or right shift operator
3089 if (rettv->v_type != VAR_NUMBER)
3090 {
3091 // left operand should be a number
3092 emsg(_(e_bitshift_ops_must_be_number));
3093 clear_tv(rettv);
3094 return FAIL;
3095 }
3096
3097 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3098 vim9script = in_vim9script();
3099 if (getnext)
3100 {
3101 *arg = eval_next_line(*arg, evalarg);
3102 p = *arg;
3103 }
3104 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3105 {
3106 error_white_both(*arg, 2);
3107 clear_tv(rettv);
3108 return FAIL;
3109 }
3110
3111 /*
3112 * Get the second variable.
3113 */
3114 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3115 {
3116 error_white_both(p, 2);
3117 clear_tv(rettv);
3118 return FAIL;
3119 }
3120 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3121 if (eval6(arg, &var2, evalarg) == FAIL)
3122 {
3123 clear_tv(rettv);
3124 return FAIL;
3125 }
3126
3127 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3128 {
3129 // right operand should be a positive number
3130 if (var2.v_type != VAR_NUMBER)
3131 emsg(_(e_bitshift_ops_must_be_number));
3132 else
3133 emsg(_(e_bitshift_ops_must_be_postive));
3134 clear_tv(rettv);
3135 clear_tv(&var2);
3136 return FAIL;
3137 }
3138
3139 if (evaluate)
3140 {
3141 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3142 // shifting more bits than we have always results in zero
3143 rettv->vval.v_number = 0;
3144 else if (type == EXPR_LSHIFT)
3145 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003146 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003147 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003148 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003149 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003150 }
3151
3152 clear_tv(&var2);
3153 }
3154
3155 return OK;
3156}
3157
3158/*
3159 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003160 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003162 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003163 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 *
3165 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003166 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 *
3168 * Return OK or FAIL.
3169 */
3170 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003171eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003174 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003176 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 return FAIL;
3178
3179 /*
3180 * Repeat computing, until no '+', '-' or '.' is following.
3181 */
3182 for (;;)
3183 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003184 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003185 int getnext;
3186 char_u *p;
3187 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003188 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003189 int concat;
3190 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003191 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003192
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003193 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003194 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003195 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003196 p = eval_next_non_blank(*arg, evalarg, &getnext);
3197 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003198 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003199 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3200 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003202 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3203 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003204
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003205 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003206 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003207 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003208 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003209 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003210 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003211 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003212 {
mityu4ac198c2021-05-28 17:52:40 +02003213 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003214 clear_tv(rettv);
3215 return FAIL;
3216 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003217 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003218 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003219 if ((op != '+' || (rettv->v_type != VAR_LIST
3220 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003221#ifdef FEAT_FLOAT
3222 && (op == '.' || rettv->v_type != VAR_FLOAT)
3223#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003224 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003225 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003226 int error = FALSE;
3227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003228 // For "list + ...", an illegal use of the first operand as
3229 // a number cannot be determined before evaluating the 2nd
3230 // operand: if this is also a list, all is ok.
3231 // For "something . ...", "something - ..." or "non-list + ...",
3232 // we know that the first operand needs to be a string or number
3233 // without evaluating the 2nd operand. So check before to avoid
3234 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003235 if (op != '.')
3236 tv_get_number_chk(rettv, &error);
3237 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003238 {
3239 clear_tv(rettv);
3240 return FAIL;
3241 }
3242 }
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 /*
3245 * Get the second variable.
3246 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003247 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003248 {
mityu89dcb4d2021-05-28 13:50:17 +02003249 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003250 clear_tv(rettv);
3251 return FAIL;
3252 }
3253 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003254 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003256 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 return FAIL;
3258 }
3259
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003260 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 /*
3263 * Compute the result.
3264 */
3265 if (op == '.')
3266 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003267 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3268 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003269 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003270
Bram Moolenaar2e086612020-08-25 22:37:48 +02003271 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003272 || var2.v_type == VAR_CHANNEL
3273 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003274 semsg(_(e_using_invalid_value_as_string_str),
3275 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003276#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003277 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003278 {
3279 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3280 var2.vval.v_float);
3281 s2 = buf2;
3282 }
3283#endif
3284 else
3285 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003286 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003287 {
3288 clear_tv(rettv);
3289 clear_tv(&var2);
3290 return FAIL;
3291 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003292 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003293 clear_tv(rettv);
3294 rettv->v_type = VAR_STRING;
3295 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003297 else if (op == '+' && rettv->v_type == VAR_BLOB
3298 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003300 else if (op == '+' && rettv->v_type == VAR_LIST
3301 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003302 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003304 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 else
3307 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003308 int error = FALSE;
3309 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003310#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003311 float_T f1 = 0, f2 = 0;
3312
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003313 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003314 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003315 f1 = rettv->vval.v_float;
3316 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318 else
3319#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 if (error)
3323 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003324 // This can only happen for "list + non-list" or
3325 // "blob + non-blob". For "non-list + ..." or
3326 // "something - ...", we returned before evaluating the
3327 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003329 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003330 return FAIL;
3331 }
3332#ifdef FEAT_FLOAT
3333 if (var2.v_type == VAR_FLOAT)
3334 f1 = n1;
3335#endif
3336 }
3337#ifdef FEAT_FLOAT
3338 if (var2.v_type == VAR_FLOAT)
3339 {
3340 f2 = var2.vval.v_float;
3341 n2 = 0;
3342 }
3343 else
3344#endif
3345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 if (error)
3348 {
3349 clear_tv(rettv);
3350 clear_tv(&var2);
3351 return FAIL;
3352 }
3353#ifdef FEAT_FLOAT
3354 if (rettv->v_type == VAR_FLOAT)
3355 f2 = n2;
3356#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359
3360#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003362 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3363 {
3364 if (op == '+')
3365 f1 = f1 + f2;
3366 else
3367 f1 = f1 - f2;
3368 rettv->v_type = VAR_FLOAT;
3369 rettv->vval.v_float = f1;
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003372#endif
3373 {
3374 if (op == '+')
3375 n1 = n1 + n2;
3376 else
3377 n1 = n1 - n2;
3378 rettv->v_type = VAR_NUMBER;
3379 rettv->vval.v_number = n1;
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003382 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 }
3385 return OK;
3386}
3387
3388/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003389 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * * number multiplication
3391 * / number division
3392 * % number modulo
3393 *
3394 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003395 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 *
3397 * Return OK or FAIL.
3398 */
3399 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003400eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003401 char_u **arg,
3402 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003403 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003406#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003407 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003411 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003413 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return FAIL;
3415
3416 /*
3417 * Repeat computing, until no '*', '/' or '%' is following.
3418 */
3419 for (;;)
3420 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003421 int evaluate;
3422 int getnext;
3423 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003424 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003425 int op;
3426 varnumber_T n1, n2;
3427#ifdef FEAT_FLOAT
3428 float_T f1, f2;
3429#endif
3430 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003431
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003432 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003433 p = eval_next_non_blank(*arg, evalarg, &getnext);
3434 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003435 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003437
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003438 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003439 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003440 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003441 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003442 {
3443 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3444 {
mityu4ac198c2021-05-28 17:52:40 +02003445 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003446 clear_tv(rettv);
3447 return FAIL;
3448 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003449 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003452#ifdef FEAT_FLOAT
3453 f1 = 0;
3454 f2 = 0;
3455#endif
3456 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003457 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003459#ifdef FEAT_FLOAT
3460 if (rettv->v_type == VAR_FLOAT)
3461 {
3462 f1 = rettv->vval.v_float;
3463 use_float = TRUE;
3464 n1 = 0;
3465 }
3466 else
3467#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003468 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003469 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003470 if (error)
3471 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 else
3474 n1 = 0;
3475
3476 /*
3477 * Get the second variable.
3478 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003479 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3480 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003481 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003482 clear_tv(rettv);
3483 return FAIL;
3484 }
3485 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003486 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return FAIL;
3488
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003489 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003491#ifdef FEAT_FLOAT
3492 if (var2.v_type == VAR_FLOAT)
3493 {
3494 if (!use_float)
3495 {
3496 f1 = n1;
3497 use_float = TRUE;
3498 }
3499 f2 = var2.vval.v_float;
3500 n2 = 0;
3501 }
3502 else
3503#endif
3504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003506 clear_tv(&var2);
3507 if (error)
3508 return FAIL;
3509#ifdef FEAT_FLOAT
3510 if (use_float)
3511 f2 = n2;
3512#endif
3513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /*
3516 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003517 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519#ifdef FEAT_FLOAT
3520 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522 if (op == '*')
3523 f1 = f1 * f2;
3524 else if (op == '/')
3525 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003526# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003527 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003528 if (f2 == 0.0)
3529 {
3530 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003531 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003532 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003533 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003534 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003535 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003536 }
3537 else
3538 f1 = f1 / f2;
3539# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003540 // We rely on the floating point library to handle divide
3541 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003543# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003546 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003547 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 return FAIL;
3549 }
3550 rettv->v_type = VAR_FLOAT;
3551 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003556 int failed = FALSE;
3557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003558 if (op == '*')
3559 n1 = n1 * n2;
3560 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003561 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003563 n1 = num_modulus(n1, n2, &failed);
3564 if (failed)
3565 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003567 rettv->v_type = VAR_NUMBER;
3568 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 }
3572
3573 return OK;
3574}
3575
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003576/*
3577 * Handle a type cast before a base level expression.
3578 * "arg" must point to the first non-white of the expression.
3579 * "arg" is advanced to just after the recognized expression.
3580 * Return OK or FAIL.
3581 */
3582 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003583eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003584 char_u **arg,
3585 typval_T *rettv,
3586 evalarg_T *evalarg,
3587 int want_string) // after "." operator
3588{
3589 type_T *want_type = NULL;
3590 garray_T type_list; // list of pointers to allocated types
3591 int res;
3592 int evaluate = evalarg == NULL ? 0
3593 : (evalarg->eval_flags & EVAL_EVALUATE);
3594
3595 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003596 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3597 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003598 {
3599 ++*arg;
3600 ga_init2(&type_list, sizeof(type_T *), 10);
3601 want_type = parse_type(arg, &type_list, TRUE);
3602 if (want_type == NULL && (evaluate || **arg != '>'))
3603 {
3604 clear_type_list(&type_list);
3605 return FAIL;
3606 }
3607
3608 if (**arg != '>')
3609 {
3610 if (*skipwhite(*arg) == '>')
3611 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3612 else
3613 emsg(_(e_missing_gt));
3614 clear_type_list(&type_list);
3615 return FAIL;
3616 }
3617 ++*arg;
3618 *arg = skipwhite_and_linebreak(*arg, evalarg);
3619 }
3620
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003621 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003622
3623 if (want_type != NULL && evaluate)
3624 {
3625 if (res == OK)
3626 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003627 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3628 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003629
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003630 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003631 {
3632 if (want_type == &t_bool && actual != &t_bool
3633 && (actual->tt_flags & TTFLAG_BOOL_OK))
3634 {
3635 int n = tv2bool(rettv);
3636
3637 // can use "0" and "1" for boolean in some places
3638 clear_tv(rettv);
3639 rettv->v_type = VAR_BOOL;
3640 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3641 }
3642 else
3643 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003644 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003645
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003646 where.wt_variable = TRUE;
3647 res = check_type(want_type, actual, TRUE, where);
3648 }
3649 }
3650 }
3651 clear_type_list(&type_list);
3652 }
3653
3654 return res;
3655}
3656
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003657 int
3658eval_leader(char_u **arg, int vim9)
3659{
3660 char_u *s = *arg;
3661 char_u *p = *arg;
3662
3663 while (*p == '!' || *p == '-' || *p == '+')
3664 {
3665 char_u *n = skipwhite(p + 1);
3666
3667 // ++, --, -+ and +- are not accepted in Vim9 script
3668 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3669 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003670 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003671 return FAIL;
3672 }
3673 p = n;
3674 }
3675 *arg = p;
3676 return OK;
3677}
3678
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003680 * Check for a predefined value "true", "false" and "null.*".
3681 * Return OK when recognized.
3682 */
3683 int
3684handle_predefined(char_u *s, int len, typval_T *rettv)
3685{
3686 switch (len)
3687 {
3688 case 4: if (STRNCMP(s, "true", 4) == 0)
3689 {
3690 rettv->v_type = VAR_BOOL;
3691 rettv->vval.v_number = VVAL_TRUE;
3692 return OK;
3693 }
3694 if (STRNCMP(s, "null", 4) == 0)
3695 {
3696 rettv->v_type = VAR_SPECIAL;
3697 rettv->vval.v_number = VVAL_NULL;
3698 return OK;
3699 }
3700 break;
3701 case 5: if (STRNCMP(s, "false", 5) == 0)
3702 {
3703 rettv->v_type = VAR_BOOL;
3704 rettv->vval.v_number = VVAL_FALSE;
3705 return OK;
3706 }
3707 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003708 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3709 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003710#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003711 rettv->v_type = VAR_JOB;
3712 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003713#else
3714 rettv->v_type = VAR_SPECIAL;
3715 rettv->vval.v_number = VVAL_NULL;
3716#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003717 return OK;
3718 }
3719 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003720 case 9:
3721 if (STRNCMP(s, "null_", 5) != 0)
3722 break;
3723 if (STRNCMP(s + 5, "list", 4) == 0)
3724 {
3725 rettv->v_type = VAR_LIST;
3726 rettv->vval.v_list = NULL;
3727 return OK;
3728 }
3729 if (STRNCMP(s + 5, "dict", 4) == 0)
3730 {
3731 rettv->v_type = VAR_DICT;
3732 rettv->vval.v_dict = NULL;
3733 return OK;
3734 }
3735 if (STRNCMP(s + 5, "blob", 4) == 0)
3736 {
3737 rettv->v_type = VAR_BLOB;
3738 rettv->vval.v_blob = NULL;
3739 return OK;
3740 }
3741 break;
3742 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3743 {
3744 rettv->v_type = VAR_STRING;
3745 rettv->vval.v_string = NULL;
3746 return OK;
3747 }
3748 break;
3749 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003750 if (STRNCMP(s, "null_channel", 12) == 0)
3751 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003752#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003753 rettv->v_type = VAR_CHANNEL;
3754 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003755#else
3756 rettv->v_type = VAR_SPECIAL;
3757 rettv->vval.v_number = VVAL_NULL;
3758#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003759 return OK;
3760 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003761 if (STRNCMP(s, "null_partial", 12) == 0)
3762 {
3763 rettv->v_type = VAR_PARTIAL;
3764 rettv->vval.v_partial = NULL;
3765 return OK;
3766 }
3767 break;
3768 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3769 {
3770 rettv->v_type = VAR_FUNC;
3771 rettv->vval.v_string = NULL;
3772 return OK;
3773 }
3774 break;
3775 }
3776 return FAIL;
3777}
3778
3779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Handle sixth level expression:
3781 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003782 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003783 * "string" string constant
3784 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 * &option-name option value
3786 * @r register contents
3787 * identifier variable value
3788 * function() function call
3789 * $VAR environment variable
3790 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003791 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003793 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003794 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 *
3796 * Also handle:
3797 * ! in front logical NOT
3798 * - in front unary minus
3799 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003800 * trailing [] subscript in String or List
3801 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003802 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 *
3804 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003805 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 *
3807 * Return OK or FAIL.
3808 */
3809 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003810eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003811 char_u **arg,
3812 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003813 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003816 int evaluate = evalarg != NULL
3817 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 int len;
3819 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003820 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 char_u *start_leader, *end_leader;
3822 int ret = OK;
3823 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003824 static int recurse = 0;
3825 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003828 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003829 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003834 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
3836 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003837 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 end_leader = *arg;
3840
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003841 if (**arg == '.' && (!isdigit(*(*arg + 1))
3842#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003843 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003844#endif
3845 ))
3846 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003847 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003848 ++*arg;
3849 return FAIL;
3850 }
3851
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003852 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003853 // and crash. With MSVC the stack is smaller.
3854 if (recurse ==
3855#ifdef _MSC_VER
3856 300
3857#else
3858 1000
3859#endif
3860 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003861 {
3862 semsg(_(e_expression_too_recursive_str), *arg);
3863 return FAIL;
3864 }
3865 ++recurse;
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 switch (**arg)
3868 {
3869 /*
3870 * Number constant.
3871 */
3872 case '0':
3873 case '1':
3874 case '2':
3875 case '3':
3876 case '4':
3877 case '5':
3878 case '6':
3879 case '7':
3880 case '8':
3881 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003882 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003883
3884 // Apply prefixed "-" and "+" now. Matters especially when
3885 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003886 if (ret == OK && evaluate && end_leader > start_leader
3887 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003888 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 break;
3890
3891 /*
3892 * String constant: "string".
3893 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003894 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 break;
3896
3897 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003900 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003901 break;
3902
3903 /*
3904 * List: [expr, expr]
3905 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003906 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908
3909 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003910 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003911 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003912 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003913 {
3914 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3915 }
3916 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003917 {
3918 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003919 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003920 }
3921 else
3922 ret = NOTDONE;
3923 break;
3924
3925 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003926 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003927 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003928 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003929 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003930 ret = NOTDONE;
3931 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003932 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003933 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003934 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003935 break;
3936
3937 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003938 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003940 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942
3943 /*
3944 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003945 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 */
LemonBoy2eaef102022-05-06 13:14:50 +01003947 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3948 ret = eval_interp_string(arg, rettv, evaluate);
3949 else
3950 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 break;
3952
3953 /*
3954 * Register contents: @r.
3955 */
3956 case '@': ++*arg;
3957 if (evaluate)
3958 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003959 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003960 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003961 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003962 emsg_invreg(**arg);
3963 else
3964 {
3965 rettv->v_type = VAR_STRING;
3966 rettv->vval.v_string = get_reg_contents(**arg,
3967 GREG_EXPR_SRC);
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 if (**arg != NUL)
3971 ++*arg;
3972 break;
3973
3974 /*
3975 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003976 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003978 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003979 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003980 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003981 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003982 if (ret == OK && evaluate)
3983 {
3984 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3985
Bram Moolenaara9931532021-06-12 15:58:16 +02003986 // Compile it here to get the return type. The return
3987 // type is optional, when it's missing use t_unknown.
3988 // This is recognized in compile_return().
3989 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3990 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003991 if (compile_def_function(ufunc, FALSE,
3992 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003993 {
3994 clear_tv(rettv);
3995 ret = FAIL;
3996 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003997 }
3998 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003999 if (ret == NOTDONE)
4000 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004001 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004002 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004003
Bram Moolenaar9215f012020-06-27 21:18:00 +02004004 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004005 if (**arg == ')')
4006 ++*arg;
4007 else if (ret == OK)
4008 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004009 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004010 clear_tv(rettv);
4011 ret = FAIL;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 break;
4015
Bram Moolenaar8c711452005-01-14 21:53:12 +00004016 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 break;
4018 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019
4020 if (ret == NOTDONE)
4021 {
4022 /*
4023 * Must be a variable or function name.
4024 * Can also be a curly-braces kind of name: {expr}.
4025 */
4026 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004027 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004028 if (alias != NULL)
4029 s = alias;
4030
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004031 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004032 ret = FAIL;
4033 else
4034 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004035 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4036
Bram Moolenaar4525a572022-02-13 11:57:33 +00004037 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004038 {
4039 emsg(_(e_cannot_use_underscore_here));
4040 ret = FAIL;
4041 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004042 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004043 && s[0] == 's' && s[1] == ':')
4044 {
4045 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4046 ret = FAIL;
4047 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004048 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004049 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004050 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004051 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004052 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004053 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004054 else if (flags & EVAL_CONSTANT)
4055 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004056 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004057 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004058 // get the value of "true", "false", etc. or a variable
4059 ret = FAIL;
4060 if (vim9script)
4061 ret = handle_predefined(s, len, rettv);
4062 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004063 {
4064 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004065 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004066 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004067 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004069 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004070 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004071 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004072 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004073 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004074 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004075 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004076 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004077 }
4078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004079 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4080 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004081 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004082 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4086 */
4087 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004088 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004089
4090 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004091 return ret;
4092}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004094/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004095 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004096 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004097 * Adjusts "end_leaderp" until it is at "start_leader".
4098 */
4099 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004100eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004101 typval_T *rettv,
4102 int numeric_only,
4103 char_u *start_leader,
4104 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004105{
4106 char_u *end_leader = *end_leaderp;
4107 int ret = OK;
4108 int error = FALSE;
4109 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004110 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004111 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004112#ifdef FEAT_FLOAT
4113 float_T f = 0.0;
4114
4115 if (rettv->v_type == VAR_FLOAT)
4116 f = rettv->vval.v_float;
4117 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004118#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004119 {
4120 while (VIM_ISWHITE(end_leader[-1]))
4121 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004122 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004123 val = tv2bool(rettv);
4124 else
4125 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004126 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004127 if (error)
4128 {
4129 clear_tv(rettv);
4130 ret = FAIL;
4131 }
4132 else
4133 {
4134 while (end_leader > start_leader)
4135 {
4136 --end_leader;
4137 if (*end_leader == '!')
4138 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004139 if (numeric_only)
4140 {
4141 ++end_leader;
4142 break;
4143 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004144#ifdef FEAT_FLOAT
4145 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004146 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004147 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004148 {
4149 rettv->v_type = VAR_BOOL;
4150 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4151 }
4152 else
4153 f = !f;
4154 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004155 else
4156#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004157 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004158 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004159 type = VAR_BOOL;
4160 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004161 }
4162 else if (*end_leader == '-')
4163 {
4164#ifdef FEAT_FLOAT
4165 if (rettv->v_type == VAR_FLOAT)
4166 f = -f;
4167 else
4168#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004169 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004170 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004171 type = VAR_NUMBER;
4172 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004173 }
4174 }
4175#ifdef FEAT_FLOAT
4176 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004179 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004182#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004184 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004185 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004186 rettv->v_type = type;
4187 else
4188 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004189 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004192 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return ret;
4194}
4195
4196/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004197 * Call the function referred to in "rettv".
4198 */
4199 static int
4200call_func_rettv(
4201 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004202 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004203 typval_T *rettv,
4204 int evaluate,
4205 dict_T *selfdict,
4206 typval_T *basetv)
4207{
4208 partial_T *pt = NULL;
4209 funcexe_T funcexe;
4210 typval_T functv;
4211 char_u *s;
4212 int ret;
4213
4214 // need to copy the funcref so that we can clear rettv
4215 if (evaluate)
4216 {
4217 functv = *rettv;
4218 rettv->v_type = VAR_UNKNOWN;
4219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004221 if (functv.v_type == VAR_PARTIAL)
4222 {
4223 pt = functv.vval.v_partial;
4224 s = partial_name(pt);
4225 }
4226 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004227 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004228 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004229 if (s == NULL || *s == NUL)
4230 {
4231 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004232 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004233 goto theend;
4234 }
4235 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004236 }
4237 else
4238 s = (char_u *)"";
4239
Bram Moolenaara80faa82020-04-12 19:37:17 +02004240 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004241 funcexe.fe_firstline = curwin->w_cursor.lnum;
4242 funcexe.fe_lastline = curwin->w_cursor.lnum;
4243 funcexe.fe_evaluate = evaluate;
4244 funcexe.fe_partial = pt;
4245 funcexe.fe_selfdict = selfdict;
4246 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004247 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004248
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004249theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004250 // Clear the funcref afterwards, so that deleting it while
4251 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004252 if (evaluate)
4253 clear_tv(&functv);
4254
4255 return ret;
4256}
4257
4258/*
4259 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004260 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004261 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4262 */
4263 static int
4264eval_lambda(
4265 char_u **arg,
4266 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004267 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004268 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004269{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004270 int evaluate = evalarg != NULL
4271 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004272 typval_T base = *rettv;
4273 int ret;
4274
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004275 rettv->v_type = VAR_UNKNOWN;
4276
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004277 if (**arg == '{')
4278 {
4279 // ->{lambda}()
4280 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4281 }
4282 else
4283 {
4284 // ->(lambda)()
4285 ++*arg;
4286 ret = eval1(arg, rettv, evalarg);
4287 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004288 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004289 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004290 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004291 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004292 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004293 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4294 && rettv->v_type != VAR_PARTIAL)
4295 {
4296 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4297 return FAIL;
4298 }
4299 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004300 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004301 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004302 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004303
4304 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004305 {
4306 if (verbose)
4307 {
4308 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004309 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004310 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004311 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004312 }
4313 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004314 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004315 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004316 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004317 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004318
4319 // Clear the funcref afterwards, so that deleting it while
4320 // evaluating the arguments is possible (see test55).
4321 if (evaluate)
4322 clear_tv(&base);
4323
4324 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004325}
4326
4327/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004328 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004329 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004330 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4331 */
4332 static int
4333eval_method(
4334 char_u **arg,
4335 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004336 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004337 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004338{
4339 char_u *name;
4340 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004341 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004342 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004343 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004344 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004345 int evaluate = evalarg != NULL
4346 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004347
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004348 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004349
Bram Moolenaarac92e252019-08-03 21:58:38 +02004350 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004351 len = get_name_len(arg, &alias, evaluate, TRUE);
4352 if (alias != NULL)
4353 name = alias;
4354
4355 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004356 {
4357 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004358 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004359 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004360 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004361 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004362 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004363 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004364
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004365 // If there is no "(" immediately following, but there is further on,
4366 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4367 // Does not handle anything where "(" is part of the expression.
4368 *arg = skipwhite(*arg);
4369
4370 if (**arg != '(' && alias == NULL
4371 && (paren = vim_strchr(*arg, '(')) != NULL)
4372 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004373 char_u *deref;
4374
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004375 *arg = name;
4376 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004377 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4378 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004379 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004380 *arg = name + len;
4381 ret = FAIL;
4382 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004383 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004384 {
4385 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004386 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004387 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004388 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004389 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004390
4391 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004392 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004393 *arg = skipwhite(*arg);
4394
4395 if (**arg != '(')
4396 {
4397 if (verbose)
4398 semsg(_(e_missing_parenthesis_str), name);
4399 ret = FAIL;
4400 }
4401 else if (VIM_ISWHITE((*arg)[-1]))
4402 {
4403 if (verbose)
4404 emsg(_(e_no_white_space_allowed_before_parenthesis));
4405 ret = FAIL;
4406 }
4407 else
4408 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004409 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004410 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004411 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004412
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004413 // Clear the funcref afterwards, so that deleting it while
4414 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004415 if (evaluate)
4416 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004417 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004418
Bram Moolenaarac92e252019-08-03 21:58:38 +02004419 return ret;
4420}
4421
4422/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004423 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4424 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4426 */
4427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004428eval_index(
4429 char_u **arg,
4430 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004431 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004432 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004434 int evaluate = evalarg != NULL
4435 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004436 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004437 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004439 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004440 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004441 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004443 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4444 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004446 init_tv(&var1);
4447 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 /*
4451 * dict.name
4452 */
4453 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004454 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004456 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004458 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459 }
4460 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 /*
4463 * something[idx]
4464 *
4465 * Get the (first) variable from inside the [].
4466 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004467 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 if (**arg == ':')
4469 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004470 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004471 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004472 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004473 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004474 semsg(_(e_white_space_required_before_and_after_str_at_str),
4475 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004476 clear_tv(&var1);
4477 return FAIL;
4478 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004479 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004480 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004481 int error = FALSE;
4482
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004483#ifdef FEAT_FLOAT
4484 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004485 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004486 && var1.v_type == VAR_FLOAT)
4487 {
4488 var1.vval.v_string = typval_tostring(&var1, TRUE);
4489 var1.v_type = VAR_STRING;
4490 }
4491#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004492 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004493 tv_get_number_chk(&var1, &error);
4494 else
4495 error = tv_get_string_chk(&var1) == NULL;
4496 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004497 {
4498 // not a number or string
4499 clear_tv(&var1);
4500 return FAIL;
4501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503
4504 /*
4505 * Get the second variable from inside the [:].
4506 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004507 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 if (**arg == ':')
4509 {
4510 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004511 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004512 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004513 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004514 semsg(_(e_white_space_required_before_and_after_str_at_str),
4515 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004516 if (!empty1)
4517 clear_tv(&var1);
4518 return FAIL;
4519 }
4520 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 if (**arg == ']')
4522 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004523 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004525 if (!empty1)
4526 clear_tv(&var1);
4527 return FAIL;
4528 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004529 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004530 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004532 if (!empty1)
4533 clear_tv(&var1);
4534 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 return FAIL;
4536 }
4537 }
4538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004540 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 if (**arg != ']')
4542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004544 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 clear_tv(&var1);
4546 if (range)
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004550 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 }
4552
4553 if (evaluate)
4554 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004556 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004557 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004558
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004559 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004562 clear_tv(&var2);
4563 return res;
4564 }
4565 return OK;
4566}
4567
4568/*
4569 * Check if "rettv" can have an [index] or [sli:ce]
4570 */
4571 int
4572check_can_index(typval_T *rettv, int evaluate, int verbose)
4573{
4574 switch (rettv->v_type)
4575 {
4576 case VAR_FUNC:
4577 case VAR_PARTIAL:
4578 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004579 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004580 return FAIL;
4581 case VAR_FLOAT:
4582#ifdef FEAT_FLOAT
4583 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004584 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004585 return FAIL;
4586#endif
4587 case VAR_BOOL:
4588 case VAR_SPECIAL:
4589 case VAR_JOB:
4590 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004591 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004592 if (verbose)
4593 emsg(_(e_cannot_index_special_variable));
4594 return FAIL;
4595 case VAR_UNKNOWN:
4596 case VAR_ANY:
4597 case VAR_VOID:
4598 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004600 emsg(_(e_cannot_index_special_variable));
4601 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004603 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004605 case VAR_STRING:
4606 case VAR_LIST:
4607 case VAR_DICT:
4608 case VAR_BLOB:
4609 break;
4610 case VAR_NUMBER:
4611 if (in_vim9script())
4612 emsg(_(e_cannot_index_number));
4613 break;
4614 }
4615 return OK;
4616}
4617
4618/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004619 * slice() function
4620 */
4621 void
4622f_slice(typval_T *argvars, typval_T *rettv)
4623{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004624 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004625 && ((argvars[0].v_type != VAR_STRING
4626 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004627 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004628 && check_for_list_arg(argvars, 0) == FAIL)
4629 || check_for_number_arg(argvars, 1) == FAIL
4630 || check_for_opt_number_arg(argvars, 2) == FAIL))
4631 return;
4632
Bram Moolenaar6601b622021-01-13 21:47:15 +01004633 if (check_can_index(argvars, TRUE, FALSE) == OK)
4634 {
4635 copy_tv(argvars, rettv);
4636 eval_index_inner(rettv, TRUE, argvars + 1,
4637 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4638 TRUE, NULL, 0, FALSE);
4639 }
4640}
4641
4642/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004643 * Apply index or range to "rettv".
4644 * "var1" is the first index, NULL for [:expr].
4645 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004646 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4647 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004648 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4649 */
4650 int
4651eval_index_inner(
4652 typval_T *rettv,
4653 int is_range,
4654 typval_T *var1,
4655 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004657 char_u *key,
4658 int keylen,
4659 int verbose)
4660{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004661 varnumber_T n1, n2 = 0;
4662 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663
4664 n1 = 0;
4665 if (var1 != NULL && rettv->v_type != VAR_DICT)
4666 n1 = tv_get_number(var1);
4667
4668 if (is_range)
4669 {
4670 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 if (verbose)
4673 emsg(_(e_cannot_slice_dictionary));
4674 return FAIL;
4675 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004676 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004677 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004678 else
4679 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004680 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004681
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004682 switch (rettv->v_type)
4683 {
4684 case VAR_UNKNOWN:
4685 case VAR_ANY:
4686 case VAR_VOID:
4687 case VAR_FUNC:
4688 case VAR_PARTIAL:
4689 case VAR_FLOAT:
4690 case VAR_BOOL:
4691 case VAR_SPECIAL:
4692 case VAR_JOB:
4693 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004694 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004695 break; // not evaluating, skipping over subscript
4696
4697 case VAR_NUMBER:
4698 case VAR_STRING:
4699 {
4700 char_u *s = tv_get_string(rettv);
4701
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004703 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004704 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004705 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004706 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004707 else
4708 s = char_from_string(s, n1);
4709 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004710 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // The resulting variable is a substring. If the indexes
4713 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714 if (n1 < 0)
4715 {
4716 n1 = len + n1;
4717 if (n1 < 0)
4718 n1 = 0;
4719 }
4720 if (n2 < 0)
4721 n2 = len + n2;
4722 else if (n2 >= len)
4723 n2 = len;
4724 if (n1 >= len || n2 < 0 || n1 > n2)
4725 s = NULL;
4726 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004727 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 }
4729 else
4730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004731 // The resulting variable is a string of a single
4732 // character. If the index is too big or negative the
4733 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 if (n1 >= len || n1 < 0)
4735 s = NULL;
4736 else
4737 s = vim_strnsave(s + n1, 1);
4738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
4740 rettv->v_type = VAR_STRING;
4741 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004742 }
4743 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004745 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004746 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4747 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004748 break;
4749
4750 case VAR_LIST:
4751 if (var1 == NULL)
4752 n1 = 0;
4753 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004754 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004755 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004756 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004757 return FAIL;
4758 break;
4759
4760 case VAR_DICT:
4761 {
4762 dictitem_T *item;
4763 typval_T tmp;
4764
4765 if (key == NULL)
4766 {
4767 key = tv_get_string_chk(var1);
4768 if (key == NULL)
4769 return FAIL;
4770 }
4771
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004772 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004775 {
4776 if (verbose)
4777 {
4778 if (keylen > 0)
4779 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004780 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004781 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004782 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004783 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004784
4785 copy_tv(&item->di_tv, &tmp);
4786 clear_tv(rettv);
4787 *rettv = tmp;
4788 }
4789 break;
4790 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 return OK;
4792}
4793
4794/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004795 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004796 */
4797 char_u *
4798partial_name(partial_T *pt)
4799{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004800 if (pt != NULL)
4801 {
4802 if (pt->pt_name != NULL)
4803 return pt->pt_name;
4804 if (pt->pt_func != NULL)
4805 return pt->pt_func->uf_name;
4806 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004807 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004808}
4809
Bram Moolenaarddecc252016-04-06 22:59:37 +02004810 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004811partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004812{
4813 int i;
4814
4815 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004816 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004817 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004818 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004819 if (pt->pt_name != NULL)
4820 {
4821 func_unref(pt->pt_name);
4822 vim_free(pt->pt_name);
4823 }
4824 else
4825 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004826
Bram Moolenaar54656012021-06-09 20:50:46 +02004827 // "out_up" is no longer used, decrement refcount on partial that owns it.
4828 partial_unref(pt->pt_outer.out_up_partial);
4829
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004830 // Using pt_outer from another partial.
4831 partial_unref(pt->pt_outer_partial);
4832
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004833 // Decrease the reference count for the context of a closure. If down
4834 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004835 if (pt->pt_funcstack != NULL)
4836 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004837 --pt->pt_funcstack->fs_refcount;
4838 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004839 }
4840
Bram Moolenaarddecc252016-04-06 22:59:37 +02004841 vim_free(pt);
4842}
4843
4844/*
4845 * Unreference a closure: decrement the reference count and free it when it
4846 * becomes zero.
4847 */
4848 void
4849partial_unref(partial_T *pt)
4850{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004851 if (pt != NULL)
4852 {
4853 if (--pt->pt_refcount <= 0)
4854 partial_free(pt);
4855
4856 // If the reference count goes down to one, the funcstack may be the
4857 // only reference and can be freed if no other partials reference it.
4858 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4859 funcstack_check_refcount(pt->pt_funcstack);
4860 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004861}
4862
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004863/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004864 * Return the next (unique) copy ID.
4865 * Used for serializing nested structures.
4866 */
4867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004868get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004869{
4870 current_copyID += COPYID_INC;
4871 return current_copyID;
4872}
4873
4874/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004875 * Garbage collection for lists and dictionaries.
4876 *
4877 * We use reference counts to be able to free most items right away when they
4878 * are no longer used. But for composite items it's possible that it becomes
4879 * unused while the reference count is > 0: When there is a recursive
4880 * reference. Example:
4881 * :let l = [1, 2, 3]
4882 * :let d = {9: l}
4883 * :let l[1] = d
4884 *
4885 * Since this is quite unusual we handle this with garbage collection: every
4886 * once in a while find out which lists and dicts are not referenced from any
4887 * variable.
4888 *
4889 * Here is a good reference text about garbage collection (refers to Python
4890 * but it applies to all reference-counting mechanisms):
4891 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004892 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004893
4894/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004895 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004896 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004897 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004898 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004899 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004900garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004901{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004902 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004903 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004904 buf_T *buf;
4905 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004906 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004907 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004908
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004909 if (!testing)
4910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004912 want_garbage_collect = FALSE;
4913 may_garbage_collect = FALSE;
4914 garbage_collect_at_exit = FALSE;
4915 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004916
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004917 // The execution stack can grow big, limit the size.
4918 if (exestack.ga_maxlen - exestack.ga_len > 500)
4919 {
4920 size_t new_len;
4921 char_u *pp;
4922 int n;
4923
4924 // Keep 150% of the current size, with a minimum of the growth size.
4925 n = exestack.ga_len / 2;
4926 if (n < exestack.ga_growsize)
4927 n = exestack.ga_growsize;
4928
4929 // Don't make it bigger though.
4930 if (exestack.ga_len + n < exestack.ga_maxlen)
4931 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004932 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004933 pp = vim_realloc(exestack.ga_data, new_len);
4934 if (pp == NULL)
4935 return FAIL;
4936 exestack.ga_maxlen = exestack.ga_len + n;
4937 exestack.ga_data = pp;
4938 }
4939 }
4940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // We advance by two because we add one for items referenced through
4942 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004943 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004944
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004945 /*
4946 * 1. Go through all accessible variables and mark all lists and dicts
4947 * with copyID.
4948 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // Don't free variables in the previous_funccal list unless they are only
4951 // referenced through previous_funccal. This must be first, because if
4952 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004956 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004959 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004960 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4961 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004964 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004965 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4966 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004967 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004968 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4969 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004970#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004971 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004972 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4973 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004974 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004975 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004976 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4977 NULL, NULL);
4978#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004981 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004982 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4983 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004985 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004987 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004991 abort = abort || set_ref_in_functions(copyID);
4992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004995
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004996 // funcstacks keep variables for closures
4997 abort = abort || set_ref_in_funcstacks(copyID);
4998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005000 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005001
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005002 // callbacks in buffers
5003 abort = abort || set_ref_in_buffers(copyID);
5004
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005005 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5006 abort = abort || set_ref_in_insexpand_funcs(copyID);
5007
5008 // 'operatorfunc' callback
5009 abort = abort || set_ref_in_opfunc(copyID);
5010
5011 // 'tagfunc' callback
5012 abort = abort || set_ref_in_tagfunc(copyID);
5013
5014 // 'imactivatefunc' and 'imstatusfunc' callbacks
5015 abort = abort || set_ref_in_im_funcs(copyID);
5016
Bram Moolenaar1dced572012-04-05 16:54:08 +02005017#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005018 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005019#endif
5020
Bram Moolenaardb913952012-06-29 12:54:53 +02005021#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005022 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005023#endif
5024
5025#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005026 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005027#endif
5028
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005029#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005030 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005031 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005032#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005033#ifdef FEAT_NETBEANS_INTG
5034 abort = abort || set_ref_in_nb_channel(copyID);
5035#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005036
Bram Moolenaare3188e22016-05-31 21:13:04 +02005037#ifdef FEAT_TIMERS
5038 abort = abort || set_ref_in_timer(copyID);
5039#endif
5040
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005041#ifdef FEAT_QUICKFIX
5042 abort = abort || set_ref_in_quickfix(copyID);
5043#endif
5044
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005045#ifdef FEAT_TERMINAL
5046 abort = abort || set_ref_in_term(copyID);
5047#endif
5048
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005049#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005050 abort = abort || set_ref_in_popups(copyID);
5051#endif
5052
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005053 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005054 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005055 /*
5056 * 2. Free lists and dictionaries that are not referenced.
5057 */
5058 did_free = free_unref_items(copyID);
5059
5060 /*
5061 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005062 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005063 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005065 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005066 else if (p_verbose > 0)
5067 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005068 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005069 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005070
5071 return did_free;
5072}
5073
5074/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005075 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005076 */
5077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005078free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005079{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005080 int did_free = FALSE;
5081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 // Let all "free" functions know that we are here. This means no
5083 // dictionaries, lists, channels or jobs are to be freed, because we will
5084 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005085 in_free_unref_items = TRUE;
5086
5087 /*
5088 * PASS 1: free the contents of the items. We don't free the items
5089 * themselves yet, so that it is possible to decrement refcount counters
5090 */
5091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005092 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005093 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005096 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005097
5098#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // Go through the list of jobs and free items without the copyID. This
5100 // must happen before doing channels, because jobs refer to channels, but
5101 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005105 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5106#endif
5107
5108 /*
5109 * PASS 2: free the items themselves.
5110 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005111 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005112 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005113
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005114#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 // Go through the list of jobs and free items without the copyID. This
5116 // must happen before doing channels, because jobs refer to channels, but
5117 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005118 free_unused_jobs(copyID, COPYID_MASK);
5119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005121 free_unused_channels(copyID, COPYID_MASK);
5122#endif
5123
5124 in_free_unref_items = FALSE;
5125
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005126 return did_free;
5127}
5128
5129/*
5130 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005131 * "list_stack" is used to add lists to be marked. Can be NULL.
5132 *
5133 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005134 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005135 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005136set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005137{
5138 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005139 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005140 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005141 hashtab_T *cur_ht;
5142 ht_stack_T *ht_stack = NULL;
5143 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005144
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005145 cur_ht = ht;
5146 for (;;)
5147 {
5148 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // Mark each item in the hashtab. If the item contains a hashtab
5151 // it is added to ht_stack, if it contains a list it is added to
5152 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005153 todo = (int)cur_ht->ht_used;
5154 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5155 if (!HASHITEM_EMPTY(hi))
5156 {
5157 --todo;
5158 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5159 &ht_stack, list_stack);
5160 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005161 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005162
5163 if (ht_stack == NULL)
5164 break;
5165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005167 cur_ht = ht_stack->ht;
5168 tempitem = ht_stack;
5169 ht_stack = ht_stack->prev;
5170 free(tempitem);
5171 }
5172
5173 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005174}
5175
Dominique Pelle748b3082022-01-08 12:41:16 +00005176#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5177 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005178/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005179 * Mark a dict and its items with "copyID".
5180 * Returns TRUE if setting references failed somehow.
5181 */
5182 int
5183set_ref_in_dict(dict_T *d, int copyID)
5184{
5185 if (d != NULL && d->dv_copyID != copyID)
5186 {
5187 d->dv_copyID = copyID;
5188 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5189 }
5190 return FALSE;
5191}
Dominique Pelle748b3082022-01-08 12:41:16 +00005192#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005193
5194/*
5195 * Mark a list and its items with "copyID".
5196 * Returns TRUE if setting references failed somehow.
5197 */
5198 int
5199set_ref_in_list(list_T *ll, int copyID)
5200{
5201 if (ll != NULL && ll->lv_copyID != copyID)
5202 {
5203 ll->lv_copyID = copyID;
5204 return set_ref_in_list_items(ll, copyID, NULL);
5205 }
5206 return FALSE;
5207}
5208
5209/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005210 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005211 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5212 *
5213 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005214 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005215 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005216set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005218 listitem_T *li;
5219 int abort = FALSE;
5220 list_T *cur_l;
5221 list_stack_T *list_stack = NULL;
5222 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005223
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005224 cur_l = l;
5225 for (;;)
5226 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005227 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 // Mark each item in the list. If the item contains a hashtab
5229 // it is added to ht_stack, if it contains a list it is added to
5230 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5232 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5233 ht_stack, &list_stack);
5234 if (list_stack == NULL)
5235 break;
5236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005238 cur_l = list_stack->list;
5239 tempitem = list_stack;
5240 list_stack = list_stack->prev;
5241 free(tempitem);
5242 }
5243
5244 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005245}
5246
5247/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005248 * Mark the partial in callback 'cb' with "copyID".
5249 */
5250 int
5251set_ref_in_callback(callback_T *cb, int copyID)
5252{
5253 typval_T tv;
5254
5255 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5256 return FALSE;
5257
5258 tv.v_type = VAR_PARTIAL;
5259 tv.vval.v_partial = cb->cb_partial;
5260 return set_ref_in_item(&tv, copyID, NULL, NULL);
5261}
5262
5263/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005264 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005265 * "list_stack" is used to add lists to be marked. Can be NULL.
5266 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5267 *
5268 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005271set_ref_in_item(
5272 typval_T *tv,
5273 int copyID,
5274 ht_stack_T **ht_stack,
5275 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005276{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005278
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005279 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005280 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005281 dict_T *dd = tv->vval.v_dict;
5282
Bram Moolenaara03f2332016-02-06 18:09:59 +01005283 if (dd != NULL && dd->dv_copyID != copyID)
5284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005286 dd->dv_copyID = copyID;
5287 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005288 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005289 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5290 }
5291 else
5292 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005293 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5294
Bram Moolenaara03f2332016-02-06 18:09:59 +01005295 if (newitem == NULL)
5296 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005297 else
5298 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005299 newitem->ht = &dd->dv_hashtab;
5300 newitem->prev = *ht_stack;
5301 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005303 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005304 }
5305 }
5306 else if (tv->v_type == VAR_LIST)
5307 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005308 list_T *ll = tv->vval.v_list;
5309
Bram Moolenaara03f2332016-02-06 18:09:59 +01005310 if (ll != NULL && ll->lv_copyID != copyID)
5311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005313 ll->lv_copyID = copyID;
5314 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005316 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005317 }
5318 else
5319 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005320 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5321
Bram Moolenaara03f2332016-02-06 18:09:59 +01005322 if (newitem == NULL)
5323 abort = TRUE;
5324 else
5325 {
5326 newitem->list = ll;
5327 newitem->prev = *list_stack;
5328 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005330 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005331 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005332 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005333 else if (tv->v_type == VAR_FUNC)
5334 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005335 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005336 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005337 else if (tv->v_type == VAR_PARTIAL)
5338 {
5339 partial_T *pt = tv->vval.v_partial;
5340 int i;
5341
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005342 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005343 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005344 // Didn't see this partial yet.
5345 pt->pt_copyID = copyID;
5346
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005347 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005348
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349 if (pt->pt_dict != NULL)
5350 {
5351 typval_T dtv;
5352
5353 dtv.v_type = VAR_DICT;
5354 dtv.vval.v_dict = pt->pt_dict;
5355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5356 }
5357
5358 for (i = 0; i < pt->pt_argc; ++i)
5359 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5360 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005361 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005362 }
5363 }
5364#ifdef FEAT_JOB_CHANNEL
5365 else if (tv->v_type == VAR_JOB)
5366 {
5367 job_T *job = tv->vval.v_job;
5368 typval_T dtv;
5369
5370 if (job != NULL && job->jv_copyID != copyID)
5371 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005372 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 if (job->jv_channel != NULL)
5374 {
5375 dtv.v_type = VAR_CHANNEL;
5376 dtv.vval.v_channel = job->jv_channel;
5377 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5378 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005379 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380 {
5381 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005382 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005383 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5384 }
5385 }
5386 }
5387 else if (tv->v_type == VAR_CHANNEL)
5388 {
5389 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005390 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005391 typval_T dtv;
5392 jsonq_T *jq;
5393 cbq_T *cq;
5394
5395 if (ch != NULL && ch->ch_copyID != copyID)
5396 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005397 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005398 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005399 {
5400 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5401 jq = jq->jq_next)
5402 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5403 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5404 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005405 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005406 {
5407 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005408 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005409 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5410 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005411 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005412 {
5413 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005414 dtv.vval.v_partial =
5415 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005416 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5417 }
5418 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005419 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005420 {
5421 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005422 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5424 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005425 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 {
5427 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005428 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5430 }
5431 }
5432 }
5433#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005434 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005435}
5436
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 * Return a string with the string representation of a variable.
5439 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005440 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005441 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005442 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005443 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005444 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005445 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5446 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005447 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005449 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005450echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451 typval_T *tv,
5452 char_u **tofree,
5453 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005454 int copyID,
5455 int echo_style,
5456 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005457 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005459 static int recurse = 0;
5460 char_u *r = NULL;
5461
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005463 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005464 if (!did_echo_string_emsg)
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // Only give this message once for a recursive call to avoid
5467 // flooding the user with errors. And stop iterating over lists
5468 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005469 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005470 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005472 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005473 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005474 }
5475 ++recurse;
5476
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 switch (tv->v_type)
5478 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005479 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005480 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005481 {
5482 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005483 r = tv->vval.v_string;
5484 if (r == NULL)
5485 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005486 }
5487 else
5488 {
5489 *tofree = string_quote(tv->vval.v_string, FALSE);
5490 r = *tofree;
5491 }
5492 break;
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005495 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005496 char_u buf[MAX_FUNC_NAME_LEN];
5497
5498 if (echo_style)
5499 {
LemonBoya5d35902022-04-29 21:15:02 +01005500 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5501 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005502 buf, MAX_FUNC_NAME_LEN);
5503 if (r == buf)
5504 {
5505 r = vim_strsave(buf);
5506 *tofree = r;
5507 }
5508 else
5509 *tofree = NULL;
5510 }
5511 else
5512 {
5513 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5514 : make_ufunc_name_readable(
5515 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5516 TRUE);
5517 r = *tofree;
5518 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005521
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005522 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005523 {
5524 partial_T *pt = tv->vval.v_partial;
5525 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005526 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005527 garray_T ga;
5528 int i;
5529 char_u *tf;
5530
5531 ga_init2(&ga, 1, 100);
5532 ga_concat(&ga, (char_u *)"function(");
5533 if (fname != NULL)
5534 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005535 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005536 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005537 && vim_isupper(fname[1]))
5538 {
5539 ga_concat(&ga, (char_u *)"'g:");
5540 ga_concat(&ga, fname + 1);
5541 }
5542 else
5543 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005544 vim_free(fname);
5545 }
5546 if (pt != NULL && pt->pt_argc > 0)
5547 {
5548 ga_concat(&ga, (char_u *)", [");
5549 for (i = 0; i < pt->pt_argc; ++i)
5550 {
5551 if (i > 0)
5552 ga_concat(&ga, (char_u *)", ");
5553 ga_concat(&ga,
5554 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5555 vim_free(tf);
5556 }
5557 ga_concat(&ga, (char_u *)"]");
5558 }
5559 if (pt != NULL && pt->pt_dict != NULL)
5560 {
5561 typval_T dtv;
5562
5563 ga_concat(&ga, (char_u *)", ");
5564 dtv.v_type = VAR_DICT;
5565 dtv.vval.v_dict = pt->pt_dict;
5566 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5567 vim_free(tf);
5568 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005569 // terminate with ')' and a NUL
5570 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005571
5572 *tofree = ga.ga_data;
5573 r = *tofree;
5574 break;
5575 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005576
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005577 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005578 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005579 break;
5580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005582 if (tv->vval.v_list == NULL)
5583 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005584 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005585 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005586 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005587 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005588 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5589 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005590 {
5591 *tofree = NULL;
5592 r = (char_u *)"[...]";
5593 }
5594 else
5595 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005596 int old_copyID = tv->vval.v_list->lv_copyID;
5597
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005598 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005599 *tofree = list2string(tv, copyID, restore_copyID);
5600 if (restore_copyID)
5601 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602 r = *tofree;
5603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005605
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005607 if (tv->vval.v_dict == NULL)
5608 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005609 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005610 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005611 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005612 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005613 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5614 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005615 {
5616 *tofree = NULL;
5617 r = (char_u *)"{...}";
5618 }
5619 else
5620 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005621 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005622
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005623 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005624 *tofree = dict2string(tv, copyID, restore_copyID);
5625 if (restore_copyID)
5626 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627 r = *tofree;
5628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005632 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005633 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005635 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005636 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005637 break;
5638
Bram Moolenaar835dc632016-02-07 14:27:38 +01005639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005640 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005641#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005643 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5644 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005645 if (composite_val)
5646 {
5647 *tofree = string_quote(r, FALSE);
5648 r = *tofree;
5649 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005650#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005652
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005653 case VAR_INSTR:
5654 *tofree = NULL;
5655 r = (char_u *)"instructions";
5656 break;
5657
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005658 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005659#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005660 *tofree = NULL;
5661 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5662 r = numbuf;
5663 break;
5664#endif
5665
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005666 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005667 case VAR_SPECIAL:
5668 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005669 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005670 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005672
Bram Moolenaar8502c702014-06-17 12:51:16 +02005673 if (--recurse == 0)
5674 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005675 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676}
5677
5678/*
5679 * Return a string with the string representation of a variable.
5680 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5681 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005682 * Does not put quotes around strings, as ":echo" displays values.
5683 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5684 * May return NULL.
5685 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005687echo_string(
5688 typval_T *tv,
5689 char_u **tofree,
5690 char_u *numbuf,
5691 int copyID)
5692{
5693 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5694}
5695
5696/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005697 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5698 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005699 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005700 */
5701 int
5702buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5703{
5704 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005705 char_u *t;
5706 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005707
5708 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5709 return -1;
5710
5711 if (lnum > buf->b_ml.ml_line_count)
5712 lnum = buf->b_ml.ml_line_count;
5713
5714 str = ml_get_buf(buf, lnum, FALSE);
5715 if (str == NULL)
5716 return -1;
5717
5718 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005719 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005720
Bram Moolenaar91458462021-01-13 20:08:38 +01005721 // count the number of characters
5722 t = str;
5723 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5724 t += mb_ptr2len(t);
5725
5726 // In insert mode, when the cursor is at the end of a non-empty line,
5727 // byteidx points to the NUL character immediately past the end of the
5728 // string. In this case, add one to the character count.
5729 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5730 count++;
5731
5732 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005733}
5734
5735/*
5736 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005737 * byte index. Works only for loaded buffers. Returns -1 on failure.
5738 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005739 */
5740 int
5741buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5742{
5743 char_u *str;
5744 char_u *t;
5745
5746 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5747 return -1;
5748
5749 if (lnum > buf->b_ml.ml_line_count)
5750 lnum = buf->b_ml.ml_line_count;
5751
5752 str = ml_get_buf(buf, lnum, FALSE);
5753 if (str == NULL)
5754 return -1;
5755
5756 // Convert the character offset to a byte offset
5757 t = str;
5758 while (*t != NUL && --charidx > 0)
5759 t += mb_ptr2len(t);
5760
Bram Moolenaar91458462021-01-13 20:08:38 +01005761 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005762}
5763
5764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005766 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005768 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005769var2fpos(
5770 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005771 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005772 int *fnum, // set to fnum for '0, 'A, etc.
5773 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005775 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005777 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005779 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005780 if (varp->v_type == VAR_LIST)
5781 {
5782 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005783 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005784 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005785 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005786
5787 l = varp->vval.v_list;
5788 if (l == NULL)
5789 return NULL;
5790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005792 pos.lnum = list_find_nr(l, 0L, &error);
5793 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005794 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005795 if (charcol)
5796 len = (long)mb_charlen(ml_get(pos.lnum));
5797 else
5798 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005799
Bram Moolenaarec65d772020-08-20 22:29:12 +02005800 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005802 li = list_find(l, 1L);
5803 if (li != NULL && li->li_tv.v_type == VAR_STRING
5804 && li->li_tv.vval.v_string != NULL
5805 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005806 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005807 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005808 }
5809 else
5810 {
5811 pos.col = list_find_nr(l, 1L, &error);
5812 if (error)
5813 return NULL;
5814 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005816 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005817 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005818 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005819 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005821 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005822 pos.coladd = list_find_nr(l, 2L, &error);
5823 if (error)
5824 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005825
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005826 return &pos;
5827 }
5828
Bram Moolenaarc5809432021-03-27 21:23:30 +01005829 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5830 return NULL;
5831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005832 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005833 if (name == NULL)
5834 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005835
5836 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005837 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005838 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005839 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005840 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005841 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005842 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005843 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005844 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005845 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005846 pos = VIsual;
5847 else
5848 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005849 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005850 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005851 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005853 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005854 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5856 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005857 pos = *pp;
5858 }
5859 if (pos.lnum != 0)
5860 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005861 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005862 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5863 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005865
Bram Moolenaara5525202006-03-02 22:52:09 +00005866 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005867
Bram Moolenaar477933c2007-07-17 14:32:23 +00005868 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005869 {
5870 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005871 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005872 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005873 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005874 // In silent Ex mode topline is zero, but that's not a valid line
5875 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005876 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005877 return &pos;
5878 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005880 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005881 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005882 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005883 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005884 return &pos;
5885 }
5886 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005889 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 pos.lnum = curbuf->b_ml.ml_line_count;
5892 pos.col = 0;
5893 }
5894 else
5895 {
5896 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005897 if (charcol)
5898 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5899 else
5900 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 return &pos;
5903 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005904 if (in_vim9script())
5905 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 return NULL;
5907}
5908
5909/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005910 * Convert list in "arg" into a position and optional file number.
5911 * When "fnump" is NULL there is no file number, only 3 items.
5912 * Note that the column is passed on as-is, the caller may want to decrement
5913 * it to use 1 for the first column.
5914 * Return FAIL when conversion is not possible, doesn't check the position for
5915 * validity.
5916 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005918list2fpos(
5919 typval_T *arg,
5920 pos_T *posp,
5921 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005922 colnr_T *curswantp,
5923 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005924{
5925 list_T *l = arg->vval.v_list;
5926 long i = 0;
5927 long n;
5928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005929 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5930 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005931 if (arg->v_type != VAR_LIST
5932 || l == NULL
5933 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005934 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005935 return FAIL;
5936
5937 if (fnump != NULL)
5938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005939 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005940 if (n < 0)
5941 return FAIL;
5942 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005944 *fnump = n;
5945 }
5946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005947 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005948 if (n < 0)
5949 return FAIL;
5950 posp->lnum = n;
5951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005952 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005953 if (n < 0)
5954 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005955 // If character position is specified, then convert to byte position
5956 if (charcol)
5957 {
5958 buf_T *buf;
5959
5960 // Get the text for the specified line in a loaded buffer
5961 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5962 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5963 return FAIL;
5964
Bram Moolenaar91458462021-01-13 20:08:38 +01005965 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005966 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005967 posp->col = n;
5968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005970 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005971 posp->coladd = 0;
5972 else
5973 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005974
Bram Moolenaar493c1782014-05-28 14:34:46 +02005975 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005976 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005977
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005978 return OK;
5979}
5980
5981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 * Get the length of an environment variable name.
5983 * Advance "arg" to the first character after the name.
5984 * Return 0 for error.
5985 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 char_u *p;
5990 int len;
5991
5992 for (p = *arg; vim_isIDc(*p); ++p)
5993 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 return 0;
5996
5997 len = (int)(p - *arg);
5998 *arg = p;
5999 return len;
6000}
6001
6002/*
6003 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006004 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 * Return 0 if something is wrong.
6006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 char_u *p;
6011 int len;
6012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006013 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006015 {
6016 if (*p == ':')
6017 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006018 // "s:" is start of "s:var", but "n:" is not and can be used in
6019 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006020 len = (int)(p - *arg);
6021 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6022 || len > 1)
6023 break;
6024 }
6025 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006026 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 return 0;
6028
6029 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006030 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031
6032 return len;
6033}
6034
6035/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006036 * Get the length of the name of a variable or function.
6037 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006039 * Return -1 if curly braces expansion failed.
6040 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * If the name contains 'magic' {}'s, expand them and return the
6042 * expanded name in an allocated string via 'alias' - caller must free.
6043 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045get_name_len(
6046 char_u **arg,
6047 char_u **alias,
6048 int evaluate,
6049 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 char_u *p;
6053 char_u *expr_start;
6054 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006056 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6059 && (*arg)[2] == (int)KE_SNR)
6060 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006061 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 *arg += 3;
6063 return get_id_len(arg) + 3;
6064 }
6065 len = eval_fname_script(*arg);
6066 if (len > 0)
6067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 *arg += len;
6070 }
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006075 p = find_name_end(*arg, &expr_start, &expr_end,
6076 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (expr_start != NULL)
6078 {
6079 char_u *temp_string;
6080
6081 if (!evaluate)
6082 {
6083 len += (int)(p - *arg);
6084 *arg = skipwhite(p);
6085 return len;
6086 }
6087
6088 /*
6089 * Include any <SID> etc in the expanded string:
6090 * Thus the -len here.
6091 */
6092 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6093 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006094 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 *alias = temp_string;
6096 *arg = skipwhite(p);
6097 return (int)STRLEN(temp_string);
6098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006101 // Only give an error when there is something, otherwise it will be
6102 // reported at a higher level.
6103 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006104 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 return len;
6107}
6108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109/*
6110 * Find the end of a variable or function name, taking care of magic braces.
6111 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6112 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006113 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006114 * Return a pointer to just after the name. Equal to "arg" if there is no
6115 * valid name.
6116 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006117 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118find_name_end(
6119 char_u *arg,
6120 char_u **expr_start,
6121 char_u **expr_end,
6122 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006124 int mb_nest = 0;
6125 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006127 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006128 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006130 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006132 *expr_start = NULL;
6133 *expr_end = NULL;
6134 }
6135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006137 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6138 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006139 return arg;
6140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006141 for (p = arg; *p != NUL
6142 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006143 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006144 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006145 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006146 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006147 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006148 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006149 if (*p == '\'')
6150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006151 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006152 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006153 ;
6154 if (*p == NUL)
6155 break;
6156 }
6157 else if (*p == '"')
6158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006160 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006161 if (*p == '\\' && p[1] != NUL)
6162 ++p;
6163 if (*p == NUL)
6164 break;
6165 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006166 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006168 // "s:" is start of "s:var", but "n:" is not and can be used in
6169 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006170 len = (int)(p - arg);
6171 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006172 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006173 break;
6174 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006176 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006178 if (*p == '[')
6179 ++br_nest;
6180 else if (*p == ']')
6181 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006183
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006184 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006186 if (*p == '{')
6187 {
6188 mb_nest++;
6189 if (expr_start != NULL && *expr_start == NULL)
6190 *expr_start = p;
6191 }
6192 else if (*p == '}')
6193 {
6194 mb_nest--;
6195 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6196 *expr_end = p;
6197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
6200
6201 return p;
6202}
6203
6204/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006205 * Expands out the 'magic' {}'s in a variable/function name.
6206 * Note that this can call itself recursively, to deal with
6207 * constructs like foo{bar}{baz}{bam}
6208 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6209 * "in_start" ^
6210 * "expr_start" ^
6211 * "expr_end" ^
6212 * "in_end" ^
6213 *
6214 * Returns a new allocated string, which the caller must free.
6215 * Returns NULL for failure.
6216 */
6217 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006218make_expanded_name(
6219 char_u *in_start,
6220 char_u *expr_start,
6221 char_u *expr_end,
6222 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006223{
6224 char_u c1;
6225 char_u *retval = NULL;
6226 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006227
6228 if (expr_end == NULL || in_end == NULL)
6229 return NULL;
6230 *expr_start = NUL;
6231 *expr_end = NUL;
6232 c1 = *in_end;
6233 *in_end = NUL;
6234
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006235 temp_result = eval_to_string(expr_start + 1, FALSE);
6236 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006237 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006238 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6239 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006240 if (retval != NULL)
6241 {
6242 STRCPY(retval, in_start);
6243 STRCAT(retval, temp_result);
6244 STRCAT(retval, expr_end + 1);
6245 }
6246 }
6247 vim_free(temp_result);
6248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006250 *expr_start = '{';
6251 *expr_end = '}';
6252
6253 if (retval != NULL)
6254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006255 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006256 if (expr_start != NULL)
6257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006258 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006259 temp_result = make_expanded_name(retval, expr_start,
6260 expr_end, temp_result);
6261 vim_free(retval);
6262 retval = temp_result;
6263 }
6264 }
6265
6266 return retval;
6267}
6268
6269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006271 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006274eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006276 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006277}
6278
6279/*
6280 * Return TRUE if character "c" can be used as the first character in a
6281 * variable or function name (excluding '{' and '}').
6282 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006285{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006286 return ASCII_ISALPHA(c) || c == '_';
6287}
6288
6289/*
6290 * Return TRUE if character "c" can be used as the first character of a
6291 * dictionary key.
6292 */
6293 int
6294eval_isdictc(int c)
6295{
6296 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297}
6298
6299/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006300 * Handle:
6301 * - expr[expr], expr[expr:expr] subscript
6302 * - ".name" lookup
6303 * - function call with Funcref variable: func(expr)
6304 * - method call: var->method()
6305 *
6306 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006307 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006308 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310handle_subscript(
6311 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006312 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006313 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006314 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006315 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006316{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006317 int evaluate = evalarg != NULL
6318 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006319 int ret = OK;
6320 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006321 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006322 int getnext;
6323 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006324
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006325 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006326 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006327 // When at the end of the line and ".name" or "->{" or "->X" follows in
6328 // the next line then consume the line break.
6329 p = eval_next_non_blank(*arg, evalarg, &getnext);
6330 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006331 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006332 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6333 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6334 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006335 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006336 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006337 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006338 check_white = FALSE;
6339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006340
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006341 if (rettv->v_type == VAR_ANY)
6342 {
6343 char_u *exp_name;
6344 int cc;
6345 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006346 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006347 type_T *type;
6348
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006349 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006350 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006351 if (**arg != '.')
6352 {
6353 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006354 semsg(_(e_expected_dot_after_name_str),
6355 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006356 ret = FAIL;
6357 break;
6358 }
6359 ++*arg;
6360 if (IS_WHITE_OR_NUL(**arg))
6361 {
6362 if (verbose)
6363 emsg(_(e_no_white_space_allowed_after_dot));
6364 ret = FAIL;
6365 break;
6366 }
6367
6368 // isolate the name
6369 exp_name = *arg;
6370 while (eval_isnamec(**arg))
6371 ++*arg;
6372 cc = **arg;
6373 **arg = NUL;
6374
6375 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006376 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006377 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006378
6379 if (idx < 0 && ufunc == NULL)
6380 {
6381 ret = FAIL;
6382 break;
6383 }
6384 if (idx >= 0)
6385 {
6386 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6387 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6388
6389 copy_tv(sv->sv_tv, rettv);
6390 }
6391 else
6392 {
6393 rettv->v_type = VAR_FUNC;
6394 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6395 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006396 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006397 }
6398
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006399 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6400 || rettv->v_type == VAR_PARTIAL))
6401 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006402 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006403 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6404 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006405
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006406 // Stop the expression evaluation when immediately aborting on
6407 // error, or when an interrupt occurred or an exception was thrown
6408 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006409 if (aborting())
6410 {
6411 if (ret == OK)
6412 clear_tv(rettv);
6413 ret = FAIL;
6414 }
6415 dict_unref(selfdict);
6416 selfdict = NULL;
6417 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006418 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006419 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006420 if (in_vim9script())
6421 *arg = skipwhite(p + 2);
6422 else
6423 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006424 if (ret == OK)
6425 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006426 if (VIM_ISWHITE(**arg))
6427 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006428 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006429 ret = FAIL;
6430 }
6431 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006432 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006433 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006434 else
6435 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006436 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006437 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006438 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006439 // "." is ".name" lookup when we found a dict or when evaluating and
6440 // scriptversion is at least 2, where string concatenation is "..".
6441 else if (**arg == '['
6442 || (**arg == '.' && (rettv->v_type == VAR_DICT
6443 || (!evaluate
6444 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006445 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006446 {
6447 dict_unref(selfdict);
6448 if (rettv->v_type == VAR_DICT)
6449 {
6450 selfdict = rettv->vval.v_dict;
6451 if (selfdict != NULL)
6452 ++selfdict->dv_refcount;
6453 }
6454 else
6455 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006456 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006457 {
6458 clear_tv(rettv);
6459 ret = FAIL;
6460 }
6461 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006462 else
6463 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006464 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006466 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6467 // Don't do this when "Func" is already a partial that was bound
6468 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006469 if (selfdict != NULL
6470 && (rettv->v_type == VAR_FUNC
6471 || (rettv->v_type == VAR_PARTIAL
6472 && (rettv->vval.v_partial->pt_auto
6473 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006475
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006476 dict_unref(selfdict);
6477 return ret;
6478}
6479
6480/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 * Make a copy of an item.
6482 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006483 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6485 * reference to an already copied list/dict can be used.
6486 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489item_copy(
6490 typval_T *from,
6491 typval_T *to,
6492 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006493 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006494 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495{
6496 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006498
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006500 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006501 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006503 }
6504 ++recurse;
6505
6506 switch (from->v_type)
6507 {
6508 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006509 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 case VAR_STRING:
6511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006512 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006513 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006514 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006516 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006517 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 copy_tv(from, to);
6519 break;
6520 case VAR_LIST:
6521 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006522 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523 if (from->vval.v_list == NULL)
6524 to->vval.v_list = NULL;
6525 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6526 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006527 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006528 to->vval.v_list = from->vval.v_list->lv_copylist;
6529 ++to->vval.v_list->lv_refcount;
6530 }
6531 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006532 to->vval.v_list = list_copy(from->vval.v_list,
6533 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 if (to->vval.v_list == NULL)
6535 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006537 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006539 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540 case VAR_DICT:
6541 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006542 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 if (from->vval.v_dict == NULL)
6544 to->vval.v_dict = NULL;
6545 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006547 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6549 ++to->vval.v_dict->dv_refcount;
6550 }
6551 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006552 to->vval.v_dict = dict_copy(from->vval.v_dict,
6553 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 if (to->vval.v_dict == NULL)
6555 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006557 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006558 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006560 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006562 }
6563 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006565}
6566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 void
6568echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6569{
6570 char_u *tofree;
6571 char_u numbuf[NUMBUFLEN];
6572 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6573
6574 if (*atstart)
6575 {
6576 *atstart = FALSE;
6577 // Call msg_start() after eval1(), evaluating the expression
6578 // may cause a message to appear.
6579 if (with_space)
6580 {
6581 // Mark the saved text as finishing the line, so that what
6582 // follows is displayed on a new line when scrolling back
6583 // at the more prompt.
6584 msg_sb_eol();
6585 msg_start();
6586 }
6587 }
6588 else if (with_space)
6589 msg_puts_attr(" ", echo_attr);
6590
6591 if (p != NULL)
6592 for ( ; *p != NUL && !got_int; ++p)
6593 {
6594 if (*p == '\n' || *p == '\r' || *p == TAB)
6595 {
6596 if (*p != TAB && *needclr)
6597 {
6598 // remove any text still there from the command
6599 msg_clr_eos();
6600 *needclr = FALSE;
6601 }
6602 msg_putchar_attr(*p, echo_attr);
6603 }
6604 else
6605 {
6606 if (has_mbyte)
6607 {
6608 int i = (*mb_ptr2len)(p);
6609
6610 (void)msg_outtrans_len_attr(p, i, echo_attr);
6611 p += i - 1;
6612 }
6613 else
6614 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6615 }
6616 }
6617 vim_free(tofree);
6618}
6619
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 * ":echo expr1 ..." print each argument separated with a space, add a
6622 * newline at the end.
6623 * ":echon expr1 ..." print each argument plain.
6624 */
6625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006630 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 int needclr = TRUE;
6632 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006633 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006634 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006635 evalarg_T evalarg;
6636
Bram Moolenaare707c882020-07-01 13:07:37 +02006637 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 if (eap->skip)
6640 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006641 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006643 // If eval1() causes an error message the text from the command may
6644 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006645 need_clr_eos = needclr;
6646
Bram Moolenaar68db9962021-05-09 23:19:22 +02006647 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006648 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
6650 /*
6651 * Report the invalid expression unless the expression evaluation
6652 * has been cancelled due to an aborting error, an interrupt, or an
6653 * exception.
6654 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006655 if (!aborting() && did_emsg == did_emsg_before
6656 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006657 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006658 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 break;
6660 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006661 need_clr_eos = FALSE;
6662
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006664 {
6665 if (rettv.v_type == VAR_VOID)
6666 {
6667 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6668 break;
6669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006671 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 arg = skipwhite(arg);
6675 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006676 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006677 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 if (eap->skip)
6680 --emsg_skip;
6681 else
6682 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006683 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (needclr)
6685 msg_clr_eos();
6686 if (eap->cmdidx == CMD_echo)
6687 msg_end();
6688 }
6689}
6690
6691/*
6692 * ":echohl {name}".
6693 */
6694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006695ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006697 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
6700/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006701 * Returns the :echo attribute
6702 */
6703 int
6704get_echo_attr(void)
6705{
6706 return echo_attr;
6707}
6708
6709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 * ":execute expr1 ..." execute the result of an expression.
6711 * ":echomsg expr1 ..." Print a message
6712 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006713 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 * Each gets spaces around each argument and a newline at the end for
6715 * echo commands
6716 */
6717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006718ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006721 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 int ret = OK;
6723 char_u *p;
6724 garray_T ga;
6725 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006726 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 ga_init2(&ga, 1, 80);
6729
6730 if (eap->skip)
6731 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006732 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006734 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006735 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 if (!eap->skip)
6739 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006740 char_u buf[NUMBUFLEN];
6741
6742 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006743 {
6744 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6745 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006746 semsg(_(e_using_invalid_value_as_string_str),
6747 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006748 p = NULL;
6749 }
6750 else
6751 p = tv_get_string_buf(&rettv, buf);
6752 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006753 else
6754 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006755 if (p == NULL)
6756 {
6757 clear_tv(&rettv);
6758 ret = FAIL;
6759 break;
6760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 len = (int)STRLEN(p);
6762 if (ga_grow(&ga, len + 2) == FAIL)
6763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006764 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 ret = FAIL;
6766 break;
6767 }
6768 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 ga.ga_len += len;
6772 }
6773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 arg = skipwhite(arg);
6776 }
6777
6778 if (ret != FAIL && ga.ga_data != NULL)
6779 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006780 // use the first line of continuation lines for messages
6781 SOURCING_LNUM = start_lnum;
6782
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006783 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006785 // Mark the already saved text as finishing the line, so that what
6786 // follows is displayed on a new line when scrolling back at the
6787 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006788 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006789 }
6790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006792 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006793 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006794 out_flush();
6795 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006796 else if (eap->cmdidx == CMD_echoconsole)
6797 {
6798 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6799 ui_write((char_u *)"\r\n", 2, TRUE);
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 else if (eap->cmdidx == CMD_echoerr)
6802 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006803 int save_did_emsg = did_emsg;
6804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006805 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006806 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (!force_abort)
6808 did_emsg = save_did_emsg;
6809 }
6810 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006811 {
6812 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6813
6814 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6815 sticky_cmdmod_flags = cmdmod.cmod_flags
6816 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 do_cmdline((char_u *)ga.ga_data,
6818 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006819 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822
6823 ga_clear(&ga);
6824
6825 if (eap->skip)
6826 --emsg_skip;
6827
Bram Moolenaar63b91732021-08-05 20:40:03 +02006828 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829}
6830
6831/*
6832 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6833 * "arg" points to the "&" or '+' when called, to "option" when returning.
6834 * Returns NULL when no option name found. Otherwise pointer to the char
6835 * after the option name.
6836 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006837 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006838find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
6840 char_u *p = *arg;
6841
6842 ++p;
6843 if (*p == 'g' && p[1] == ':')
6844 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006845 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 p += 2;
6847 }
6848 else if (*p == 'l' && p[1] == ':')
6849 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006850 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 p += 2;
6852 }
6853 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006854 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 if (!ASCII_ISALPHA(*p))
6857 return NULL;
6858 *arg = p;
6859
6860 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006861 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 else
6863 while (ASCII_ISALPHA(*p))
6864 ++p;
6865 return p;
6866}
6867
6868/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006869 * Display script name where an item was last set.
6870 * Should only be invoked when 'verbose' is non-zero.
6871 */
6872 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006873last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006874{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006875 char_u *p;
6876
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006877 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006878 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006879 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006880 if (p != NULL)
6881 {
6882 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006883 msg_puts(_("\n\tLast set from "));
6884 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006885 if (script_ctx.sc_lnum > 0)
6886 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006887 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006888 msg_outnum((long)script_ctx.sc_lnum);
6889 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006890 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006891 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006892 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006893 }
6894}
6895
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006896#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898/*
6899 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006900 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 * "flags" can be "g" to do a global substitute.
6902 * Returns an allocated string, NULL for error.
6903 */
6904 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006905do_string_sub(
6906 char_u *str,
6907 char_u *pat,
6908 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006909 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006910 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 int sublen;
6913 regmatch_T regmatch;
6914 int i;
6915 int do_all;
6916 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006917 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 garray_T ga;
6919 char_u *ret;
6920 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006921 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006925 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 ga_init2(&ga, 1, 200);
6928
6929 do_all = (flags[0] == 'g');
6930
6931 regmatch.rm_ic = p_ic;
6932 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6933 if (regmatch.regprog != NULL)
6934 {
6935 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006936 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006939 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006940 if (regmatch.startp[0] == regmatch.endp[0])
6941 {
6942 if (zero_width == regmatch.startp[0])
6943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006944 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006945 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006946 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6947 (size_t)i);
6948 ga.ga_len += i;
6949 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006950 continue;
6951 }
6952 zero_width = regmatch.startp[0];
6953 }
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /*
6956 * Get some space for a temporary buffer to do the substitution
6957 * into. It will contain:
6958 * - The text up to where the match is.
6959 * - The substituted text.
6960 * - The text after the match.
6961 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006962 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006963 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6965 {
6966 ga_clear(&ga);
6967 break;
6968 }
6969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006970 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 i = (int)(regmatch.startp[0] - tail);
6972 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006973 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006974 (void)vim_regsub(&regmatch, sub, expr,
6975 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6976 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006978 tail = regmatch.endp[0];
6979 if (*tail == NUL)
6980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (!do_all)
6982 break;
6983 }
6984
6985 if (ga.ga_data != NULL)
6986 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6987
Bram Moolenaar473de612013-06-08 18:19:48 +02006988 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 }
6990
6991 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6992 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006993 if (p_cpo == empty_option)
6994 p_cpo = save_cpo;
6995 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006997 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006998 // If it's still empty it was changed and restored, need to restore in
6999 // the complicated way.
7000 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007001 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007002 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 return ret;
7006}