blob: b8aa79fe4dfafbe5966e90c0cd2d6186e991adb8 [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;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100697 int save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000698
699 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100700 if (evalarg != NULL)
701 {
702 // need to evaluate this to get an import, like in "a.Func"
703 save_flags = evalarg->eval_flags;
704 evalarg->eval_flags |= EVAL_EVALUATE;
705 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100706 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100707 {
708 dictitem_T *v;
709
710 // If <SID>VarName was used it would not be found, try another way.
711 v = find_var_also_in_script(name, NULL, FALSE);
712 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100713 {
714 name = NULL;
715 goto theend;
716 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100717 copy_tv(&v->di_tv, &ref);
718 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000719 if (*skipwhite(*arg) != NUL)
720 {
721 if (verbose)
722 semsg(_(e_trailing_characters_str), *arg);
723 name = NULL;
724 }
725 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
726 {
727 name = ref.vval.v_string;
728 ref.vval.v_string = NULL;
729 *tofree = name;
730 }
731 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
732 {
733 if (ref.vval.v_partial->pt_argc > 0
734 || ref.vval.v_partial->pt_dict != NULL)
735 {
736 if (verbose)
737 emsg(_(e_cannot_use_partial_here));
738 name = NULL;
739 }
740 else
741 {
742 name = vim_strsave(partial_name(ref.vval.v_partial));
743 *tofree = name;
744 }
745 }
746 else
747 {
748 if (verbose)
749 semsg(_(e_not_callable_type_str), name);
750 name = NULL;
751 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100752
753theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000754 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100755 if (evalarg != NULL)
756 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000757 return name;
758}
759
760/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100761 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200762 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
763 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100767call_vim_function(
768 char_u *func,
769 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200770 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200771 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000773 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200774 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000775 char_u *arg;
776 char_u *name;
777 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000778 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200781 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000782 funcexe.fe_firstline = curwin->w_cursor.lnum;
783 funcexe.fe_lastline = curwin->w_cursor.lnum;
784 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000785
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000786 // The name might be "import.Func" or "Funcref". We don't know, we need to
787 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000788 // autoload script has errors. Guess that when there is a dot in the name
789 // showing errors is the right choice.
790 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000791 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000792 if (ignore_errors)
793 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000794 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000795 if (ignore_errors)
796 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000797 if (name == NULL)
798 name = func;
799
800 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
801
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000802 if (ret == FAIL)
803 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000804 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000805
806 return ret;
807}
808
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100809/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100810 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000811 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
812 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000813 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 */
815 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816call_func_retstr(
817 char_u *func,
818 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200819 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000820{
821 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000822 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000823
Bram Moolenaarded27a12018-08-01 19:06:03 +0200824 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000825 return NULL;
826
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100827 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return retval;
830}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000831
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000832/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100833 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000834 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000835 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836 */
837 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838call_func_retlist(
839 char_u *func,
840 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200841 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000842{
843 typval_T rettv;
844
Bram Moolenaarded27a12018-08-01 19:06:03 +0200845 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000846 return NULL;
847
848 if (rettv.v_type != VAR_LIST)
849 {
850 clear_tv(&rettv);
851 return NULL;
852 }
853
854 return rettv.vval.v_list;
855}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaare70dd112022-01-21 16:31:11 +0000857#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100859 * Evaluate "arg", which is 'foldexpr'.
860 * Note: caller must set "curwin" to match "arg".
861 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
862 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 */
864 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000865eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000867 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200869 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000871 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000872 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
873 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaare70dd112022-01-21 16:31:11 +0000875 arg = wp->w_p_fde;
876 current_sctx = wp->w_p_script_ctx[WV_FDE];
877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000879 if (use_sandbox)
880 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100881 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200883 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 retval = 0;
885 else
886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100887 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 if (tv.v_type == VAR_NUMBER)
889 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000890 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 retval = 0;
892 else
893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100894 // If the result is a string, check if there is a non-digit before
895 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (!VIM_ISDIGIT(*s) && *s != '-')
898 *cp = *s++;
899 retval = atol((char *)s);
900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000901 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000904 if (use_sandbox)
905 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100906 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200907 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000908 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200910 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912#endif
913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 * Get an lval: variable, Dict item or List item that can be assigned a value
916 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
917 * "name.key", "name.key[expr]" etc.
918 * Indexing only works if "name" is an existing List or Dictionary.
919 * "name" points to the start of the name.
920 * If "rettv" is not NULL it points to the value to be assigned.
921 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
922 * wrong; must end in space or cmd separator.
923 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100924 * flags:
925 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100926 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100927 * GLV_NO_AUTOLOAD: do not use script autoloading
928 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000930 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000932 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200933 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100934get_lval(
935 char_u *name,
936 typval_T *rettv,
937 lval_T *lp,
938 int unlet,
939 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100940 int flags, // GLV_ values
941 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 char_u *expr_start, *expr_end;
945 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 dictitem_T *v;
947 typval_T var1;
948 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100952 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100953 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100954 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000955 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100957 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200958 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100960 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100962 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200964 lp->ll_name_end = find_name_end(name, NULL, NULL,
965 FNE_INCL_BR | fne_flags);
966 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 }
968
Bram Moolenaara749a422022-02-12 19:52:25 +0000969 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000970 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000971 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
972 {
973 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
974 return NULL;
975 }
976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100977 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000978 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 if (expr_start != NULL)
981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100982 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100983 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 && *p != '[' && *p != '.')
985 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000986 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
988 }
989
990 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
991 if (lp->ll_exp_name == NULL)
992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100993 // Report an invalid expression in braces, unless the
994 // expression evaluation has been cancelled due to an
995 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 if (!aborting() && !quiet)
997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000998 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000999 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 return NULL;
1001 }
1002 }
1003 lp->ll_name = lp->ll_exp_name;
1004 }
1005 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 lp->ll_name = name;
1008
Bram Moolenaar4525a572022-02-13 11:57:33 +00001009 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001011 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001012 // However, "g:[key]" is indexing a dictionary.
1013 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001014 {
1015 --p;
1016 lp->ll_name_end = p;
1017 }
1018 if (*p == ':')
1019 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001020 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001021
1022 if (tp == p + 1 && !quiet)
1023 {
1024 semsg(_(e_white_space_required_after_str_str), ":", p);
1025 return NULL;
1026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001028 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001029 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001030 semsg(_(e_using_type_not_in_script_context_str), p);
1031 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001032 }
1033
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001034 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001035 lp->ll_type = parse_type(&tp,
1036 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1037 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001038 if (lp->ll_type == NULL && !quiet)
1039 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001040 lp->ll_name_end = tp;
1041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 }
1043 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001044 if (lp->ll_name == NULL)
1045 return p;
1046
Bram Moolenaar62aec932022-01-29 21:45:34 +00001047 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001048 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001049 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001050
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001051 if (import != NULL)
1052 {
1053 ufunc_T *ufunc;
1054 type_T *type;
1055
Bram Moolenaar753885b2022-08-24 16:30:36 +01001056 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001057 lp->ll_sid = import->imp_sid;
1058 lp->ll_name = skipwhite(p + 1);
1059 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1060 lp->ll_name_end = p;
1061
1062 // check the item is exported
1063 cc = *p;
1064 *p = NUL;
1065 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001066 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001067 {
1068 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001069 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001070 }
1071 *p = cc;
1072 }
1073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001075 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001076 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return p;
1078
Bram Moolenaar4525a572022-02-13 11:57:33 +00001079 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001080 {
1081 // using local variable
1082 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001083 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001084 }
1085 else
1086 {
1087 cc = *p;
1088 *p = NUL;
1089 // When we would write to the variable pass &ht and prevent autoload.
1090 writing = !(flags & GLV_READ_ONLY);
1091 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001092 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001093 if (v == NULL && !quiet)
1094 semsg(_(e_undefined_variable_str), lp->ll_name);
1095 *p = cc;
1096 if (v == NULL)
1097 return NULL;
1098 lp->ll_tv = &v->di_tv;
1099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100
Bram Moolenaar4525a572022-02-13 11:57:33 +00001101 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001102 {
1103 if (!quiet)
1104 semsg(_(e_variable_already_declared), lp->ll_name);
1105 return NULL;
1106 }
1107
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 /*
1109 * Loop until no more [idx] or .key is following.
1110 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001111 var1.v_type = VAR_UNKNOWN;
1112 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001113 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001115 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1116 {
1117 if (!quiet)
1118 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1119 return NULL;
1120 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001121 if (lp->ll_tv->v_type != VAR_LIST
1122 && lp->ll_tv->v_type != VAR_DICT
1123 && lp->ll_tv->v_type != VAR_BLOB)
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_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001129
1130 // a NULL list/blob works like an empty list/blob, allocate one now.
1131 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1132 rettv_list_alloc(lp->ll_tv);
1133 else if (lp->ll_tv->v_type == VAR_BLOB
1134 && lp->ll_tv->vval.v_blob == NULL)
1135 rettv_blob_alloc(lp->ll_tv);
1136
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001140 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001143
Bram Moolenaar4525a572022-02-13 11:57:33 +00001144 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001145 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001146 && lp->ll_tv == &v->di_tv
1147 && ht != NULL && ht == get_script_local_ht())
1148 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001149 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001150
1151 // Vim9 script local variable: get the type
1152 if (sv != NULL)
1153 lp->ll_valtype = sv->sv_type;
1154 }
1155
Bram Moolenaar8c711452005-01-14 21:53:12 +00001156 len = -1;
1157 if (*p == '.')
1158 {
1159 key = p + 1;
1160 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1161 ;
1162 if (len == 0)
1163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001165 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 }
1168 p = key + len;
1169 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001170 else
1171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001173 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 if (*p == ':')
1175 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001176 else
1177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001179 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001183 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001184 clear_tv(&var1);
1185 return NULL;
1186 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001187 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 }
1189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001190 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 if (*p == ':')
1192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001196 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001197 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001199 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 if (rettv != NULL
1201 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001202 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001204 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001207 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001208 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001210 }
1211 p = skipwhite(p + 1);
1212 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 else
1215 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001217 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001218 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001220 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001223 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001227 clear_tv(&var2);
1228 return NULL;
1229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001232 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001233 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001235
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001239 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001240 clear_tv(&var1);
1241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 }
1244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 ++p;
1247 }
1248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
1251 if (len == -1)
1252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001253 // "[key]": get key from "var1"
1254 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001255 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 }
1260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001262
1263 // a NULL dict is equivalent with an empty dict
1264 if (lp->ll_tv->vval.v_dict == NULL)
1265 {
1266 lp->ll_tv->vval.v_dict = dict_alloc();
1267 if (lp->ll_tv->vval.v_dict == NULL)
1268 {
1269 clear_tv(&var1);
1270 return NULL;
1271 }
1272 ++lp->ll_tv->vval.v_dict->dv_refcount;
1273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001275
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001276 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 // When assigning to a scope dictionary check that a function and
1279 // variable name is valid (only variable name unless it is l: or
1280 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001281 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001282 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001283 int prevval;
1284 int wrong;
1285
1286 if (len != -1)
1287 {
1288 prevval = key[len];
1289 key[len] = NUL;
1290 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001291 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001292 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001293 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1294 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001295 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001296 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001297 if (len != -1)
1298 key[len] = prevval;
1299 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001300 {
1301 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001302 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001303 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001304 }
1305
Bram Moolenaar10c65862020-10-08 21:16:42 +02001306 if (lp->ll_valtype != NULL)
1307 // use the type of the member
1308 lp->ll_valtype = lp->ll_valtype->tt_member;
1309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001312 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001313 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001314 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001315 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001316 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001317 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001318 return NULL;
1319 }
1320
Bram Moolenaar31b81602019-02-10 22:14:27 +01001321 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001326 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 p = NULL;
1336 break;
1337 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001338 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001339 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001340 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1341 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001342 {
1343 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001344 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001345 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001346
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001347 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 else if (lp->ll_tv->v_type == VAR_BLOB)
1351 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1353
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001354 /*
1355 * Get the number and item for the only or first index of the List.
1356 */
1357 if (empty1)
1358 lp->ll_n1 = 0;
1359 else
1360 // is number or string
1361 lp->ll_n1 = (long)tv_get_number(&var1);
1362 clear_tv(&var1);
1363
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001364 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001366 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367 return NULL;
1368 }
1369 if (lp->ll_range && !lp->ll_empty2)
1370 {
1371 lp->ll_n2 = (long)tv_get_number(&var2);
1372 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001373 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1374 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001375 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001376 }
1377 lp->ll_blob = lp->ll_tv->vval.v_blob;
1378 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001379 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001381 else
1382 {
1383 /*
1384 * Get the number and item for the only or first index of the List.
1385 */
1386 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001387 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001388 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001391 clear_tv(&var1);
1392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001395 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1396 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001397 if (lp->ll_li == NULL)
1398 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001399 clear_tv(&var2);
1400 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001401 }
1402
Bram Moolenaar10c65862020-10-08 21:16:42 +02001403 if (lp->ll_valtype != NULL)
1404 // use the type of the member
1405 lp->ll_valtype = lp->ll_valtype->tt_member;
1406
Bram Moolenaar8c711452005-01-14 21:53:12 +00001407 /*
1408 * May need to find the item or absolute index for the second
1409 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 * When no index given: "lp->ll_empty2" is TRUE.
1411 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001412 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001416 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001417 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001418 if (check_range_index_two(lp->ll_list,
1419 &lp->ll_n1, lp->ll_li,
1420 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 }
1423
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 }
1427
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001428 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 return p;
1431}
1432
1433/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001437clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438{
1439 vim_free(lp->ll_exp_name);
1440 vim_free(lp->ll_newkey);
1441}
1442
1443/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001444 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1447 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450set_var_lval(
1451 lval_T *lp,
1452 char_u *endp,
1453 typval_T *rettv,
1454 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001455 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1456 char_u *op,
1457 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458{
1459 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001460 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461
1462 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001464 cc = *endp;
1465 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001466 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1467 return;
1468
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 if (lp->ll_blob != NULL)
1470 {
1471 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001472
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 if (op != NULL && *op != '=')
1474 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001475 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 return;
1477 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001478 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001479 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480
1481 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1482 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483 if (lp->ll_empty2)
1484 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1485
Bram Moolenaar68452172021-04-12 21:21:02 +02001486 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1487 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001488 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 }
1490 else
1491 {
1492 val = (int)tv_get_number_chk(rettv, &error);
1493 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001494 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001495 }
1496 }
1497 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001499 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001501 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1502 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001503 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001504 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001505 *endp = cc;
1506 return;
1507 }
1508
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001510 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001511 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001512 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001513 {
1514 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001515 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1516 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001517 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001518 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001519 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001520 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001523 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001524 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001525 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1526 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001527 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001528 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001529 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001530 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001531 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001533 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001534 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001535 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001536 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001537 else if (lp->ll_range)
1538 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001539 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1540 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001541 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001542 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001543 return;
1544 }
1545
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001546 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1547 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001548 }
1549 else
1550 {
1551 /*
1552 * Assign to a List or Dictionary item.
1553 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001554 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1555 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001556 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001557 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001558 return;
1559 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001560
1561 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001562 && check_typval_arg_type(lp->ll_valtype, rettv,
1563 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001564 return;
1565
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001566 if (lp->ll_newkey != NULL)
1567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 if (op != NULL && *op != '=')
1569 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001570 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 return;
1572 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001573 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1574 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001575 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001578 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001579 if (di == NULL)
1580 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001581 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1582 {
1583 vim_free(di);
1584 return;
1585 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001586 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 else if (op != NULL && *op != '=')
1589 {
1590 tv_op(lp->ll_tv, rettv, op);
1591 return;
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001594 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001596 /*
1597 * Assign the value to the variable or list item.
1598 */
1599 if (copy)
1600 copy_tv(rettv, lp->ll_tv);
1601 else
1602 {
1603 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001604 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001605 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 }
1607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608}
1609
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1612 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 * Returns OK or FAIL.
1614 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001618 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 char_u numbuf[NUMBUFLEN];
1620 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001621 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001623 // Can't do anything with a Funcref or Dict on the right.
1624 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001625 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001626 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1627 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 {
1629 switch (tv1->v_type)
1630 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001631 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001632 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 case VAR_DICT:
1635 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001636 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001637 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001638 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001640 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001641 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 break;
1643
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 case VAR_BLOB:
1645 if (*op != '+' || tv2->v_type != VAR_BLOB)
1646 break;
1647 // BLOB += BLOB
1648 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1649 {
1650 blob_T *b1 = tv1->vval.v_blob;
1651 blob_T *b2 = tv2->vval.v_blob;
1652 int i, len = blob_len(b2);
1653 for (i = 0; i < len; i++)
1654 ga_append(&b1->bv_ga, blob_get(b2, i));
1655 }
1656 return OK;
1657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 case VAR_LIST:
1659 if (*op != '+' || tv2->v_type != VAR_LIST)
1660 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001661 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001662 if (tv2->vval.v_list != NULL)
1663 {
1664 if (tv1->vval.v_list == NULL)
1665 {
1666 tv1->vval.v_list = tv2->vval.v_list;
1667 ++tv1->vval.v_list->lv_refcount;
1668 }
1669 else
1670 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 return OK;
1673
1674 case VAR_NUMBER:
1675 case VAR_STRING:
1676 if (tv2->v_type == VAR_LIST)
1677 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001678 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001680 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001681 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682#ifdef FEAT_FLOAT
1683 if (tv2->v_type == VAR_FLOAT)
1684 {
1685 float_T f = n;
1686
Bram Moolenaarff697e62019-02-12 22:28:33 +01001687 if (*op == '%')
1688 break;
1689 switch (*op)
1690 {
1691 case '+': f += tv2->vval.v_float; break;
1692 case '-': f -= tv2->vval.v_float; break;
1693 case '*': f *= tv2->vval.v_float; break;
1694 case '/': f /= tv2->vval.v_float; break;
1695 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001696 clear_tv(tv1);
1697 tv1->v_type = VAR_FLOAT;
1698 tv1->vval.v_float = f;
1699 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001701#endif
1702 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001703 switch (*op)
1704 {
1705 case '+': n += tv_get_number(tv2); break;
1706 case '-': n -= tv_get_number(tv2); break;
1707 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001708 case '/': n = num_divide(n, tv_get_number(tv2),
1709 &failed); break;
1710 case '%': n = num_modulus(n, tv_get_number(tv2),
1711 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001712 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001713 clear_tv(tv1);
1714 tv1->v_type = VAR_NUMBER;
1715 tv1->vval.v_number = n;
1716 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 }
1718 else
1719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001720 if (tv2->v_type == VAR_FLOAT)
1721 break;
1722
Bram Moolenaarff697e62019-02-12 22:28:33 +01001723 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001724 s = tv_get_string(tv1);
1725 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 clear_tv(tv1);
1727 tv1->v_type = VAR_STRING;
1728 tv1->vval.v_string = s;
1729 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001730 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001731
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001732 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001733#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001734 {
1735 float_T f;
1736
Bram Moolenaarff697e62019-02-12 22:28:33 +01001737 if (*op == '%' || *op == '.'
1738 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001739 && tv2->v_type != VAR_NUMBER
1740 && tv2->v_type != VAR_STRING))
1741 break;
1742 if (tv2->v_type == VAR_FLOAT)
1743 f = tv2->vval.v_float;
1744 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001745 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001746 switch (*op)
1747 {
1748 case '+': tv1->vval.v_float += f; break;
1749 case '-': tv1->vval.v_float -= f; break;
1750 case '*': tv1->vval.v_float *= f; break;
1751 case '/': tv1->vval.v_float /= f; break;
1752 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001753 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001754#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001755 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 }
1757 }
1758
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001759 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 return FAIL;
1761}
1762
1763/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 * Evaluate the expression used in a ":for var in expr" command.
1765 * "arg" points to "var".
1766 * Set "*errp" to TRUE for an error, FALSE otherwise;
1767 * Return a pointer that holds the info. Null when there is an error.
1768 */
1769 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001770eval_for_line(
1771 char_u *arg,
1772 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001773 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001774 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775{
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001777 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001779 typval_T tv;
1780 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001781 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001785 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 if (fi == NULL)
1787 return NULL;
1788
Bram Moolenaar404557e2021-07-05 21:41:48 +02001789 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1790 &fi->fi_semicolon, FALSE);
1791 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 return fi;
1793
Bram Moolenaar404557e2021-07-05 21:41:48 +02001794 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001795 if (expr[0] != 'i' || expr[1] != 'n'
1796 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001798 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1799 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1800 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001801 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 return fi;
1803 }
1804
1805 if (skip)
1806 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001807 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1808 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 {
1810 *errp = FALSE;
1811 if (!skip)
1812 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001813 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001814 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001815 l = tv.vval.v_list;
1816 if (l == NULL)
1817 {
1818 // a null list is like an empty list: do nothing
1819 clear_tv(&tv);
1820 }
1821 else
1822 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001824 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001826 // No need to increment the refcount, it's already set for
1827 // the list being used in "tv".
1828 fi->fi_list = l;
1829 list_add_watch(l, &fi->fi_lw);
1830 fi->fi_lw.lw_item = l->lv_first;
1831 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001832 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001833 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001834 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001835 fi->fi_bi = 0;
1836 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001837 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001838 typval_T btv;
1839
1840 // Make a copy, so that the iteration still works when the
1841 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001843 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001844 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001845 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001846 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001847 else if (tv.v_type == VAR_STRING)
1848 {
1849 fi->fi_byte_idx = 0;
1850 fi->fi_string = tv.vval.v_string;
1851 tv.vval.v_string = NULL;
1852 if (fi->fi_string == NULL)
1853 fi->fi_string = vim_strsave((char_u *)"");
1854 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 else
1856 {
Sean Dewar80d73952021-08-04 19:25:54 +02001857 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001858 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 }
1860 }
1861 }
1862 if (skip)
1863 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001864 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865
1866 return fi;
1867}
1868
1869/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001870 * Used when looping over a :for line, skip the "in expr" part.
1871 */
1872 void
1873skip_for_lines(void *fi_void, evalarg_T *evalarg)
1874{
1875 forinfo_T *fi = (forinfo_T *)fi_void;
1876 int i;
1877
1878 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001879 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001880}
1881
1882/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 * Use the first item in a ":for" list. Advance to the next.
1884 * Assign the values to the variable (list). "arg" points to the first one.
1885 * Return TRUE when a valid item was found, FALSE when at end of list or
1886 * something wrong.
1887 */
1888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001891 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001893 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001894 ? (ASSIGN_FINAL
1895 // first round: error if variable exists
1896 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1897 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001898 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001900 int skip_assign = in_vim9script() && arg[0] == '_'
1901 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001903 if (fi->fi_blob != NULL)
1904 {
1905 typval_T tv;
1906
1907 if (fi->fi_bi >= blob_len(fi->fi_blob))
1908 return FALSE;
1909 tv.v_type = VAR_NUMBER;
1910 tv.v_lock = VAR_FIXED;
1911 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1912 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001913 if (skip_assign)
1914 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001915 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001916 fi->fi_varcount, flag, NULL) == OK;
1917 }
1918
1919 if (fi->fi_string != NULL)
1920 {
1921 typval_T tv;
1922 int len;
1923
1924 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1925 if (len == 0)
1926 return FALSE;
1927 tv.v_type = VAR_STRING;
1928 tv.v_lock = VAR_FIXED;
1929 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1930 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001931 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001932 if (skip_assign)
1933 result = TRUE;
1934 else
1935 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001936 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001937 vim_free(tv.vval.v_string);
1938 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001939 }
1940
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 item = fi->fi_lw.lw_item;
1942 if (item == NULL)
1943 result = FALSE;
1944 else
1945 {
1946 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001947 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001948 if (skip_assign)
1949 result = TRUE;
1950 else
1951 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001952 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 }
1954 return result;
1955}
1956
1957/*
1958 * Free the structure used to store info used by ":for".
1959 */
1960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001961free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962{
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001965 if (fi == NULL)
1966 return;
1967 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001968 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001970 list_unref(fi->fi_list);
1971 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001972 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001973 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001974 else
1975 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 vim_free(fi);
1977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001980set_context_for_expression(
1981 expand_T *xp,
1982 char_u *arg,
1983 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001985 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001989 if (cmdidx == CMD_let || cmdidx == CMD_var
1990 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 {
1992 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001995 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 {
1998 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001999 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002000 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 break;
2002 }
2003 return;
2004 }
2005 }
2006 else
2007 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2008 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 while ((xp->xp_pattern = vim_strpbrk(arg,
2010 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2011 {
2012 c = *xp->xp_pattern;
2013 if (c == '&')
2014 {
2015 c = xp->xp_pattern[1];
2016 if (c == '&')
2017 {
2018 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002019 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002024 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2025 xp->xp_pattern += 2;
2026
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 else if (c == '$')
2030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002031 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 xp->xp_context = EXPAND_ENV_VARS;
2033 }
2034 else if (c == '=')
2035 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002036 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 xp->xp_context = EXPAND_EXPRESSION;
2038 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002039 else if (c == '#'
2040 && xp->xp_context == EXPAND_EXPRESSION)
2041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002042 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002043 break;
2044 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002045 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 && xp->xp_context == EXPAND_FUNCTIONS
2047 && vim_strchr(xp->xp_pattern, '(') == NULL)
2048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
2051 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002052 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002054 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2057 if (c == '\\' && xp->xp_pattern[1] != NUL)
2058 ++xp->xp_pattern;
2059 xp->xp_context = EXPAND_NOTHING;
2060 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002061 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2065 /* skip */ ;
2066 xp->xp_context = EXPAND_NOTHING;
2067 }
2068 else if (c == '|')
2069 {
2070 if (xp->xp_pattern[1] == '|')
2071 {
2072 ++xp->xp_pattern;
2073 xp->xp_context = EXPAND_EXPRESSION;
2074 }
2075 else
2076 xp->xp_context = EXPAND_COMMANDS;
2077 }
2078 else
2079 xp->xp_context = EXPAND_EXPRESSION;
2080 }
2081 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002082 // Doesn't look like something valid, expand as an expression
2083 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 arg = xp->xp_pattern;
2086 if (*arg != NUL)
2087 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2088 /* skip */ ;
2089 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002090
2091 // ":exe one two" completes "two"
2092 if ((cmdidx == CMD_execute
2093 || cmdidx == CMD_echo
2094 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002095 || cmdidx == CMD_echomsg
2096 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002097 && xp->xp_context == EXPAND_EXPRESSION)
2098 {
2099 for (;;)
2100 {
2101 char_u *n = skiptowhite(arg);
2102
2103 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2104 break;
2105 arg = skipwhite(n);
2106 }
2107 }
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 xp->xp_pattern = arg;
2110}
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002113 * Return TRUE if "pat" matches "text".
2114 * Does not use 'cpo' and always uses 'magic'.
2115 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002116 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002117pattern_match(char_u *pat, char_u *text, int ic)
2118{
2119 int matches = FALSE;
2120 char_u *save_cpo;
2121 regmatch_T regmatch;
2122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002123 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002124 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002125 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002126 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2127 if (regmatch.regprog != NULL)
2128 {
2129 regmatch.rm_ic = ic;
2130 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2131 vim_regfree(regmatch.regprog);
2132 }
2133 p_cpo = save_cpo;
2134 return matches;
2135}
2136
2137/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002138 * Handle a name followed by "(". Both for just "name(arg)" and for
2139 * "expr->name(arg)".
2140 * Returns OK or FAIL.
2141 */
2142 static int
2143eval_func(
2144 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002145 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002146 char_u *name,
2147 int name_len,
2148 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002149 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002150 typval_T *basetv) // "expr" for "expr->name(arg)"
2151{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002152 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002153 char_u *s = name;
2154 int len = name_len;
2155 partial_T *partial;
2156 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002157 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002158 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002159
2160 if (!evaluate)
2161 check_vars(s, len);
2162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002163 // If "s" is the name of a variable of type VAR_FUNC
2164 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002165 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002166 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002167
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002168 // Need to make a copy, in case evaluating the arguments makes
2169 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002170 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002171 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002172 ret = FAIL;
2173 else
2174 {
2175 funcexe_T funcexe;
2176
2177 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002178 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002179 funcexe.fe_firstline = curwin->w_cursor.lnum;
2180 funcexe.fe_lastline = curwin->w_cursor.lnum;
2181 funcexe.fe_evaluate = evaluate;
2182 funcexe.fe_partial = partial;
2183 funcexe.fe_basetv = basetv;
2184 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002185 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002186 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002187 }
2188 vim_free(s);
2189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002190 // If evaluate is FALSE rettv->v_type was not set in
2191 // get_func_tv, but it's needed in handle_subscript() to parse
2192 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002193 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2194 {
2195 rettv->vval.v_string = NULL;
2196 rettv->v_type = VAR_FUNC;
2197 }
2198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002199 // Stop the expression evaluation when immediately
2200 // aborting on error, or when an interrupt occurred or
2201 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002202 if (evaluate && aborting())
2203 {
2204 if (ret == OK)
2205 clear_tv(rettv);
2206 ret = FAIL;
2207 }
2208 return ret;
2209}
2210
2211/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002212 * After a NL, skip over empty lines and comment-only lines.
2213 */
2214 static char_u *
2215newline_skip_comments(char_u *arg)
2216{
2217 char_u *p = arg + 1;
2218
2219 for (;;)
2220 {
2221 p = skipwhite(p);
2222
2223 if (*p == NUL)
2224 break;
2225 if (vim9_comment_start(p))
2226 {
2227 char_u *nl = vim_strchr(p, NL);
2228
2229 if (nl == NULL)
2230 break;
2231 p = nl;
2232 }
2233 if (*p != NL)
2234 break;
2235 ++p; // skip another NL
2236 }
2237 return p;
2238}
2239
2240/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002241 * Get the next line source line without advancing. But do skip over comment
2242 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002243 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002244 */
2245 static char_u *
2246getline_peek_skip_comments(evalarg_T *evalarg)
2247{
2248 for (;;)
2249 {
2250 char_u *next = getline_peek(evalarg->eval_getline,
2251 evalarg->eval_cookie);
2252 char_u *p;
2253
2254 if (next == NULL)
2255 break;
2256 p = skipwhite(next);
2257 if (*p != NUL && !vim9_comment_start(p))
2258 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002259 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002260 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002261 }
2262 return NULL;
2263}
2264
2265/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002266 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2267 * comment) and there is a next line, return the next line (skipping blanks)
2268 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002269 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2270 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 * "arg" must point somewhere inside a line, not at the start.
2272 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002273 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002274eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2275{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002276 char_u *p = skipwhite(arg);
2277
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002279 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002281 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2282 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002283 && (*p == NUL || *p == NL
2284 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002285 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002286 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002287
Bram Moolenaare442d592022-05-05 12:20:28 +01002288 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002289 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002290 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002291 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002292 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002293 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294
Bram Moolenaar9d489562020-07-30 20:08:50 +02002295 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 {
2297 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299 }
2300 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002301 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302}
2303
2304/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002305 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002306 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002307 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002308 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002309eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002310{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002311 garray_T *gap = &evalarg->eval_ga;
2312 char_u *line;
2313
Bram Moolenaar39be4982022-05-06 21:51:50 +01002314 if (arg != NULL)
2315 {
2316 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002317 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002318 // Truncate before a trailing comment, so that concatenating the lines
2319 // won't turn the rest into a comment.
2320 if (*skipwhite(arg) == '#')
2321 *arg = NUL;
2322 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002323
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002324 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002325 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2326 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002327 else
2328 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002329 if (line == NULL)
2330 return NULL;
2331
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002332 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002333 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2334 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002335 char_u *p = skipwhite(line);
2336
2337 // Going to concatenate the lines after parsing. For an empty or
2338 // comment line use an empty string.
2339 if (*p == NUL || vim9_comment_start(p))
2340 {
2341 vim_free(line);
2342 line = vim_strsave((char_u *)"");
2343 }
2344
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002345 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2346 ++gap->ga_len;
2347 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002348 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002349 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002350 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002351 evalarg->eval_tofree = line;
2352 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002353
2354 // Advanced to the next line, "arg" no longer points into the previous
2355 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002356 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002357 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002358}
2359
Bram Moolenaare6b53242020-07-01 17:28:33 +02002360/*
2361 * Call eval_next_non_blank() and get the next line if needed.
2362 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002363 char_u *
2364skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2365{
2366 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002367 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002368
Bram Moolenaar962d7212020-07-04 14:15:00 +02002369 if (evalarg == NULL)
2370 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002371 eval_next_non_blank(p, evalarg, &getnext);
2372 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002373 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002374 return p;
2375}
2376
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2381 */
2382
2383/*
2384 * Handle zero level expression.
2385 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002387 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 * Return OK or FAIL.
2390 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002392eval0(
2393 char_u *arg,
2394 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002395 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002398 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2399}
2400
2401/*
2402 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2403 * expression and don't check what comes after the expression.
2404 */
2405 int
2406eval0_retarg(
2407 char_u *arg,
2408 typval_T *rettv,
2409 exarg_T *eap,
2410 evalarg_T *evalarg,
2411 char_u **retarg)
2412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 int ret;
2414 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002415 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002416 int did_emsg_before = did_emsg;
2417 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002419 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002420 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002424
Bram Moolenaar79481362022-06-27 20:15:10 +01002425 if (ret != FAIL)
2426 {
2427 expr_end = p;
2428 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002429
Bram Moolenaar79481362022-06-27 20:15:10 +01002430 // In Vim9 script a command block is not split at NL characters for
2431 // commands using an expression argument. Skip over a '#' comment to
2432 // check for a following NL. Require white space before the '#'.
2433 if (in_vim9script() && p > expr_end && retarg == NULL)
2434 while (*p == '#')
2435 {
2436 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002437
Bram Moolenaar79481362022-06-27 20:15:10 +01002438 if (nl == NULL)
2439 break;
2440 p = skipwhite(nl + 1);
2441 if (eap != NULL && *p != NUL)
2442 eap->nextcmd = p;
2443 check_for_end = FALSE;
2444 }
2445
2446 if (check_for_end)
2447 end_error = !ends_excmd2(arg, p);
2448 }
2449
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002450 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
2452 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 /*
2455 * Report the invalid expression unless the expression evaluation has
2456 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002457 * exception, or we already gave a more specific error.
2458 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002460 if (!aborting()
2461 && did_emsg == did_emsg_before
2462 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002463 && (flags & EVAL_CONSTANT) == 0
2464 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002465 {
2466 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002467 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002468 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002469 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002470 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002471
2472 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002473 // a next command to avoid more errors, unless "|" is following, which
2474 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002475 if (eap != NULL && p != NULL
2476 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002477 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002478 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002480
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002481 if (retarg != NULL)
2482 *retarg = p;
2483 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002484 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 return ret;
2487}
2488
2489/*
2490 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002491 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002492 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 *
2494 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002495 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002497 * Note: "rettv.v_lock" is not set.
2498 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * Return OK or FAIL.
2500 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002501 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002504 char_u *p;
2505 int getnext;
2506
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002507 CLEAR_POINTER(rettv);
2508
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 /*
2510 * Get the first variable.
2511 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return FAIL;
2514
Bram Moolenaar793648f2020-06-26 21:28:25 +02002515 p = eval_next_non_blank(*arg, evalarg, &getnext);
2516 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002518 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002519 int result;
2520 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 evalarg_T *evalarg_used = evalarg;
2522 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002524 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002525 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526
2527 if (evalarg == NULL)
2528 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002529 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 orig_flags = evalarg_used->eval_flags;
2533 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002535 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002536 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002537 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002538 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002539 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002540 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002541 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002542 clear_tv(rettv);
2543 return FAIL;
2544 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002545 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002551 int error = FALSE;
2552
Bram Moolenaar13106602020-10-04 16:06:05 +02002553 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002554 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002555 else if (vim9script)
2556 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002557 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002559 if (error || !op_falsy || !result)
2560 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002561 if (error)
2562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564
2565 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002566 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002568 if (op_falsy)
2569 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002570 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002571 {
mityu4ac198c2021-05-28 17:52:40 +02002572 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002573 clear_tv(rettv);
2574 return FAIL;
2575 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002577 evalarg_used->eval_flags = (op_falsy ? !result : result)
2578 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2579 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002580 {
2581 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002583 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002584 if (!op_falsy || !result)
2585 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002587 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002589 /*
2590 * Check for the ":".
2591 */
2592 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2593 if (*p != ':')
2594 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002595 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002596 if (evaluate && result)
2597 clear_tv(rettv);
2598 evalarg_used->eval_flags = orig_flags;
2599 return FAIL;
2600 }
2601 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002602 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002603 else
2604 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002605 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002606 {
2607 error_white_both(p, 1);
2608 clear_tv(rettv);
2609 evalarg_used->eval_flags = orig_flags;
2610 return FAIL;
2611 }
2612 *arg = p;
2613 }
2614
2615 /*
2616 * Get the third variable. Recursive!
2617 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002618 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002619 {
mityu4ac198c2021-05-28 17:52:40 +02002620 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002621 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002622 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002623 return FAIL;
2624 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002625 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2626 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002628 if (eval1(arg, &var2, evalarg_used) == FAIL)
2629 {
2630 if (evaluate && result)
2631 clear_tv(rettv);
2632 evalarg_used->eval_flags = orig_flags;
2633 return FAIL;
2634 }
2635 if (evaluate && !result)
2636 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638
2639 if (evalarg == NULL)
2640 clear_evalarg(&local_evalarg, NULL);
2641 else
2642 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644
2645 return OK;
2646}
2647
2648/*
2649 * Handle first level expression:
2650 * expr2 || expr2 || expr2 logical OR
2651 *
2652 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002653 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 *
2655 * Return OK or FAIL.
2656 */
2657 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002660 char_u *p;
2661 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002664 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 return FAIL;
2668
2669 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002672 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 evalarg_T *evalarg_used = evalarg;
2676 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677 int evaluate;
2678 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 long result = FALSE;
2680 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002681 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002683
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 if (evalarg == NULL)
2685 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002686 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002689 orig_flags = evalarg_used->eval_flags;
2690 evaluate = orig_flags & EVAL_EVALUATE;
2691 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002692 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002693 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002694 result = tv_get_bool_chk(rettv, &error);
2695 else if (tv_get_number_chk(rettv, &error) != 0)
2696 result = TRUE;
2697 clear_tv(rettv);
2698 if (error)
2699 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002703 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002705 while (p[0] == '|' && p[1] == '|')
2706 {
2707 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002708 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002709 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002710 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002711 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002712 {
2713 error_white_both(p, 2);
2714 clear_tv(rettv);
2715 return FAIL;
2716 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002717 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002718 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719
2720 /*
2721 * Get the second variable.
2722 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002723 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002724 {
mityu4ac198c2021-05-28 17:52:40 +02002725 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002726 clear_tv(rettv);
2727 return FAIL;
2728 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002729 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2730 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002731 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734
2735 /*
2736 * Compute the result.
2737 */
2738 if (evaluate && !result)
2739 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002740 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 result = tv_get_bool_chk(&var2, &error);
2742 else if (tv_get_number_chk(&var2, &error) != 0)
2743 result = TRUE;
2744 clear_tv(&var2);
2745 if (error)
2746 return FAIL;
2747 }
2748 if (evaluate)
2749 {
2750 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002751 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002752 rettv->v_type = VAR_BOOL;
2753 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002754 }
2755 else
2756 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002757 rettv->v_type = VAR_NUMBER;
2758 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002759 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761
2762 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002764
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765 if (evalarg == NULL)
2766 clear_evalarg(&local_evalarg, NULL);
2767 else
2768 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770
2771 return OK;
2772}
2773
2774/*
2775 * Handle second level expression:
2776 * expr3 && expr3 && expr3 logical AND
2777 *
2778 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002779 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 *
2781 * Return OK or FAIL.
2782 */
2783 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002786 char_u *p;
2787 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002790 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 return FAIL;
2794
2795 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002796 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002798 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801 evalarg_T *evalarg_used = evalarg;
2802 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002803 int orig_flags;
2804 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 long result = TRUE;
2806 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002807 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002808 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002809
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 if (evalarg == NULL)
2811 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002812 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002814 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002815 orig_flags = evalarg_used->eval_flags;
2816 evaluate = orig_flags & EVAL_EVALUATE;
2817 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002819 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002820 result = tv_get_bool_chk(rettv, &error);
2821 else if (tv_get_number_chk(rettv, &error) == 0)
2822 result = FALSE;
2823 clear_tv(rettv);
2824 if (error)
2825 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827
2828 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002829 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002831 while (p[0] == '&' && p[1] == '&')
2832 {
2833 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002834 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002835 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002836 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002837 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002838 {
2839 error_white_both(p, 2);
2840 clear_tv(rettv);
2841 return FAIL;
2842 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002843 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002844 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002845
2846 /*
2847 * Get the second variable.
2848 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002849 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002850 {
mityu4ac198c2021-05-28 17:52:40 +02002851 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002852 clear_tv(rettv);
2853 return FAIL;
2854 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002855 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2856 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002858 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002859 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002860 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002861
2862 /*
2863 * Compute the result.
2864 */
2865 if (evaluate && result)
2866 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002867 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002868 result = tv_get_bool_chk(&var2, &error);
2869 else if (tv_get_number_chk(&var2, &error) == 0)
2870 result = FALSE;
2871 clear_tv(&var2);
2872 if (error)
2873 return FAIL;
2874 }
2875 if (evaluate)
2876 {
2877 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002878 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002879 rettv->v_type = VAR_BOOL;
2880 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002881 }
2882 else
2883 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 rettv->v_type = VAR_NUMBER;
2885 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002886 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002887 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002888
2889 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002891
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002892 if (evalarg == NULL)
2893 clear_evalarg(&local_evalarg, NULL);
2894 else
2895 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897
2898 return OK;
2899}
2900
2901/*
2902 * Handle third level expression:
2903 * var1 == var2
2904 * var1 =~ var2
2905 * var1 != var2
2906 * var1 !~ var2
2907 * var1 > var2
2908 * var1 >= var2
2909 * var1 < var2
2910 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002911 * var1 is var2
2912 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 *
2914 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002915 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 *
2917 * Return OK or FAIL.
2918 */
2919 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002923 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002924 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002926 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002929 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 return FAIL;
2933
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002934 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002935
Bram Moolenaar696ba232020-07-29 21:20:41 +02002936 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002939 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002941 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002943 typval_T var2;
2944 int ic;
2945 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002946 int evaluate = evalarg == NULL
2947 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002948 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002949
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002950 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002951 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002952 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002953 p = *arg;
2954 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002955 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2956 {
mityu4ac198c2021-05-28 17:52:40 +02002957 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002958 clear_tv(rettv);
2959 return FAIL;
2960 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002961
Bram Moolenaar696ba232020-07-29 21:20:41 +02002962 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2963 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002964 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002965 clear_tv(rettv);
2966 return FAIL;
2967 }
2968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002969 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (p[len] == '?')
2971 {
2972 ic = TRUE;
2973 ++len;
2974 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002975 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else if (p[len] == '#')
2977 {
2978 ic = FALSE;
2979 ++len;
2980 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002981 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002983 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 /*
2986 * Get the second variable.
2987 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002988 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2989 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002990 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002991 clear_tv(rettv);
2992 return FAIL;
2993 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002994 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 return FAIL;
2999 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003000 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003001 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003002 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003003
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003004 // use the line of the comparison for messages
3005 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003006 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003007 {
3008 ret = FAIL;
3009 clear_tv(rettv);
3010 }
3011 else
3012 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003013 clear_tv(&var2);
3014 return ret;
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017
3018 return OK;
3019}
3020
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003021/*
3022 * Make a copy of blob "tv1" and append blob "tv2".
3023 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 void
3025eval_addblob(typval_T *tv1, typval_T *tv2)
3026{
3027 blob_T *b1 = tv1->vval.v_blob;
3028 blob_T *b2 = tv2->vval.v_blob;
3029 blob_T *b = blob_alloc();
3030 int i;
3031
3032 if (b != NULL)
3033 {
3034 for (i = 0; i < blob_len(b1); i++)
3035 ga_append(&b->bv_ga, blob_get(b1, i));
3036 for (i = 0; i < blob_len(b2); i++)
3037 ga_append(&b->bv_ga, blob_get(b2, i));
3038
3039 clear_tv(tv1);
3040 rettv_blob_set(tv1, b);
3041 }
3042}
3043
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003044/*
3045 * Make a copy of list "tv1" and append list "tv2".
3046 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 int
3048eval_addlist(typval_T *tv1, typval_T *tv2)
3049{
3050 typval_T var3;
3051
3052 // concatenate Lists
3053 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3054 {
3055 clear_tv(tv1);
3056 clear_tv(tv2);
3057 return FAIL;
3058 }
3059 clear_tv(tv1);
3060 *tv1 = var3;
3061 return OK;
3062}
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003065 * Handle the bitwise left/right shift operator expression:
3066 * var1 << var2
3067 * var1 >> var2
3068 *
3069 * "arg" must point to the first non-white of the expression.
3070 * "arg" is advanced to just after the recognized expression.
3071 *
3072 * Return OK or FAIL.
3073 */
3074 static int
3075eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3076{
3077 /*
3078 * Get the first expression.
3079 */
3080 if (eval6(arg, rettv, evalarg) == FAIL)
3081 return FAIL;
3082
3083 /*
3084 * Repeat computing, until no '<<' or '>>' is following.
3085 */
3086 for (;;)
3087 {
3088 char_u *p;
3089 int getnext;
3090 exprtype_T type;
3091 int evaluate;
3092 typval_T var2;
3093 int vim9script;
3094
3095 p = eval_next_non_blank(*arg, evalarg, &getnext);
3096 if (p[0] == '<' && p[1] == '<')
3097 type = EXPR_LSHIFT;
3098 else if (p[0] == '>' && p[1] == '>')
3099 type = EXPR_RSHIFT;
3100 else
3101 return OK;
3102
3103 // Handle a bitwise left or right shift operator
3104 if (rettv->v_type != VAR_NUMBER)
3105 {
3106 // left operand should be a number
3107 emsg(_(e_bitshift_ops_must_be_number));
3108 clear_tv(rettv);
3109 return FAIL;
3110 }
3111
3112 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3113 vim9script = in_vim9script();
3114 if (getnext)
3115 {
3116 *arg = eval_next_line(*arg, evalarg);
3117 p = *arg;
3118 }
3119 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3120 {
3121 error_white_both(*arg, 2);
3122 clear_tv(rettv);
3123 return FAIL;
3124 }
3125
3126 /*
3127 * Get the second variable.
3128 */
3129 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3130 {
3131 error_white_both(p, 2);
3132 clear_tv(rettv);
3133 return FAIL;
3134 }
3135 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3136 if (eval6(arg, &var2, evalarg) == FAIL)
3137 {
3138 clear_tv(rettv);
3139 return FAIL;
3140 }
3141
3142 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3143 {
3144 // right operand should be a positive number
3145 if (var2.v_type != VAR_NUMBER)
3146 emsg(_(e_bitshift_ops_must_be_number));
3147 else
3148 emsg(_(e_bitshift_ops_must_be_postive));
3149 clear_tv(rettv);
3150 clear_tv(&var2);
3151 return FAIL;
3152 }
3153
3154 if (evaluate)
3155 {
3156 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3157 // shifting more bits than we have always results in zero
3158 rettv->vval.v_number = 0;
3159 else if (type == EXPR_LSHIFT)
3160 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003161 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003162 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003163 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003164 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003165 }
3166
3167 clear_tv(&var2);
3168 }
3169
3170 return OK;
3171}
3172
3173/*
3174 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003175 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003177 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003178 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 *
3180 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003181 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 *
3183 * Return OK or FAIL.
3184 */
3185 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003186eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003189 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003191 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 return FAIL;
3193
3194 /*
3195 * Repeat computing, until no '+', '-' or '.' is following.
3196 */
3197 for (;;)
3198 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003199 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200 int getnext;
3201 char_u *p;
3202 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003203 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003204 int concat;
3205 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003206 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003207
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003208 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003209 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003210 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003211 p = eval_next_non_blank(*arg, evalarg, &getnext);
3212 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003213 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003214 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3215 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003217 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3218 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003219
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003220 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003221 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003222 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003223 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003224 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003225 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003226 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003227 {
mityu4ac198c2021-05-28 17:52:40 +02003228 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003229 clear_tv(rettv);
3230 return FAIL;
3231 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003232 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003233 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003234 if ((op != '+' || (rettv->v_type != VAR_LIST
3235 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003236#ifdef FEAT_FLOAT
3237 && (op == '.' || rettv->v_type != VAR_FLOAT)
3238#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003239 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003240 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003241 int error = FALSE;
3242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003243 // For "list + ...", an illegal use of the first operand as
3244 // a number cannot be determined before evaluating the 2nd
3245 // operand: if this is also a list, all is ok.
3246 // For "something . ...", "something - ..." or "non-list + ...",
3247 // we know that the first operand needs to be a string or number
3248 // without evaluating the 2nd operand. So check before to avoid
3249 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003250 if (op != '.')
3251 tv_get_number_chk(rettv, &error);
3252 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253 {
3254 clear_tv(rettv);
3255 return FAIL;
3256 }
3257 }
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 /*
3260 * Get the second variable.
3261 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003262 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003263 {
mityu89dcb4d2021-05-28 13:50:17 +02003264 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003265 clear_tv(rettv);
3266 return FAIL;
3267 }
3268 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003269 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003271 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return FAIL;
3273 }
3274
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003275 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
3277 /*
3278 * Compute the result.
3279 */
3280 if (op == '.')
3281 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003282 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3283 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003284 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003285
Bram Moolenaar2e086612020-08-25 22:37:48 +02003286 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003287 || var2.v_type == VAR_CHANNEL
3288 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003289 semsg(_(e_using_invalid_value_as_string_str),
3290 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003291#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003292 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003293 {
3294 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3295 var2.vval.v_float);
3296 s2 = buf2;
3297 }
3298#endif
3299 else
3300 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003301 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003302 {
3303 clear_tv(rettv);
3304 clear_tv(&var2);
3305 return FAIL;
3306 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003307 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003308 clear_tv(rettv);
3309 rettv->v_type = VAR_STRING;
3310 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003312 else if (op == '+' && rettv->v_type == VAR_BLOB
3313 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003315 else if (op == '+' && rettv->v_type == VAR_LIST
3316 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003317 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003319 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
3322 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003323 int error = FALSE;
3324 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003325#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003326 float_T f1 = 0, f2 = 0;
3327
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003330 f1 = rettv->vval.v_float;
3331 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003333 else
3334#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003337 if (error)
3338 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003339 // This can only happen for "list + non-list" or
3340 // "blob + non-blob". For "non-list + ..." or
3341 // "something - ...", we returned before evaluating the
3342 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003344 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 return FAIL;
3346 }
3347#ifdef FEAT_FLOAT
3348 if (var2.v_type == VAR_FLOAT)
3349 f1 = n1;
3350#endif
3351 }
3352#ifdef FEAT_FLOAT
3353 if (var2.v_type == VAR_FLOAT)
3354 {
3355 f2 = var2.vval.v_float;
3356 n2 = 0;
3357 }
3358 else
3359#endif
3360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003361 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003362 if (error)
3363 {
3364 clear_tv(rettv);
3365 clear_tv(&var2);
3366 return FAIL;
3367 }
3368#ifdef FEAT_FLOAT
3369 if (rettv->v_type == VAR_FLOAT)
3370 f2 = n2;
3371#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003374
3375#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003376 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003377 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3378 {
3379 if (op == '+')
3380 f1 = f1 + f2;
3381 else
3382 f1 = f1 - f2;
3383 rettv->v_type = VAR_FLOAT;
3384 rettv->vval.v_float = f1;
3385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003387#endif
3388 {
3389 if (op == '+')
3390 n1 = n1 + n2;
3391 else
3392 n1 = n1 - n2;
3393 rettv->v_type = VAR_NUMBER;
3394 rettv->vval.v_number = n1;
3395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399 }
3400 return OK;
3401}
3402
3403/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003404 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * * number multiplication
3406 * / number division
3407 * % number modulo
3408 *
3409 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003410 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 *
3412 * Return OK or FAIL.
3413 */
3414 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003415eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003416 char_u **arg,
3417 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003418 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003419 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003421#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003422 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003426 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003428 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return FAIL;
3430
3431 /*
3432 * Repeat computing, until no '*', '/' or '%' is following.
3433 */
3434 for (;;)
3435 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003436 int evaluate;
3437 int getnext;
3438 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003439 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003440 int op;
3441 varnumber_T n1, n2;
3442#ifdef FEAT_FLOAT
3443 float_T f1, f2;
3444#endif
3445 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003446
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003447 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003448 p = eval_next_non_blank(*arg, evalarg, &getnext);
3449 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003450 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003452
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003453 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003454 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003455 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003456 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003457 {
3458 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3459 {
mityu4ac198c2021-05-28 17:52:40 +02003460 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003461 clear_tv(rettv);
3462 return FAIL;
3463 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003464 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003467#ifdef FEAT_FLOAT
3468 f1 = 0;
3469 f2 = 0;
3470#endif
3471 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003472 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003474#ifdef FEAT_FLOAT
3475 if (rettv->v_type == VAR_FLOAT)
3476 {
3477 f1 = rettv->vval.v_float;
3478 use_float = TRUE;
3479 n1 = 0;
3480 }
3481 else
3482#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003483 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 if (error)
3486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 else
3489 n1 = 0;
3490
3491 /*
3492 * Get the second variable.
3493 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003494 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3495 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003496 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003497 clear_tv(rettv);
3498 return FAIL;
3499 }
3500 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003501 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return FAIL;
3503
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003504 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003506#ifdef FEAT_FLOAT
3507 if (var2.v_type == VAR_FLOAT)
3508 {
3509 if (!use_float)
3510 {
3511 f1 = n1;
3512 use_float = TRUE;
3513 }
3514 f2 = var2.vval.v_float;
3515 n2 = 0;
3516 }
3517 else
3518#endif
3519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003520 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 clear_tv(&var2);
3522 if (error)
3523 return FAIL;
3524#ifdef FEAT_FLOAT
3525 if (use_float)
3526 f2 = n2;
3527#endif
3528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /*
3531 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003532 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534#ifdef FEAT_FLOAT
3535 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 if (op == '*')
3538 f1 = f1 * f2;
3539 else if (op == '/')
3540 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003541# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003542 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003543 if (f2 == 0.0)
3544 {
3545 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003546 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003547 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003548 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003549 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003550 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003551 }
3552 else
3553 f1 = f1 / f2;
3554# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 // We rely on the floating point library to handle divide
3556 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003558# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003562 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 return FAIL;
3564 }
3565 rettv->v_type = VAR_FLOAT;
3566 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003571 int failed = FALSE;
3572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003573 if (op == '*')
3574 n1 = n1 * n2;
3575 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003576 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003578 n1 = num_modulus(n1, n2, &failed);
3579 if (failed)
3580 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003582 rettv->v_type = VAR_NUMBER;
3583 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 }
3587
3588 return OK;
3589}
3590
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003591/*
3592 * Handle a type cast before a base level expression.
3593 * "arg" must point to the first non-white of the expression.
3594 * "arg" is advanced to just after the recognized expression.
3595 * Return OK or FAIL.
3596 */
3597 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003598eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003599 char_u **arg,
3600 typval_T *rettv,
3601 evalarg_T *evalarg,
3602 int want_string) // after "." operator
3603{
3604 type_T *want_type = NULL;
3605 garray_T type_list; // list of pointers to allocated types
3606 int res;
3607 int evaluate = evalarg == NULL ? 0
3608 : (evalarg->eval_flags & EVAL_EVALUATE);
3609
3610 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003611 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3612 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003613 {
3614 ++*arg;
3615 ga_init2(&type_list, sizeof(type_T *), 10);
3616 want_type = parse_type(arg, &type_list, TRUE);
3617 if (want_type == NULL && (evaluate || **arg != '>'))
3618 {
3619 clear_type_list(&type_list);
3620 return FAIL;
3621 }
3622
3623 if (**arg != '>')
3624 {
3625 if (*skipwhite(*arg) == '>')
3626 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3627 else
3628 emsg(_(e_missing_gt));
3629 clear_type_list(&type_list);
3630 return FAIL;
3631 }
3632 ++*arg;
3633 *arg = skipwhite_and_linebreak(*arg, evalarg);
3634 }
3635
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003636 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003637
3638 if (want_type != NULL && evaluate)
3639 {
3640 if (res == OK)
3641 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003642 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3643 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003644
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003645 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003646 {
3647 if (want_type == &t_bool && actual != &t_bool
3648 && (actual->tt_flags & TTFLAG_BOOL_OK))
3649 {
3650 int n = tv2bool(rettv);
3651
3652 // can use "0" and "1" for boolean in some places
3653 clear_tv(rettv);
3654 rettv->v_type = VAR_BOOL;
3655 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3656 }
3657 else
3658 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003659 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003660
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003661 where.wt_variable = TRUE;
3662 res = check_type(want_type, actual, TRUE, where);
3663 }
3664 }
3665 }
3666 clear_type_list(&type_list);
3667 }
3668
3669 return res;
3670}
3671
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003672 int
3673eval_leader(char_u **arg, int vim9)
3674{
3675 char_u *s = *arg;
3676 char_u *p = *arg;
3677
3678 while (*p == '!' || *p == '-' || *p == '+')
3679 {
3680 char_u *n = skipwhite(p + 1);
3681
3682 // ++, --, -+ and +- are not accepted in Vim9 script
3683 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3684 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003685 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003686 return FAIL;
3687 }
3688 p = n;
3689 }
3690 *arg = p;
3691 return OK;
3692}
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003695 * Check for a predefined value "true", "false" and "null.*".
3696 * Return OK when recognized.
3697 */
3698 int
3699handle_predefined(char_u *s, int len, typval_T *rettv)
3700{
3701 switch (len)
3702 {
3703 case 4: if (STRNCMP(s, "true", 4) == 0)
3704 {
3705 rettv->v_type = VAR_BOOL;
3706 rettv->vval.v_number = VVAL_TRUE;
3707 return OK;
3708 }
3709 if (STRNCMP(s, "null", 4) == 0)
3710 {
3711 rettv->v_type = VAR_SPECIAL;
3712 rettv->vval.v_number = VVAL_NULL;
3713 return OK;
3714 }
3715 break;
3716 case 5: if (STRNCMP(s, "false", 5) == 0)
3717 {
3718 rettv->v_type = VAR_BOOL;
3719 rettv->vval.v_number = VVAL_FALSE;
3720 return OK;
3721 }
3722 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003723 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3724 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003725#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003726 rettv->v_type = VAR_JOB;
3727 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003728#else
3729 rettv->v_type = VAR_SPECIAL;
3730 rettv->vval.v_number = VVAL_NULL;
3731#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003732 return OK;
3733 }
3734 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003735 case 9:
3736 if (STRNCMP(s, "null_", 5) != 0)
3737 break;
3738 if (STRNCMP(s + 5, "list", 4) == 0)
3739 {
3740 rettv->v_type = VAR_LIST;
3741 rettv->vval.v_list = NULL;
3742 return OK;
3743 }
3744 if (STRNCMP(s + 5, "dict", 4) == 0)
3745 {
3746 rettv->v_type = VAR_DICT;
3747 rettv->vval.v_dict = NULL;
3748 return OK;
3749 }
3750 if (STRNCMP(s + 5, "blob", 4) == 0)
3751 {
3752 rettv->v_type = VAR_BLOB;
3753 rettv->vval.v_blob = NULL;
3754 return OK;
3755 }
3756 break;
3757 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3758 {
3759 rettv->v_type = VAR_STRING;
3760 rettv->vval.v_string = NULL;
3761 return OK;
3762 }
3763 break;
3764 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003765 if (STRNCMP(s, "null_channel", 12) == 0)
3766 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003767#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003768 rettv->v_type = VAR_CHANNEL;
3769 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003770#else
3771 rettv->v_type = VAR_SPECIAL;
3772 rettv->vval.v_number = VVAL_NULL;
3773#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003774 return OK;
3775 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003776 if (STRNCMP(s, "null_partial", 12) == 0)
3777 {
3778 rettv->v_type = VAR_PARTIAL;
3779 rettv->vval.v_partial = NULL;
3780 return OK;
3781 }
3782 break;
3783 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3784 {
3785 rettv->v_type = VAR_FUNC;
3786 rettv->vval.v_string = NULL;
3787 return OK;
3788 }
3789 break;
3790 }
3791 return FAIL;
3792}
3793
3794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 * Handle sixth level expression:
3796 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003797 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003798 * "string" string constant
3799 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * &option-name option value
3801 * @r register contents
3802 * identifier variable value
3803 * function() function call
3804 * $VAR environment variable
3805 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003808 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003809 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 *
3811 * Also handle:
3812 * ! in front logical NOT
3813 * - in front unary minus
3814 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003815 * trailing [] subscript in String or List
3816 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003817 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 *
3819 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003820 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 *
3822 * Return OK or FAIL.
3823 */
3824 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003825eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003826 char_u **arg,
3827 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003828 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003831 int evaluate = evalarg != NULL
3832 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 int len;
3834 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003835 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 char_u *start_leader, *end_leader;
3837 int ret = OK;
3838 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003839 static int recurse = 0;
3840 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003843 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003844 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003849 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 */
3851 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003852 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 end_leader = *arg;
3855
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003856 if (**arg == '.' && (!isdigit(*(*arg + 1))
3857#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003858 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003859#endif
3860 ))
3861 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003862 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003863 ++*arg;
3864 return FAIL;
3865 }
3866
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003867 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003868 // and crash. With MSVC the stack is smaller.
3869 if (recurse ==
3870#ifdef _MSC_VER
3871 300
3872#else
3873 1000
3874#endif
3875 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003876 {
3877 semsg(_(e_expression_too_recursive_str), *arg);
3878 return FAIL;
3879 }
3880 ++recurse;
3881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 switch (**arg)
3883 {
3884 /*
3885 * Number constant.
3886 */
3887 case '0':
3888 case '1':
3889 case '2':
3890 case '3':
3891 case '4':
3892 case '5':
3893 case '6':
3894 case '7':
3895 case '8':
3896 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003897 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003898
3899 // Apply prefixed "-" and "+" now. Matters especially when
3900 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003901 if (ret == OK && evaluate && end_leader > start_leader
3902 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003903 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 break;
3905
3906 /*
3907 * String constant: "string".
3908 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003909 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 break;
3911
3912 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003913 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003915 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003916 break;
3917
3918 /*
3919 * List: [expr, expr]
3920 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003921 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003925 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003926 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003927 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003928 {
3929 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3930 }
3931 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003932 {
3933 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003934 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003935 }
3936 else
3937 ret = NOTDONE;
3938 break;
3939
3940 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003941 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003942 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003943 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003944 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003945 ret = NOTDONE;
3946 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003947 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003948 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003949 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003950 break;
3951
3952 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003953 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003955 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957
3958 /*
3959 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003960 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
LemonBoy2eaef102022-05-06 13:14:50 +01003962 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3963 ret = eval_interp_string(arg, rettv, evaluate);
3964 else
3965 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 break;
3967
3968 /*
3969 * Register contents: @r.
3970 */
3971 case '@': ++*arg;
3972 if (evaluate)
3973 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003974 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003975 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003976 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003977 emsg_invreg(**arg);
3978 else
3979 {
3980 rettv->v_type = VAR_STRING;
3981 rettv->vval.v_string = get_reg_contents(**arg,
3982 GREG_EXPR_SRC);
3983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
3985 if (**arg != NUL)
3986 ++*arg;
3987 break;
3988
3989 /*
3990 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003991 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003993 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003994 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003995 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003996 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003997 if (ret == OK && evaluate)
3998 {
3999 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4000
Bram Moolenaara9931532021-06-12 15:58:16 +02004001 // Compile it here to get the return type. The return
4002 // type is optional, when it's missing use t_unknown.
4003 // This is recognized in compile_return().
4004 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4005 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004006 if (compile_def_function(ufunc, FALSE,
4007 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004008 {
4009 clear_tv(rettv);
4010 ret = FAIL;
4011 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004012 }
4013 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004014 if (ret == NOTDONE)
4015 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004016 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004017 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004018
Bram Moolenaar9215f012020-06-27 21:18:00 +02004019 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004020 if (**arg == ')')
4021 ++*arg;
4022 else if (ret == OK)
4023 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004024 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004025 clear_tv(rettv);
4026 ret = FAIL;
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 break;
4030
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
4033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034
4035 if (ret == NOTDONE)
4036 {
4037 /*
4038 * Must be a variable or function name.
4039 * Can also be a curly-braces kind of name: {expr}.
4040 */
4041 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004042 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 if (alias != NULL)
4044 s = alias;
4045
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004046 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 ret = FAIL;
4048 else
4049 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004050 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4051
Bram Moolenaar4525a572022-02-13 11:57:33 +00004052 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004053 {
4054 emsg(_(e_cannot_use_underscore_here));
4055 ret = FAIL;
4056 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004058 && s[0] == 's' && s[1] == ':')
4059 {
4060 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4061 ret = FAIL;
4062 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004063 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004064 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004065 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004066 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004067 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004068 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004069 else if (flags & EVAL_CONSTANT)
4070 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004072 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004073 // get the value of "true", "false", etc. or a variable
4074 ret = FAIL;
4075 if (vim9script)
4076 ret = handle_predefined(s, len, rettv);
4077 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004078 {
4079 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004080 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004081 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004082 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004084 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004085 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004086 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004087 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004088 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004089 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004090 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004091 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 }
4093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4095 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004096 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004097 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 /*
4100 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4101 */
4102 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004103 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004104
4105 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004106 return ret;
4107}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004108
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004109/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004110 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004111 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004112 * Adjusts "end_leaderp" until it is at "start_leader".
4113 */
4114 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004115eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004116 typval_T *rettv,
4117 int numeric_only,
4118 char_u *start_leader,
4119 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004120{
4121 char_u *end_leader = *end_leaderp;
4122 int ret = OK;
4123 int error = FALSE;
4124 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004125 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004126 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004127#ifdef FEAT_FLOAT
4128 float_T f = 0.0;
4129
4130 if (rettv->v_type == VAR_FLOAT)
4131 f = rettv->vval.v_float;
4132 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004133#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004134 {
4135 while (VIM_ISWHITE(end_leader[-1]))
4136 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004137 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004138 val = tv2bool(rettv);
4139 else
4140 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004141 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004142 if (error)
4143 {
4144 clear_tv(rettv);
4145 ret = FAIL;
4146 }
4147 else
4148 {
4149 while (end_leader > start_leader)
4150 {
4151 --end_leader;
4152 if (*end_leader == '!')
4153 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004154 if (numeric_only)
4155 {
4156 ++end_leader;
4157 break;
4158 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004159#ifdef FEAT_FLOAT
4160 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004161 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004162 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004163 {
4164 rettv->v_type = VAR_BOOL;
4165 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4166 }
4167 else
4168 f = !f;
4169 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004170 else
4171#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004172 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004173 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004174 type = VAR_BOOL;
4175 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004176 }
4177 else if (*end_leader == '-')
4178 {
4179#ifdef FEAT_FLOAT
4180 if (rettv->v_type == VAR_FLOAT)
4181 f = -f;
4182 else
4183#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004184 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004185 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004186 type = VAR_NUMBER;
4187 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004188 }
4189 }
4190#ifdef FEAT_FLOAT
4191 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004194 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004197#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004199 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004200 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004201 rettv->v_type = type;
4202 else
4203 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004204 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004207 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return ret;
4209}
4210
4211/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004212 * Call the function referred to in "rettv".
4213 */
4214 static int
4215call_func_rettv(
4216 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004217 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004218 typval_T *rettv,
4219 int evaluate,
4220 dict_T *selfdict,
4221 typval_T *basetv)
4222{
4223 partial_T *pt = NULL;
4224 funcexe_T funcexe;
4225 typval_T functv;
4226 char_u *s;
4227 int ret;
4228
4229 // need to copy the funcref so that we can clear rettv
4230 if (evaluate)
4231 {
4232 functv = *rettv;
4233 rettv->v_type = VAR_UNKNOWN;
4234
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004235 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004236 if (functv.v_type == VAR_PARTIAL)
4237 {
4238 pt = functv.vval.v_partial;
4239 s = partial_name(pt);
4240 }
4241 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004242 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004243 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004244 if (s == NULL || *s == NUL)
4245 {
4246 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004247 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004248 goto theend;
4249 }
4250 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004251 }
4252 else
4253 s = (char_u *)"";
4254
Bram Moolenaara80faa82020-04-12 19:37:17 +02004255 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004256 funcexe.fe_firstline = curwin->w_cursor.lnum;
4257 funcexe.fe_lastline = curwin->w_cursor.lnum;
4258 funcexe.fe_evaluate = evaluate;
4259 funcexe.fe_partial = pt;
4260 funcexe.fe_selfdict = selfdict;
4261 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004262 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004263
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004264theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // Clear the funcref afterwards, so that deleting it while
4266 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004267 if (evaluate)
4268 clear_tv(&functv);
4269
4270 return ret;
4271}
4272
4273/*
4274 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004275 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004276 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4277 */
4278 static int
4279eval_lambda(
4280 char_u **arg,
4281 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004282 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004283 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004284{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004285 int evaluate = evalarg != NULL
4286 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004287 typval_T base = *rettv;
4288 int ret;
4289
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004290 rettv->v_type = VAR_UNKNOWN;
4291
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004292 if (**arg == '{')
4293 {
4294 // ->{lambda}()
4295 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4296 }
4297 else
4298 {
4299 // ->(lambda)()
4300 ++*arg;
4301 ret = eval1(arg, rettv, evalarg);
4302 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004303 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004304 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004305 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004306 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004307 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004308 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4309 && rettv->v_type != VAR_PARTIAL)
4310 {
4311 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4312 return FAIL;
4313 }
4314 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004315 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004316 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004317 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004318
4319 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004320 {
4321 if (verbose)
4322 {
4323 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004324 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004325 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004326 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004327 }
4328 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004329 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004330 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004331 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004332 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004333
4334 // Clear the funcref afterwards, so that deleting it while
4335 // evaluating the arguments is possible (see test55).
4336 if (evaluate)
4337 clear_tv(&base);
4338
4339 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004340}
4341
4342/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004343 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004344 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004345 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4346 */
4347 static int
4348eval_method(
4349 char_u **arg,
4350 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004351 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004352 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004353{
4354 char_u *name;
4355 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004356 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004357 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004358 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004359 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004360 int evaluate = evalarg != NULL
4361 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004362
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004363 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004364
Bram Moolenaarac92e252019-08-03 21:58:38 +02004365 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004366 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004367 if (alias != NULL)
4368 name = alias;
4369
4370 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004371 {
4372 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004373 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004374 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004375 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004377 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004378 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004379
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004380 // If there is no "(" immediately following, but there is further on,
4381 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4382 // Does not handle anything where "(" is part of the expression.
4383 *arg = skipwhite(*arg);
4384
4385 if (**arg != '(' && alias == NULL
4386 && (paren = vim_strchr(*arg, '(')) != NULL)
4387 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004388 char_u *deref;
4389
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004390 *arg = name;
4391 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004392 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4393 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004394 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004395 *arg = name + len;
4396 ret = FAIL;
4397 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004398 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004399 {
4400 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004401 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004402 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004403 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004404 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004405
4406 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004407 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004408 *arg = skipwhite(*arg);
4409
4410 if (**arg != '(')
4411 {
4412 if (verbose)
4413 semsg(_(e_missing_parenthesis_str), name);
4414 ret = FAIL;
4415 }
4416 else if (VIM_ISWHITE((*arg)[-1]))
4417 {
4418 if (verbose)
4419 emsg(_(e_no_white_space_allowed_before_parenthesis));
4420 ret = FAIL;
4421 }
4422 else
4423 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004424 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004425 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004426 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004427
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004428 // Clear the funcref afterwards, so that deleting it while
4429 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004430 if (evaluate)
4431 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004432 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004433
Bram Moolenaarac92e252019-08-03 21:58:38 +02004434 return ret;
4435}
4436
4437/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004438 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4439 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004440 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4441 */
4442 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004443eval_index(
4444 char_u **arg,
4445 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004446 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004449 int evaluate = evalarg != NULL
4450 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004451 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004455 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004456 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004458 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4459 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004461 init_tv(&var1);
4462 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 /*
4466 * dict.name
4467 */
4468 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004469 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004473 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 }
4475 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004477 /*
4478 * something[idx]
4479 *
4480 * Get the (first) variable from inside the [].
4481 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004482 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 if (**arg == ':')
4484 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004485 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004487 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004488 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004489 semsg(_(e_white_space_required_before_and_after_str_at_str),
4490 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004491 clear_tv(&var1);
4492 return FAIL;
4493 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004494 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004495 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004496 int error = FALSE;
4497
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004498#ifdef FEAT_FLOAT
4499 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004500 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004501 && var1.v_type == VAR_FLOAT)
4502 {
4503 var1.vval.v_string = typval_tostring(&var1, TRUE);
4504 var1.v_type = VAR_STRING;
4505 }
4506#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004507 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004508 tv_get_number_chk(&var1, &error);
4509 else
4510 error = tv_get_string_chk(&var1) == NULL;
4511 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004512 {
4513 // not a number or string
4514 clear_tv(&var1);
4515 return FAIL;
4516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004518
4519 /*
4520 * Get the second variable from inside the [:].
4521 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004522 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523 if (**arg == ':')
4524 {
4525 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004526 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004527 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004528 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004529 semsg(_(e_white_space_required_before_and_after_str_at_str),
4530 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004531 if (!empty1)
4532 clear_tv(&var1);
4533 return FAIL;
4534 }
4535 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 if (**arg == ']')
4537 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004538 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004540 if (!empty1)
4541 clear_tv(&var1);
4542 return FAIL;
4543 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004544 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 if (!empty1)
4548 clear_tv(&var1);
4549 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 return FAIL;
4551 }
4552 }
4553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004555 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004556 if (**arg != ']')
4557 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004558 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004559 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 clear_tv(&var1);
4561 if (range)
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004565 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 }
4567
4568 if (evaluate)
4569 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004570 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004571 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004573
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004577 clear_tv(&var2);
4578 return res;
4579 }
4580 return OK;
4581}
4582
4583/*
4584 * Check if "rettv" can have an [index] or [sli:ce]
4585 */
4586 int
4587check_can_index(typval_T *rettv, int evaluate, int verbose)
4588{
4589 switch (rettv->v_type)
4590 {
4591 case VAR_FUNC:
4592 case VAR_PARTIAL:
4593 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004594 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004595 return FAIL;
4596 case VAR_FLOAT:
4597#ifdef FEAT_FLOAT
4598 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004599 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004600 return FAIL;
4601#endif
4602 case VAR_BOOL:
4603 case VAR_SPECIAL:
4604 case VAR_JOB:
4605 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004606 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004607 if (verbose)
4608 emsg(_(e_cannot_index_special_variable));
4609 return FAIL;
4610 case VAR_UNKNOWN:
4611 case VAR_ANY:
4612 case VAR_VOID:
4613 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004615 emsg(_(e_cannot_index_special_variable));
4616 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004618 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004620 case VAR_STRING:
4621 case VAR_LIST:
4622 case VAR_DICT:
4623 case VAR_BLOB:
4624 break;
4625 case VAR_NUMBER:
4626 if (in_vim9script())
4627 emsg(_(e_cannot_index_number));
4628 break;
4629 }
4630 return OK;
4631}
4632
4633/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004634 * slice() function
4635 */
4636 void
4637f_slice(typval_T *argvars, typval_T *rettv)
4638{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004639 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004640 && ((argvars[0].v_type != VAR_STRING
4641 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004642 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004643 && check_for_list_arg(argvars, 0) == FAIL)
4644 || check_for_number_arg(argvars, 1) == FAIL
4645 || check_for_opt_number_arg(argvars, 2) == FAIL))
4646 return;
4647
Bram Moolenaar6601b622021-01-13 21:47:15 +01004648 if (check_can_index(argvars, TRUE, FALSE) == OK)
4649 {
4650 copy_tv(argvars, rettv);
4651 eval_index_inner(rettv, TRUE, argvars + 1,
4652 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4653 TRUE, NULL, 0, FALSE);
4654 }
4655}
4656
4657/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 * Apply index or range to "rettv".
4659 * "var1" is the first index, NULL for [:expr].
4660 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004661 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4662 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4664 */
4665 int
4666eval_index_inner(
4667 typval_T *rettv,
4668 int is_range,
4669 typval_T *var1,
4670 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004671 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 char_u *key,
4673 int keylen,
4674 int verbose)
4675{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004676 varnumber_T n1, n2 = 0;
4677 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678
4679 n1 = 0;
4680 if (var1 != NULL && rettv->v_type != VAR_DICT)
4681 n1 = tv_get_number(var1);
4682
4683 if (is_range)
4684 {
4685 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004687 if (verbose)
4688 emsg(_(e_cannot_slice_dictionary));
4689 return FAIL;
4690 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004691 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004693 else
4694 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004695 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004696
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004697 switch (rettv->v_type)
4698 {
4699 case VAR_UNKNOWN:
4700 case VAR_ANY:
4701 case VAR_VOID:
4702 case VAR_FUNC:
4703 case VAR_PARTIAL:
4704 case VAR_FLOAT:
4705 case VAR_BOOL:
4706 case VAR_SPECIAL:
4707 case VAR_JOB:
4708 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004709 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004710 break; // not evaluating, skipping over subscript
4711
4712 case VAR_NUMBER:
4713 case VAR_STRING:
4714 {
4715 char_u *s = tv_get_string(rettv);
4716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004718 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004719 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004720 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004721 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004722 else
4723 s = char_from_string(s, n1);
4724 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004725 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004727 // The resulting variable is a substring. If the indexes
4728 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 if (n1 < 0)
4730 {
4731 n1 = len + n1;
4732 if (n1 < 0)
4733 n1 = 0;
4734 }
4735 if (n2 < 0)
4736 n2 = len + n2;
4737 else if (n2 >= len)
4738 n2 = len;
4739 if (n1 >= len || n2 < 0 || n1 > n2)
4740 s = NULL;
4741 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004742 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 }
4744 else
4745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // The resulting variable is a string of a single
4747 // character. If the index is too big or negative the
4748 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 if (n1 >= len || n1 < 0)
4750 s = NULL;
4751 else
4752 s = vim_strnsave(s + n1, 1);
4753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 clear_tv(rettv);
4755 rettv->v_type = VAR_STRING;
4756 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004757 }
4758 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004760 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004761 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4762 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004763 break;
4764
4765 case VAR_LIST:
4766 if (var1 == NULL)
4767 n1 = 0;
4768 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004769 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004771 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 return FAIL;
4773 break;
4774
4775 case VAR_DICT:
4776 {
4777 dictitem_T *item;
4778 typval_T tmp;
4779
4780 if (key == NULL)
4781 {
4782 key = tv_get_string_chk(var1);
4783 if (key == NULL)
4784 return FAIL;
4785 }
4786
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004787 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004788
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004789 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004790 {
4791 if (verbose)
4792 {
4793 if (keylen > 0)
4794 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004795 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004796 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004797 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004798 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799
4800 copy_tv(&item->di_tv, &tmp);
4801 clear_tv(rettv);
4802 *rettv = tmp;
4803 }
4804 break;
4805 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 return OK;
4807}
4808
4809/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004810 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004811 */
4812 char_u *
4813partial_name(partial_T *pt)
4814{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004815 if (pt != NULL)
4816 {
4817 if (pt->pt_name != NULL)
4818 return pt->pt_name;
4819 if (pt->pt_func != NULL)
4820 return pt->pt_func->uf_name;
4821 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004822 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004823}
4824
Bram Moolenaarddecc252016-04-06 22:59:37 +02004825 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004826partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004827{
4828 int i;
4829
4830 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004832 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004833 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004834 if (pt->pt_name != NULL)
4835 {
4836 func_unref(pt->pt_name);
4837 vim_free(pt->pt_name);
4838 }
4839 else
4840 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004841
Bram Moolenaar54656012021-06-09 20:50:46 +02004842 // "out_up" is no longer used, decrement refcount on partial that owns it.
4843 partial_unref(pt->pt_outer.out_up_partial);
4844
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004845 // Using pt_outer from another partial.
4846 partial_unref(pt->pt_outer_partial);
4847
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004848 // Decrease the reference count for the context of a closure. If down
4849 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004850 if (pt->pt_funcstack != NULL)
4851 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004852 --pt->pt_funcstack->fs_refcount;
4853 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004854 }
4855
Bram Moolenaarddecc252016-04-06 22:59:37 +02004856 vim_free(pt);
4857}
4858
4859/*
4860 * Unreference a closure: decrement the reference count and free it when it
4861 * becomes zero.
4862 */
4863 void
4864partial_unref(partial_T *pt)
4865{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004866 if (pt != NULL)
4867 {
4868 if (--pt->pt_refcount <= 0)
4869 partial_free(pt);
4870
4871 // If the reference count goes down to one, the funcstack may be the
4872 // only reference and can be freed if no other partials reference it.
4873 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4874 funcstack_check_refcount(pt->pt_funcstack);
4875 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004876}
4877
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004878/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004879 * Return the next (unique) copy ID.
4880 * Used for serializing nested structures.
4881 */
4882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004883get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004884{
4885 current_copyID += COPYID_INC;
4886 return current_copyID;
4887}
4888
4889/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004890 * Garbage collection for lists and dictionaries.
4891 *
4892 * We use reference counts to be able to free most items right away when they
4893 * are no longer used. But for composite items it's possible that it becomes
4894 * unused while the reference count is > 0: When there is a recursive
4895 * reference. Example:
4896 * :let l = [1, 2, 3]
4897 * :let d = {9: l}
4898 * :let l[1] = d
4899 *
4900 * Since this is quite unusual we handle this with garbage collection: every
4901 * once in a while find out which lists and dicts are not referenced from any
4902 * variable.
4903 *
4904 * Here is a good reference text about garbage collection (refers to Python
4905 * but it applies to all reference-counting mechanisms):
4906 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004907 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004908
4909/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004910 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004911 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004912 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004913 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004914 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004915garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004916{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004917 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004918 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004919 buf_T *buf;
4920 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004921 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004922 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004923
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004924 if (!testing)
4925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004927 want_garbage_collect = FALSE;
4928 may_garbage_collect = FALSE;
4929 garbage_collect_at_exit = FALSE;
4930 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004931
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004932 // The execution stack can grow big, limit the size.
4933 if (exestack.ga_maxlen - exestack.ga_len > 500)
4934 {
4935 size_t new_len;
4936 char_u *pp;
4937 int n;
4938
4939 // Keep 150% of the current size, with a minimum of the growth size.
4940 n = exestack.ga_len / 2;
4941 if (n < exestack.ga_growsize)
4942 n = exestack.ga_growsize;
4943
4944 // Don't make it bigger though.
4945 if (exestack.ga_len + n < exestack.ga_maxlen)
4946 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004947 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004948 pp = vim_realloc(exestack.ga_data, new_len);
4949 if (pp == NULL)
4950 return FAIL;
4951 exestack.ga_maxlen = exestack.ga_len + n;
4952 exestack.ga_data = pp;
4953 }
4954 }
4955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 // We advance by two because we add one for items referenced through
4957 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004958 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004960 /*
4961 * 1. Go through all accessible variables and mark all lists and dicts
4962 * with copyID.
4963 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // Don't free variables in the previous_funccal list unless they are only
4966 // referenced through previous_funccal. This must be first, because if
4967 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004971 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004974 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004975 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4976 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004979 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004980 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4981 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004982 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004983 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4984 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004985#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004986 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004987 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4988 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004989 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004990 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004991 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4992 NULL, NULL);
4993#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004996 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004997 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4998 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005000 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005002 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005006 abort = abort || set_ref_in_functions(copyID);
5007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005010
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005011 // funcstacks keep variables for closures
5012 abort = abort || set_ref_in_funcstacks(copyID);
5013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005014 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005015 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005016
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005017 // callbacks in buffers
5018 abort = abort || set_ref_in_buffers(copyID);
5019
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005020 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5021 abort = abort || set_ref_in_insexpand_funcs(copyID);
5022
5023 // 'operatorfunc' callback
5024 abort = abort || set_ref_in_opfunc(copyID);
5025
5026 // 'tagfunc' callback
5027 abort = abort || set_ref_in_tagfunc(copyID);
5028
5029 // 'imactivatefunc' and 'imstatusfunc' callbacks
5030 abort = abort || set_ref_in_im_funcs(copyID);
5031
Bram Moolenaar1dced572012-04-05 16:54:08 +02005032#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005033 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005034#endif
5035
Bram Moolenaardb913952012-06-29 12:54:53 +02005036#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005037 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005038#endif
5039
5040#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005041 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005042#endif
5043
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005045 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005046 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005047#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005048#ifdef FEAT_NETBEANS_INTG
5049 abort = abort || set_ref_in_nb_channel(copyID);
5050#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005051
Bram Moolenaare3188e22016-05-31 21:13:04 +02005052#ifdef FEAT_TIMERS
5053 abort = abort || set_ref_in_timer(copyID);
5054#endif
5055
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005056#ifdef FEAT_QUICKFIX
5057 abort = abort || set_ref_in_quickfix(copyID);
5058#endif
5059
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005060#ifdef FEAT_TERMINAL
5061 abort = abort || set_ref_in_term(copyID);
5062#endif
5063
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005064#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005065 abort = abort || set_ref_in_popups(copyID);
5066#endif
5067
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005068 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005069 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 /*
5071 * 2. Free lists and dictionaries that are not referenced.
5072 */
5073 did_free = free_unref_items(copyID);
5074
5075 /*
5076 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005078 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005080 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005081 else if (p_verbose > 0)
5082 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005083 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005084 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005085
5086 return did_free;
5087}
5088
5089/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005090 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005091 */
5092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005093free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005094{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005095 int did_free = FALSE;
5096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // Let all "free" functions know that we are here. This means no
5098 // dictionaries, lists, channels or jobs are to be freed, because we will
5099 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005100 in_free_unref_items = TRUE;
5101
5102 /*
5103 * PASS 1: free the contents of the items. We don't free the items
5104 * themselves yet, so that it is possible to decrement refcount counters
5105 */
5106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005108 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005111 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112
5113#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // Go through the list of jobs and free items without the copyID. This
5115 // must happen before doing channels, because jobs refer to channels, but
5116 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005117 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005120 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5121#endif
5122
5123 /*
5124 * PASS 2: free the items themselves.
5125 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005126 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005127 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005128
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 // Go through the list of jobs and free items without the copyID. This
5131 // must happen before doing channels, because jobs refer to channels, but
5132 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005133 free_unused_jobs(copyID, COPYID_MASK);
5134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136 free_unused_channels(copyID, COPYID_MASK);
5137#endif
5138
5139 in_free_unref_items = FALSE;
5140
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005141 return did_free;
5142}
5143
5144/*
5145 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005146 * "list_stack" is used to add lists to be marked. Can be NULL.
5147 *
5148 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005152{
5153 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005154 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005155 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005156 hashtab_T *cur_ht;
5157 ht_stack_T *ht_stack = NULL;
5158 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005159
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005160 cur_ht = ht;
5161 for (;;)
5162 {
5163 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Mark each item in the hashtab. If the item contains a hashtab
5166 // it is added to ht_stack, if it contains a list it is added to
5167 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 todo = (int)cur_ht->ht_used;
5169 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5170 if (!HASHITEM_EMPTY(hi))
5171 {
5172 --todo;
5173 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5174 &ht_stack, list_stack);
5175 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005176 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005177
5178 if (ht_stack == NULL)
5179 break;
5180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005181 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005182 cur_ht = ht_stack->ht;
5183 tempitem = ht_stack;
5184 ht_stack = ht_stack->prev;
5185 free(tempitem);
5186 }
5187
5188 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189}
5190
Dominique Pelle748b3082022-01-08 12:41:16 +00005191#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5192 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005193/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005194 * Mark a dict and its items with "copyID".
5195 * Returns TRUE if setting references failed somehow.
5196 */
5197 int
5198set_ref_in_dict(dict_T *d, int copyID)
5199{
5200 if (d != NULL && d->dv_copyID != copyID)
5201 {
5202 d->dv_copyID = copyID;
5203 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5204 }
5205 return FALSE;
5206}
Dominique Pelle748b3082022-01-08 12:41:16 +00005207#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005208
5209/*
5210 * Mark a list and its items with "copyID".
5211 * Returns TRUE if setting references failed somehow.
5212 */
5213 int
5214set_ref_in_list(list_T *ll, int copyID)
5215{
5216 if (ll != NULL && ll->lv_copyID != copyID)
5217 {
5218 ll->lv_copyID = copyID;
5219 return set_ref_in_list_items(ll, copyID, NULL);
5220 }
5221 return FALSE;
5222}
5223
5224/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005226 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5227 *
5228 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005230 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005231set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005233 listitem_T *li;
5234 int abort = FALSE;
5235 list_T *cur_l;
5236 list_stack_T *list_stack = NULL;
5237 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005239 cur_l = l;
5240 for (;;)
5241 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005242 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005243 // Mark each item in the list. If the item contains a hashtab
5244 // it is added to ht_stack, if it contains a list it is added to
5245 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5247 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5248 ht_stack, &list_stack);
5249 if (list_stack == NULL)
5250 break;
5251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005253 cur_l = list_stack->list;
5254 tempitem = list_stack;
5255 list_stack = list_stack->prev;
5256 free(tempitem);
5257 }
5258
5259 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260}
5261
5262/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005263 * Mark the partial in callback 'cb' with "copyID".
5264 */
5265 int
5266set_ref_in_callback(callback_T *cb, int copyID)
5267{
5268 typval_T tv;
5269
5270 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5271 return FALSE;
5272
5273 tv.v_type = VAR_PARTIAL;
5274 tv.vval.v_partial = cb->cb_partial;
5275 return set_ref_in_item(&tv, copyID, NULL, NULL);
5276}
5277
5278/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005279 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005280 * "list_stack" is used to add lists to be marked. Can be NULL.
5281 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5282 *
5283 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286set_ref_in_item(
5287 typval_T *tv,
5288 int copyID,
5289 ht_stack_T **ht_stack,
5290 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005292 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005293
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005294 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005295 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005296 dict_T *dd = tv->vval.v_dict;
5297
Bram Moolenaara03f2332016-02-06 18:09:59 +01005298 if (dd != NULL && dd->dv_copyID != copyID)
5299 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005300 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005301 dd->dv_copyID = copyID;
5302 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005303 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005304 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5305 }
5306 else
5307 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005308 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5309
Bram Moolenaara03f2332016-02-06 18:09:59 +01005310 if (newitem == NULL)
5311 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005312 else
5313 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005314 newitem->ht = &dd->dv_hashtab;
5315 newitem->prev = *ht_stack;
5316 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005317 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005318 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005319 }
5320 }
5321 else if (tv->v_type == VAR_LIST)
5322 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005323 list_T *ll = tv->vval.v_list;
5324
Bram Moolenaara03f2332016-02-06 18:09:59 +01005325 if (ll != NULL && ll->lv_copyID != copyID)
5326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005328 ll->lv_copyID = copyID;
5329 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005330 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005331 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005332 }
5333 else
5334 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005335 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5336
Bram Moolenaara03f2332016-02-06 18:09:59 +01005337 if (newitem == NULL)
5338 abort = TRUE;
5339 else
5340 {
5341 newitem->list = ll;
5342 newitem->prev = *list_stack;
5343 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005344 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005345 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005346 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005347 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005348 else if (tv->v_type == VAR_FUNC)
5349 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005350 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005351 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005352 else if (tv->v_type == VAR_PARTIAL)
5353 {
5354 partial_T *pt = tv->vval.v_partial;
5355 int i;
5356
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005357 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005358 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005359 // Didn't see this partial yet.
5360 pt->pt_copyID = copyID;
5361
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005362 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005363
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005364 if (pt->pt_dict != NULL)
5365 {
5366 typval_T dtv;
5367
5368 dtv.v_type = VAR_DICT;
5369 dtv.vval.v_dict = pt->pt_dict;
5370 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5371 }
5372
5373 for (i = 0; i < pt->pt_argc; ++i)
5374 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5375 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005376 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377 }
5378 }
5379#ifdef FEAT_JOB_CHANNEL
5380 else if (tv->v_type == VAR_JOB)
5381 {
5382 job_T *job = tv->vval.v_job;
5383 typval_T dtv;
5384
5385 if (job != NULL && job->jv_copyID != copyID)
5386 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005387 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388 if (job->jv_channel != NULL)
5389 {
5390 dtv.v_type = VAR_CHANNEL;
5391 dtv.vval.v_channel = job->jv_channel;
5392 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5393 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005394 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005395 {
5396 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005397 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5399 }
5400 }
5401 }
5402 else if (tv->v_type == VAR_CHANNEL)
5403 {
5404 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005405 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005406 typval_T dtv;
5407 jsonq_T *jq;
5408 cbq_T *cq;
5409
5410 if (ch != NULL && ch->ch_copyID != copyID)
5411 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005412 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005413 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005414 {
5415 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5416 jq = jq->jq_next)
5417 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5418 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5419 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005420 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005421 {
5422 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005423 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5425 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005426 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005427 {
5428 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005429 dtv.vval.v_partial =
5430 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005431 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5432 }
5433 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005434 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005435 {
5436 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005437 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005438 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5439 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005440 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441 {
5442 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005443 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5445 }
5446 }
5447 }
5448#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005449 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005450}
5451
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 * Return a string with the string representation of a variable.
5454 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005456 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005457 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005458 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005459 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005460 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5461 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005462 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005464 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005465echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005466 typval_T *tv,
5467 char_u **tofree,
5468 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005469 int copyID,
5470 int echo_style,
5471 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005472 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005474 static int recurse = 0;
5475 char_u *r = NULL;
5476
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005478 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005479 if (!did_echo_string_emsg)
5480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005481 // Only give this message once for a recursive call to avoid
5482 // flooding the user with errors. And stop iterating over lists
5483 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005484 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005485 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005487 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005488 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005489 }
5490 ++recurse;
5491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 switch (tv->v_type)
5493 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005495 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005496 {
5497 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005498 r = tv->vval.v_string;
5499 if (r == NULL)
5500 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005501 }
5502 else
5503 {
5504 *tofree = string_quote(tv->vval.v_string, FALSE);
5505 r = *tofree;
5506 }
5507 break;
5508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005510 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005511 char_u buf[MAX_FUNC_NAME_LEN];
5512
5513 if (echo_style)
5514 {
LemonBoya5d35902022-04-29 21:15:02 +01005515 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5516 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005517 buf, MAX_FUNC_NAME_LEN);
5518 if (r == buf)
5519 {
5520 r = vim_strsave(buf);
5521 *tofree = r;
5522 }
5523 else
5524 *tofree = NULL;
5525 }
5526 else
5527 {
5528 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5529 : make_ufunc_name_readable(
5530 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5531 TRUE);
5532 r = *tofree;
5533 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005534 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005535 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005536
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005538 {
5539 partial_T *pt = tv->vval.v_partial;
5540 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005541 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005542 garray_T ga;
5543 int i;
5544 char_u *tf;
5545
5546 ga_init2(&ga, 1, 100);
5547 ga_concat(&ga, (char_u *)"function(");
5548 if (fname != NULL)
5549 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005550 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005551 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005552 && vim_isupper(fname[1]))
5553 {
5554 ga_concat(&ga, (char_u *)"'g:");
5555 ga_concat(&ga, fname + 1);
5556 }
5557 else
5558 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005559 vim_free(fname);
5560 }
5561 if (pt != NULL && pt->pt_argc > 0)
5562 {
5563 ga_concat(&ga, (char_u *)", [");
5564 for (i = 0; i < pt->pt_argc; ++i)
5565 {
5566 if (i > 0)
5567 ga_concat(&ga, (char_u *)", ");
5568 ga_concat(&ga,
5569 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5570 vim_free(tf);
5571 }
5572 ga_concat(&ga, (char_u *)"]");
5573 }
5574 if (pt != NULL && pt->pt_dict != NULL)
5575 {
5576 typval_T dtv;
5577
5578 ga_concat(&ga, (char_u *)", ");
5579 dtv.v_type = VAR_DICT;
5580 dtv.vval.v_dict = pt->pt_dict;
5581 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5582 vim_free(tf);
5583 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005584 // terminate with ')' and a NUL
5585 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005586
5587 *tofree = ga.ga_data;
5588 r = *tofree;
5589 break;
5590 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005591
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005592 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005593 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005594 break;
5595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005596 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005597 if (tv->vval.v_list == NULL)
5598 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005599 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005600 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005601 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005603 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5604 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005605 {
5606 *tofree = NULL;
5607 r = (char_u *)"[...]";
5608 }
5609 else
5610 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005611 int old_copyID = tv->vval.v_list->lv_copyID;
5612
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005613 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005614 *tofree = list2string(tv, copyID, restore_copyID);
5615 if (restore_copyID)
5616 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005617 r = *tofree;
5618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 if (tv->vval.v_dict == NULL)
5623 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005624 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005626 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005628 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5629 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005630 {
5631 *tofree = NULL;
5632 r = (char_u *)"{...}";
5633 }
5634 else
5635 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005636 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005637
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005639 *tofree = dict2string(tv, copyID, restore_copyID);
5640 if (restore_copyID)
5641 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005642 r = *tofree;
5643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005647 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005648 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005649 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005650 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005651 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005652 break;
5653
Bram Moolenaar835dc632016-02-07 14:27:38 +01005654 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005655 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005656#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005657 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005658 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5659 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005660 if (composite_val)
5661 {
5662 *tofree = string_quote(r, FALSE);
5663 r = *tofree;
5664 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005665#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005667
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005668 case VAR_INSTR:
5669 *tofree = NULL;
5670 r = (char_u *)"instructions";
5671 break;
5672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005673 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005674#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005675 *tofree = NULL;
5676 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5677 r = numbuf;
5678 break;
5679#endif
5680
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005681 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005682 case VAR_SPECIAL:
5683 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005684 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005687
Bram Moolenaar8502c702014-06-17 12:51:16 +02005688 if (--recurse == 0)
5689 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005690 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005691}
5692
5693/*
5694 * Return a string with the string representation of a variable.
5695 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5696 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005697 * Does not put quotes around strings, as ":echo" displays values.
5698 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5699 * May return NULL.
5700 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005702echo_string(
5703 typval_T *tv,
5704 char_u **tofree,
5705 char_u *numbuf,
5706 int copyID)
5707{
5708 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5709}
5710
5711/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005712 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5713 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005714 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005715 */
5716 int
5717buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5718{
5719 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005720 char_u *t;
5721 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005722
5723 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5724 return -1;
5725
5726 if (lnum > buf->b_ml.ml_line_count)
5727 lnum = buf->b_ml.ml_line_count;
5728
5729 str = ml_get_buf(buf, lnum, FALSE);
5730 if (str == NULL)
5731 return -1;
5732
5733 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005734 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005735
Bram Moolenaar91458462021-01-13 20:08:38 +01005736 // count the number of characters
5737 t = str;
5738 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5739 t += mb_ptr2len(t);
5740
5741 // In insert mode, when the cursor is at the end of a non-empty line,
5742 // byteidx points to the NUL character immediately past the end of the
5743 // string. In this case, add one to the character count.
5744 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5745 count++;
5746
5747 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005748}
5749
5750/*
5751 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005752 * byte index. Works only for loaded buffers. Returns -1 on failure.
5753 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005754 */
5755 int
5756buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5757{
5758 char_u *str;
5759 char_u *t;
5760
5761 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5762 return -1;
5763
5764 if (lnum > buf->b_ml.ml_line_count)
5765 lnum = buf->b_ml.ml_line_count;
5766
5767 str = ml_get_buf(buf, lnum, FALSE);
5768 if (str == NULL)
5769 return -1;
5770
5771 // Convert the character offset to a byte offset
5772 t = str;
5773 while (*t != NUL && --charidx > 0)
5774 t += mb_ptr2len(t);
5775
Bram Moolenaar91458462021-01-13 20:08:38 +01005776 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005777}
5778
5779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005781 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005783 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005784var2fpos(
5785 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005786 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005787 int *fnum, // set to fnum for '0, 'A, etc.
5788 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005790 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005792 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005794 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005795 if (varp->v_type == VAR_LIST)
5796 {
5797 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005798 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005799 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005800 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005801
5802 l = varp->vval.v_list;
5803 if (l == NULL)
5804 return NULL;
5805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005807 pos.lnum = list_find_nr(l, 0L, &error);
5808 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005809 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005810 if (charcol)
5811 len = (long)mb_charlen(ml_get(pos.lnum));
5812 else
5813 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005814
Bram Moolenaarec65d772020-08-20 22:29:12 +02005815 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005816 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005817 li = list_find(l, 1L);
5818 if (li != NULL && li->li_tv.v_type == VAR_STRING
5819 && li->li_tv.vval.v_string != NULL
5820 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005821 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005822 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005823 }
5824 else
5825 {
5826 pos.col = list_find_nr(l, 1L, &error);
5827 if (error)
5828 return NULL;
5829 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005831 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005832 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005834 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005837 pos.coladd = list_find_nr(l, 2L, &error);
5838 if (error)
5839 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005840
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005841 return &pos;
5842 }
5843
Bram Moolenaarc5809432021-03-27 21:23:30 +01005844 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5845 return NULL;
5846
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005847 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005848 if (name == NULL)
5849 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005850
5851 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005852 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005853 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005854 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005855 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005856 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005857 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005858 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005859 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005860 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005861 pos = VIsual;
5862 else
5863 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005864 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005865 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005866 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005868 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005869 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5871 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005872 pos = *pp;
5873 }
5874 if (pos.lnum != 0)
5875 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005876 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005877 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5878 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005880
Bram Moolenaara5525202006-03-02 22:52:09 +00005881 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005882
Bram Moolenaar477933c2007-07-17 14:32:23 +00005883 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005884 {
5885 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005886 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005887 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005888 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 // In silent Ex mode topline is zero, but that's not a valid line
5890 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005891 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005892 return &pos;
5893 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005894 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005895 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005898 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005899 return &pos;
5900 }
5901 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005902 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005904 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
5906 pos.lnum = curbuf->b_ml.ml_line_count;
5907 pos.col = 0;
5908 }
5909 else
5910 {
5911 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005912 if (charcol)
5913 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5914 else
5915 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 return &pos;
5918 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005919 if (in_vim9script())
5920 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 return NULL;
5922}
5923
5924/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005925 * Convert list in "arg" into a position and optional file number.
5926 * When "fnump" is NULL there is no file number, only 3 items.
5927 * Note that the column is passed on as-is, the caller may want to decrement
5928 * it to use 1 for the first column.
5929 * Return FAIL when conversion is not possible, doesn't check the position for
5930 * validity.
5931 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005933list2fpos(
5934 typval_T *arg,
5935 pos_T *posp,
5936 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005937 colnr_T *curswantp,
5938 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005939{
5940 list_T *l = arg->vval.v_list;
5941 long i = 0;
5942 long n;
5943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5945 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005946 if (arg->v_type != VAR_LIST
5947 || l == NULL
5948 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005949 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005950 return FAIL;
5951
5952 if (fnump != NULL)
5953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005955 if (n < 0)
5956 return FAIL;
5957 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005958 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005959 *fnump = n;
5960 }
5961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005963 if (n < 0)
5964 return FAIL;
5965 posp->lnum = n;
5966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005967 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005968 if (n < 0)
5969 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005970 // If character position is specified, then convert to byte position
5971 if (charcol)
5972 {
5973 buf_T *buf;
5974
5975 // Get the text for the specified line in a loaded buffer
5976 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5977 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5978 return FAIL;
5979
Bram Moolenaar91458462021-01-13 20:08:38 +01005980 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005981 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005982 posp->col = n;
5983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005984 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005985 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005986 posp->coladd = 0;
5987 else
5988 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005989
Bram Moolenaar493c1782014-05-28 14:34:46 +02005990 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005991 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005992
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005993 return OK;
5994}
5995
5996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Get the length of an environment variable name.
5998 * Advance "arg" to the first character after the name.
5999 * Return 0 for error.
6000 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 char_u *p;
6005 int len;
6006
6007 for (p = *arg; vim_isIDc(*p); ++p)
6008 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006009 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return 0;
6011
6012 len = (int)(p - *arg);
6013 *arg = p;
6014 return len;
6015}
6016
6017/*
6018 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006019 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 * Return 0 if something is wrong.
6021 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 char_u *p;
6026 int len;
6027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006028 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006030 {
6031 if (*p == ':')
6032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 // "s:" is start of "s:var", but "n:" is not and can be used in
6034 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006035 len = (int)(p - *arg);
6036 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6037 || len > 1)
6038 break;
6039 }
6040 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006041 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 return 0;
6043
6044 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006045 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047 return len;
6048}
6049
6050/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006051 * Get the length of the name of a variable or function.
6052 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006054 * Return -1 if curly braces expansion failed.
6055 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * If the name contains 'magic' {}'s, expand them and return the
6057 * expanded name in an allocated string via 'alias' - caller must free.
6058 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060get_name_len(
6061 char_u **arg,
6062 char_u **alias,
6063 int evaluate,
6064 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 char_u *p;
6068 char_u *expr_start;
6069 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006071 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6074 && (*arg)[2] == (int)KE_SNR)
6075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006076 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 *arg += 3;
6078 return get_id_len(arg) + 3;
6079 }
6080 len = eval_fname_script(*arg);
6081 if (len > 0)
6082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006083 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 *arg += len;
6085 }
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006088 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006090 p = find_name_end(*arg, &expr_start, &expr_end,
6091 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (expr_start != NULL)
6093 {
6094 char_u *temp_string;
6095
6096 if (!evaluate)
6097 {
6098 len += (int)(p - *arg);
6099 *arg = skipwhite(p);
6100 return len;
6101 }
6102
6103 /*
6104 * Include any <SID> etc in the expanded string:
6105 * Thus the -len here.
6106 */
6107 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6108 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006109 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 *alias = temp_string;
6111 *arg = skipwhite(p);
6112 return (int)STRLEN(temp_string);
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006116 // Only give an error when there is something, otherwise it will be
6117 // reported at a higher level.
6118 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006119 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 return len;
6122}
6123
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006124/*
6125 * Find the end of a variable or function name, taking care of magic braces.
6126 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6127 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006128 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006129 * Return a pointer to just after the name. Equal to "arg" if there is no
6130 * valid name.
6131 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006133find_name_end(
6134 char_u *arg,
6135 char_u **expr_start,
6136 char_u **expr_end,
6137 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006139 int mb_nest = 0;
6140 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006142 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006143 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006145 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 *expr_start = NULL;
6148 *expr_end = NULL;
6149 }
6150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006151 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006152 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6153 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006154 return arg;
6155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006156 for (p = arg; *p != NUL
6157 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006158 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006159 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006160 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006161 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006162 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006164 if (*p == '\'')
6165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006166 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006167 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006168 ;
6169 if (*p == NUL)
6170 break;
6171 }
6172 else if (*p == '"')
6173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006174 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006175 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006176 if (*p == '\\' && p[1] != NUL)
6177 ++p;
6178 if (*p == NUL)
6179 break;
6180 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006181 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 // "s:" is start of "s:var", but "n:" is not and can be used in
6184 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006185 len = (int)(p - arg);
6186 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006187 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006188 break;
6189 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006190
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006191 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 if (*p == '[')
6194 ++br_nest;
6195 else if (*p == ']')
6196 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006198
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006199 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006201 if (*p == '{')
6202 {
6203 mb_nest++;
6204 if (expr_start != NULL && *expr_start == NULL)
6205 *expr_start = p;
6206 }
6207 else if (*p == '}')
6208 {
6209 mb_nest--;
6210 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6211 *expr_end = p;
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215
6216 return p;
6217}
6218
6219/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006220 * Expands out the 'magic' {}'s in a variable/function name.
6221 * Note that this can call itself recursively, to deal with
6222 * constructs like foo{bar}{baz}{bam}
6223 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6224 * "in_start" ^
6225 * "expr_start" ^
6226 * "expr_end" ^
6227 * "in_end" ^
6228 *
6229 * Returns a new allocated string, which the caller must free.
6230 * Returns NULL for failure.
6231 */
6232 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006233make_expanded_name(
6234 char_u *in_start,
6235 char_u *expr_start,
6236 char_u *expr_end,
6237 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006238{
6239 char_u c1;
6240 char_u *retval = NULL;
6241 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006242
6243 if (expr_end == NULL || in_end == NULL)
6244 return NULL;
6245 *expr_start = NUL;
6246 *expr_end = NUL;
6247 c1 = *in_end;
6248 *in_end = NUL;
6249
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006250 temp_result = eval_to_string(expr_start + 1, FALSE);
6251 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006252 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006253 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6254 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006255 if (retval != NULL)
6256 {
6257 STRCPY(retval, in_start);
6258 STRCAT(retval, temp_result);
6259 STRCAT(retval, expr_end + 1);
6260 }
6261 }
6262 vim_free(temp_result);
6263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006264 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006265 *expr_start = '{';
6266 *expr_end = '}';
6267
6268 if (retval != NULL)
6269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006270 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006271 if (expr_start != NULL)
6272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006273 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274 temp_result = make_expanded_name(retval, expr_start,
6275 expr_end, temp_result);
6276 vim_free(retval);
6277 retval = temp_result;
6278 }
6279 }
6280
6281 return retval;
6282}
6283
6284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006286 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006291 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006292}
6293
6294/*
6295 * Return TRUE if character "c" can be used as the first character in a
6296 * variable or function name (excluding '{' and '}').
6297 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006300{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006301 return ASCII_ISALPHA(c) || c == '_';
6302}
6303
6304/*
6305 * Return TRUE if character "c" can be used as the first character of a
6306 * dictionary key.
6307 */
6308 int
6309eval_isdictc(int c)
6310{
6311 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312}
6313
6314/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006315 * Handle:
6316 * - expr[expr], expr[expr:expr] subscript
6317 * - ".name" lookup
6318 * - function call with Funcref variable: func(expr)
6319 * - method call: var->method()
6320 *
6321 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006322 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006323 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325handle_subscript(
6326 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006327 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006328 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006329 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006330 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006331{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006332 int evaluate = evalarg != NULL
6333 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006334 int ret = OK;
6335 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006336 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006337 int getnext;
6338 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006339
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006340 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006341 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006342 // When at the end of the line and ".name" or "->{" or "->X" follows in
6343 // the next line then consume the line break.
6344 p = eval_next_non_blank(*arg, evalarg, &getnext);
6345 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006346 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006347 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6348 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6349 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006350 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006351 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006352 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006353 check_white = FALSE;
6354 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006355
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006356 if (rettv->v_type == VAR_ANY)
6357 {
6358 char_u *exp_name;
6359 int cc;
6360 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006361 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006362 type_T *type;
6363
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006364 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006365 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006366 if (**arg != '.')
6367 {
6368 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006369 semsg(_(e_expected_dot_after_name_str),
6370 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006371 ret = FAIL;
6372 break;
6373 }
6374 ++*arg;
6375 if (IS_WHITE_OR_NUL(**arg))
6376 {
6377 if (verbose)
6378 emsg(_(e_no_white_space_allowed_after_dot));
6379 ret = FAIL;
6380 break;
6381 }
6382
6383 // isolate the name
6384 exp_name = *arg;
6385 while (eval_isnamec(**arg))
6386 ++*arg;
6387 cc = **arg;
6388 **arg = NUL;
6389
6390 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006391 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006392 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006393
6394 if (idx < 0 && ufunc == NULL)
6395 {
6396 ret = FAIL;
6397 break;
6398 }
6399 if (idx >= 0)
6400 {
6401 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6402 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6403
6404 copy_tv(sv->sv_tv, rettv);
6405 }
6406 else
6407 {
6408 rettv->v_type = VAR_FUNC;
6409 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6410 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006411 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006412 }
6413
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006414 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6415 || rettv->v_type == VAR_PARTIAL))
6416 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006417 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006418 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6419 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006420
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006421 // Stop the expression evaluation when immediately aborting on
6422 // error, or when an interrupt occurred or an exception was thrown
6423 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006424 if (aborting())
6425 {
6426 if (ret == OK)
6427 clear_tv(rettv);
6428 ret = FAIL;
6429 }
6430 dict_unref(selfdict);
6431 selfdict = NULL;
6432 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006433 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006434 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006435 if (in_vim9script())
6436 *arg = skipwhite(p + 2);
6437 else
6438 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006439 if (ret == OK)
6440 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006441 if (VIM_ISWHITE(**arg))
6442 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006443 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006444 ret = FAIL;
6445 }
6446 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006447 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006448 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006449 else
6450 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006451 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006452 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006453 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006454 // "." is ".name" lookup when we found a dict or when evaluating and
6455 // scriptversion is at least 2, where string concatenation is "..".
6456 else if (**arg == '['
6457 || (**arg == '.' && (rettv->v_type == VAR_DICT
6458 || (!evaluate
6459 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006460 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006461 {
6462 dict_unref(selfdict);
6463 if (rettv->v_type == VAR_DICT)
6464 {
6465 selfdict = rettv->vval.v_dict;
6466 if (selfdict != NULL)
6467 ++selfdict->dv_refcount;
6468 }
6469 else
6470 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006471 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006472 {
6473 clear_tv(rettv);
6474 ret = FAIL;
6475 }
6476 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006477 else
6478 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006479 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6482 // Don't do this when "Func" is already a partial that was bound
6483 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006484 if (selfdict != NULL
6485 && (rettv->v_type == VAR_FUNC
6486 || (rettv->v_type == VAR_PARTIAL
6487 && (rettv->vval.v_partial->pt_auto
6488 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006489 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006490
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006491 dict_unref(selfdict);
6492 return ret;
6493}
6494
6495/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 * Make a copy of an item.
6497 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006498 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6500 * reference to an already copied list/dict can be used.
6501 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006504item_copy(
6505 typval_T *from,
6506 typval_T *to,
6507 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006508 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510{
6511 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006516 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 }
6519 ++recurse;
6520
6521 switch (from->v_type)
6522 {
6523 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006524 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 case VAR_STRING:
6526 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006527 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006528 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006529 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006530 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006531 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006532 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533 copy_tv(from, to);
6534 break;
6535 case VAR_LIST:
6536 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006537 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 if (from->vval.v_list == NULL)
6539 to->vval.v_list = NULL;
6540 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006542 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 to->vval.v_list = from->vval.v_list->lv_copylist;
6544 ++to->vval.v_list->lv_refcount;
6545 }
6546 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006547 to->vval.v_list = list_copy(from->vval.v_list,
6548 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 if (to->vval.v_list == NULL)
6550 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006552 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006554 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 case VAR_DICT:
6556 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006557 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 if (from->vval.v_dict == NULL)
6559 to->vval.v_dict = NULL;
6560 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006562 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6564 ++to->vval.v_dict->dv_refcount;
6565 }
6566 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006567 to->vval.v_dict = dict_copy(from->vval.v_dict,
6568 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006569 if (to->vval.v_dict == NULL)
6570 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006572 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006573 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006574 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006575 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006576 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 }
6578 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580}
6581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006582 void
6583echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6584{
6585 char_u *tofree;
6586 char_u numbuf[NUMBUFLEN];
6587 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6588
6589 if (*atstart)
6590 {
6591 *atstart = FALSE;
6592 // Call msg_start() after eval1(), evaluating the expression
6593 // may cause a message to appear.
6594 if (with_space)
6595 {
6596 // Mark the saved text as finishing the line, so that what
6597 // follows is displayed on a new line when scrolling back
6598 // at the more prompt.
6599 msg_sb_eol();
6600 msg_start();
6601 }
6602 }
6603 else if (with_space)
6604 msg_puts_attr(" ", echo_attr);
6605
6606 if (p != NULL)
6607 for ( ; *p != NUL && !got_int; ++p)
6608 {
6609 if (*p == '\n' || *p == '\r' || *p == TAB)
6610 {
6611 if (*p != TAB && *needclr)
6612 {
6613 // remove any text still there from the command
6614 msg_clr_eos();
6615 *needclr = FALSE;
6616 }
6617 msg_putchar_attr(*p, echo_attr);
6618 }
6619 else
6620 {
6621 if (has_mbyte)
6622 {
6623 int i = (*mb_ptr2len)(p);
6624
6625 (void)msg_outtrans_len_attr(p, i, echo_attr);
6626 p += i - 1;
6627 }
6628 else
6629 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6630 }
6631 }
6632 vim_free(tofree);
6633}
6634
Bram Moolenaare9a41262005-01-15 22:18:47 +00006635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 * ":echo expr1 ..." print each argument separated with a space, add a
6637 * newline at the end.
6638 * ":echon expr1 ..." print each argument plain.
6639 */
6640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006641ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006645 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 int needclr = TRUE;
6647 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006648 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006649 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006650 evalarg_T evalarg;
6651
Bram Moolenaare707c882020-07-01 13:07:37 +02006652 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 if (eap->skip)
6655 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006656 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006658 // If eval1() causes an error message the text from the command may
6659 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006660 need_clr_eos = needclr;
6661
Bram Moolenaar68db9962021-05-09 23:19:22 +02006662 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006663 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
6665 /*
6666 * Report the invalid expression unless the expression evaluation
6667 * has been cancelled due to an aborting error, an interrupt, or an
6668 * exception.
6669 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006670 if (!aborting() && did_emsg == did_emsg_before
6671 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006672 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006673 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 break;
6675 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006676 need_clr_eos = FALSE;
6677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006679 {
6680 if (rettv.v_type == VAR_VOID)
6681 {
6682 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6683 break;
6684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006686 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006688 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 arg = skipwhite(arg);
6690 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006691 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006692 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694 if (eap->skip)
6695 --emsg_skip;
6696 else
6697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006698 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (needclr)
6700 msg_clr_eos();
6701 if (eap->cmdidx == CMD_echo)
6702 msg_end();
6703 }
6704}
6705
6706/*
6707 * ":echohl {name}".
6708 */
6709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006712 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713}
6714
6715/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006716 * Returns the :echo attribute
6717 */
6718 int
6719get_echo_attr(void)
6720{
6721 return echo_attr;
6722}
6723
6724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 * ":execute expr1 ..." execute the result of an expression.
6726 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006727 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006729 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * Each gets spaces around each argument and a newline at the end for
6731 * echo commands
6732 */
6733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006737 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 int ret = OK;
6739 char_u *p;
6740 garray_T ga;
6741 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006742 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 ga_init2(&ga, 1, 80);
6745
6746 if (eap->skip)
6747 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006748 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006750 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006751 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754 if (!eap->skip)
6755 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006756 char_u buf[NUMBUFLEN];
6757
6758 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006759 {
6760 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6761 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006762 semsg(_(e_using_invalid_value_as_string_str),
6763 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006764 p = NULL;
6765 }
6766 else
6767 p = tv_get_string_buf(&rettv, buf);
6768 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006769 else
6770 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006771 if (p == NULL)
6772 {
6773 clear_tv(&rettv);
6774 ret = FAIL;
6775 break;
6776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 len = (int)STRLEN(p);
6778 if (ga_grow(&ga, len + 2) == FAIL)
6779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 ret = FAIL;
6782 break;
6783 }
6784 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 ga.ga_len += len;
6788 }
6789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 arg = skipwhite(arg);
6792 }
6793
6794 if (ret != FAIL && ga.ga_data != NULL)
6795 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006796 // use the first line of continuation lines for messages
6797 SOURCING_LNUM = start_lnum;
6798
Bram Moolenaar37fef162022-08-29 18:16:32 +01006799 if (eap->cmdidx == CMD_echomsg
6800 || eap->cmdidx == CMD_echowindow
6801 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006802 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006803 // Mark the already saved text as finishing the line, so that what
6804 // follows is displayed on a new line when scrolling back at the
6805 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006806 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006807 }
6808
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006809 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006810 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006811 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006812 out_flush();
6813 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006814 else if (eap->cmdidx == CMD_echowindow)
6815 {
6816#ifdef HAS_MESSAGE_WINDOW
6817 start_echowindow();
6818#endif
6819 msg_attr(ga.ga_data, echo_attr);
6820#ifdef HAS_MESSAGE_WINDOW
6821 end_echowindow();
6822#endif
6823 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006824 else if (eap->cmdidx == CMD_echoconsole)
6825 {
6826 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6827 ui_write((char_u *)"\r\n", 2, TRUE);
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 else if (eap->cmdidx == CMD_echoerr)
6830 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006831 int save_did_emsg = did_emsg;
6832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006833 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006834 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (!force_abort)
6836 did_emsg = save_did_emsg;
6837 }
6838 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006839 {
6840 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6841
6842 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6843 sticky_cmdmod_flags = cmdmod.cmod_flags
6844 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 do_cmdline((char_u *)ga.ga_data,
6846 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006847 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850
6851 ga_clear(&ga);
6852
6853 if (eap->skip)
6854 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006855 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
6858/*
6859 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6860 * "arg" points to the "&" or '+' when called, to "option" when returning.
6861 * Returns NULL when no option name found. Otherwise pointer to the char
6862 * after the option name.
6863 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006864 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006865find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 char_u *p = *arg;
6868
6869 ++p;
6870 if (*p == 'g' && p[1] == ':')
6871 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006872 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 p += 2;
6874 }
6875 else if (*p == 'l' && p[1] == ':')
6876 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006877 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 p += 2;
6879 }
6880 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006881 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
6883 if (!ASCII_ISALPHA(*p))
6884 return NULL;
6885 *arg = p;
6886
6887 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006888 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 else
6890 while (ASCII_ISALPHA(*p))
6891 ++p;
6892 return p;
6893}
6894
6895/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006896 * Display script name where an item was last set.
6897 * Should only be invoked when 'verbose' is non-zero.
6898 */
6899 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006900last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006901{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006902 char_u *p;
6903
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006904 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006905 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006906 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006907 if (p != NULL)
6908 {
6909 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006910 msg_puts(_("\n\tLast set from "));
6911 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006912 if (script_ctx.sc_lnum > 0)
6913 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006914 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006915 msg_outnum((long)script_ctx.sc_lnum);
6916 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006917 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006918 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006919 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006920 }
6921}
6922
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006923#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925/*
6926 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006927 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 * "flags" can be "g" to do a global substitute.
6929 * Returns an allocated string, NULL for error.
6930 */
6931 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006932do_string_sub(
6933 char_u *str,
6934 char_u *pat,
6935 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006936 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006937 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int sublen;
6940 regmatch_T regmatch;
6941 int i;
6942 int do_all;
6943 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006944 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 garray_T ga;
6946 char_u *ret;
6947 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006948 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006950 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006952 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 ga_init2(&ga, 1, 200);
6955
6956 do_all = (flags[0] == 'g');
6957
6958 regmatch.rm_ic = p_ic;
6959 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6960 if (regmatch.regprog != NULL)
6961 {
6962 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006963 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006966 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006967 if (regmatch.startp[0] == regmatch.endp[0])
6968 {
6969 if (zero_width == regmatch.startp[0])
6970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006971 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006972 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006973 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6974 (size_t)i);
6975 ga.ga_len += i;
6976 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006977 continue;
6978 }
6979 zero_width = regmatch.startp[0];
6980 }
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 /*
6983 * Get some space for a temporary buffer to do the substitution
6984 * into. It will contain:
6985 * - The text up to where the match is.
6986 * - The substituted text.
6987 * - The text after the match.
6988 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006989 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006990 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6992 {
6993 ga_clear(&ga);
6994 break;
6995 }
6996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006997 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 i = (int)(regmatch.startp[0] - tail);
6999 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007000 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007001 (void)vim_regsub(&regmatch, sub, expr,
7002 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7003 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007005 tail = regmatch.endp[0];
7006 if (*tail == NUL)
7007 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (!do_all)
7009 break;
7010 }
7011
7012 if (ga.ga_data != NULL)
7013 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7014
Bram Moolenaar473de612013-06-08 18:19:48 +02007015 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017
7018 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7019 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007020 if (p_cpo == empty_option)
7021 p_cpo = save_cpo;
7022 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007024 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007025 // If it's still empty it was changed and restored, need to restore in
7026 // the complicated way.
7027 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007028 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007029 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
7032 return ret;
7033}