blob: 4143dd2ac681183e120a34177a6e0fbb022b9c39 [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 Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static 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 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000131
132 if (eap == NULL)
133 return;
134
135 evalarg->eval_cstack = eap->cstack;
136 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200137 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000138 evalarg->eval_getline = eap->getline;
139 evalarg->eval_cookie = eap->cookie;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200140 }
141}
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Top level evaluation function, returning a boolean.
145 * Sets "error" to TRUE if there was an error.
146 * Return TRUE or FALSE.
147 */
148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_to_bool(
150 char_u *arg,
151 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200152 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100153 int skip, // only parse, don't execute
154 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
Bram Moolenaar33570922005-01-25 22:26:29 +0000156 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200157 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200158 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100159 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200160
Bram Moolenaar37c83712020-06-30 21:18:36 +0200161 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163 if (skip)
164 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100165 if (use_simple_function)
166 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
167 else
168 r = eval0(arg, &tv, eap, &evalarg);
169 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 {
173 *error = FALSE;
174 if (!skip)
175 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200176 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200177 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200178 else
179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200185 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200187 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190/*
191 * Call eval1() and give an error message if not done at a lower level.
192 */
193 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200194eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100195{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100196 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197 int ret;
198 int did_emsg_before = did_emsg;
199 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
203
204 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200213 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 return ret;
217}
218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200220 * Return whether a typval is a valid expression to pass to eval_expr_typval()
221 * or eval_expr_to_bool(). An empty string returns FALSE;
222 */
223 int
224eval_expr_valid_arg(typval_T *tv)
225{
226 return tv->v_type != VAR_UNKNOWN
227 && (tv->v_type != VAR_STRING
228 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
229}
230
231/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100232 * When calling eval_expr_typval() many times we only need one funccall_T.
233 * Returns NULL when no funccall_T is to be used.
234 * When returning non-NULL remove_funccal() must be called later.
235 */
236 funccall_T *
237eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
238{
239 if (expr->v_type != VAR_PARTIAL)
240 return NULL;
241
242 partial_T *partial = expr->vval.v_partial;
243 if (partial == NULL)
244 return NULL;
245 if (partial->pt_func == NULL
246 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
247 return NULL;
248
249 return create_funccal(partial->pt_func, rettv);
250}
251
252/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 * Evaluate an expression, which can be a function, partial or string.
254 * Pass arguments "argv[argc]".
zeertzjqad0c4422023-08-17 22:15:47 +0200255 * If "want_func" is TRUE treat a string as a function name, not an expression.
Bram Moolenaar82418262022-09-28 16:16:15 +0100256 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 * Return the result in "rettv" and OK or FAIL.
258 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200259 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100260eval_expr_typval(
261 typval_T *expr,
zeertzjqad0c4422023-08-17 22:15:47 +0200262 int want_func,
Bram Moolenaar82418262022-09-28 16:16:15 +0100263 typval_T *argv,
264 int argc,
265 funccall_T *fc_arg,
266 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267{
268 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100271
zeertzjqad0c4422023-08-17 22:15:47 +0200272 if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100282 funccall_T *fc = fc_arg != NULL ? fc_arg
283 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100284 int r;
285
286 if (fc == NULL)
287 return FAIL;
288
Bram Moolenaar69082912022-09-22 21:35:19 +0100289 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100290 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100292 if (fc_arg == NULL)
293 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100294 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000303 funcexe.fe_evaluate = TRUE;
304 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200309 else if (expr->v_type == VAR_INSTR)
310 {
311 return exe_typval_instr(expr, rettv);
312 }
zeertzjqad0c4422023-08-17 22:15:47 +0200313 else if (expr->v_type == VAR_FUNC || want_func)
314 {
315 s = expr->v_type == VAR_FUNC
316 ? expr->vval.v_string
317 : tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
318 if (s == NULL || *s == NUL)
319 return FAIL;
320 CLEAR_FIELD(funcexe);
321 funcexe.fe_evaluate = TRUE;
322 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
323 return FAIL;
324 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 else
326 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000327 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 if (s == NULL)
329 return FAIL;
330 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200331 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200333 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100334 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200335 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200336 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 return FAIL;
338 }
339 }
340 return OK;
341}
342
343/*
344 * Like eval_to_bool() but using a typval_T instead of a string.
345 * Works for string, funcref and partial.
346 */
347 int
348eval_expr_to_bool(typval_T *expr, int *error)
349{
350 typval_T rettv;
351 int res;
352
zeertzjqad0c4422023-08-17 22:15:47 +0200353 if (eval_expr_typval(expr, FALSE, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 {
355 *error = TRUE;
356 return FALSE;
357 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200358 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100359 clear_tv(&rettv);
360 return res;
361}
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Top level evaluation function, returning a string. If "skip" is TRUE,
365 * only parsing to "nextcmd" is done, without reporting errors. Return
366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
367 */
368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100369eval_to_string_skip(
370 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200371 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100372 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar33570922005-01-25 22:26:29 +0000374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar37c83712020-06-30 21:18:36 +0200378 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (skip)
380 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 retval = NULL;
383 else
384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388 if (skip)
389 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200390 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 return retval;
393}
394
395/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100396 * Initialize "evalarg" for use.
397 */
398 void
399init_evalarg(evalarg_T *evalarg)
400{
401 CLEAR_POINTER(evalarg);
402 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
403}
404
405/*
406 * If "evalarg->eval_tofree" is not NULL free it later.
407 * Caller is expected to overwrite "evalarg->eval_tofree" next.
408 */
409 static void
410free_eval_tofree_later(evalarg_T *evalarg)
411{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000412 if (evalarg->eval_tofree == NULL)
413 return;
414
415 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
416 ((char_u **)evalarg->eval_tofree_ga.ga_data)
417 [evalarg->eval_tofree_ga.ga_len++]
418 = evalarg->eval_tofree;
419 else
420 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100421}
422
423/*
424 * After using "evalarg" filled from "eap": free the memory.
425 */
426 void
427clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
428{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000429 if (evalarg == NULL)
430 return;
431
432 garray_T *etga = &evalarg->eval_tofree_ga;
433
434 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000436 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100437 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000438 // We may need to keep the original command line, e.g. for
439 // ":let" it has the variable names. But we may also need
440 // the new one, "nextcmd" points into it. Keep both.
441 vim_free(eap->cmdline_tofree);
442 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000444 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
445 {
446 // "nextcmd" points into the last line in eval_tofree_ga,
447 // need to keep it around.
448 --etga->ga_len;
449 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
450 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100451 }
452 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000453 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455 else
456 vim_free(evalarg->eval_tofree);
457 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000459
460 ga_clear_strings(etga);
461 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100462}
463
464/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465 * Skip over an expression at "*pp".
466 * Return FAIL for an error, OK otherwise.
467 */
468 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470{
Bram Moolenaar33570922005-01-25 22:26:29 +0000471 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000472
473 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200474 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000475}
476
477/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200478 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * If in Vim9 script and line breaks are encountered, the lines are
480 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200481 * "arg" is advanced to just after the expression.
482 * "start" is set to the start of the expression, "end" to just after the end.
483 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 * Return FAIL for an error, OK otherwise.
485 */
486 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487skip_expr_concatenate(
488 char_u **arg,
489 char_u **start,
490 char_u **end,
491 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200492{
493 typval_T rettv;
494 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200495 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200496 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200497 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200498 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200499 int evaluate = evalarg == NULL
500 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200502 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 {
505 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200509 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200510 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512
513 // Don't evaluate the expression.
514 if (evalarg != NULL)
515 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200516 *arg = skipwhite(*arg);
517 res = eval1(arg, &rettv, evalarg);
518 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 if (evalarg != NULL)
520 evalarg->eval_flags = save_flags;
521
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200522 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 if (evalarg->eval_ga.ga_len == 1)
526 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200527 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528 ga_clear(gap);
529 gap->ga_itemsize = 0;
530 }
531 else
532 {
533 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200535
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200536 // Line breaks encountered, concatenate all the lines.
537 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200538 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539
540 // free the lines only when using getsourceline()
541 if (evalarg->eval_cookie != NULL)
542 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200545 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100546 // later. Also free "eval_tofree" later if needed.
547 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 evalarg->eval_tofree =
549 ((char_u **)gap->ga_data)[gap->ga_len - 1];
550 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear_strings(gap);
552 }
553 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200555 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200556
557 // free lines that were explicitly marked for freeing
558 ga_clear_strings(freegap);
559 }
560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200561 gap->ga_itemsize = 0;
562 if (p == NULL)
563 return FAIL;
564 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200565 vim_free(evalarg->eval_tofree_lambda);
566 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200567 // Compute "end" relative to the end.
568 *end = *start + STRLEN(*start) - endoff;
569 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200570 }
571
572 return res;
573}
574
575/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100577 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 * Returns an allocated string (NULL when out of memory).
579 */
580 char_u *
581typval2string(typval_T *tv, int convert)
582{
583 garray_T ga;
584 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585
586 if (convert && tv->v_type == VAR_LIST)
587 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000588 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 if (tv->vval.v_list != NULL)
590 {
591 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
592 if (tv->vval.v_list->lv_len > 0)
593 ga_append(&ga, NL);
594 }
595 ga_append(&ga, NUL);
596 retval = (char_u *)ga.ga_data;
597 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100598 else
599 retval = vim_strsave(tv_get_string(tv));
600 return retval;
601}
602
603/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200604 * Top level evaluation function, returning a string. Does not handle line
605 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100606 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Return pointer to allocated memory, or NULL for failure.
608 */
609 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100611 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100612 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100613 exarg_T *eap,
614 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar33570922005-01-25 22:26:29 +0000616 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 if (use_simple_function)
623 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
624 else
625 r = eval0(arg, &tv, NULL, &evalarg);
626 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 retval = NULL;
628 else
629 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100630 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100633 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 return retval;
636}
637
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100638 char_u *
639eval_to_string(
640 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 int convert,
642 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100643{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100645}
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000648 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100649 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200650 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653eval_to_string_safe(
654 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000655 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100656 int keep_script_version,
657 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200660 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200661 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200662 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9530b582022-01-22 13:39:08 +0000664 if (!keep_script_version)
665 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200666 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000667 if (use_sandbox)
668 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100669 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200670 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100671 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 if (use_sandbox)
673 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100674 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200675 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200676 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200677 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return retval;
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Top level evaluation function, returning a number.
683 * Evaluates "expr" silently.
684 * Returns -1 for an error.
685 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100687eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar33570922005-01-25 22:26:29 +0000689 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000691 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 ++emsg_off;
695
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 if (use_simple_function)
697 r = may_call_simple_func(expr, &rettv);
698 if (r == NOTDONE)
699 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
700 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 retval = -1;
702 else
703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100704 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 --emsg_off;
708
709 return retval;
710}
711
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000713 * Top level evaluation function.
714 * Returns an allocated typval_T with the result.
715 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716 */
717 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200718eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100720 return eval_expr_ext(arg, eap, FALSE);
721}
722
723 typval_T *
724eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
725{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200727 evalarg_T evalarg;
728
729 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200731 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100732 if (tv != NULL)
733 {
734 int r = NOTDONE;
735
736 if (use_simple_function)
737 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
738 if (r == NOTDONE)
739 r = eval0(arg, tv, eap, &evalarg);
740
741 if (r == FAIL)
742 VIM_CLEAR(tv);
743 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000744
Bram Moolenaar37c83712020-06-30 21:18:36 +0200745 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746 return tv;
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 * "*arg" points to what can be a function name in the form of "import.Name" or
751 * "Funcref". Return the name of the function. Set "tofree" to something that
752 * was allocated.
753 * If "verbose" is FALSE no errors are given.
754 * Return NULL for any failure.
755 */
756 static char_u *
757deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000758 char_u **arg,
759 char_u **tofree,
760 evalarg_T *evalarg,
761 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762{
763 typval_T ref;
764 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100765 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766
767 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100768 if (evalarg != NULL)
769 {
770 // need to evaluate this to get an import, like in "a.Func"
771 save_flags = evalarg->eval_flags;
772 evalarg->eval_flags |= EVAL_EVALUATE;
773 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100774 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100775 {
776 dictitem_T *v;
777
778 // If <SID>VarName was used it would not be found, try another way.
779 v = find_var_also_in_script(name, NULL, FALSE);
780 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100781 {
782 name = NULL;
783 goto theend;
784 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100785 copy_tv(&v->di_tv, &ref);
786 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 if (*skipwhite(*arg) != NUL)
788 {
789 if (verbose)
790 semsg(_(e_trailing_characters_str), *arg);
791 name = NULL;
792 }
793 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
794 {
795 name = ref.vval.v_string;
796 ref.vval.v_string = NULL;
797 *tofree = name;
798 }
799 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
800 {
801 if (ref.vval.v_partial->pt_argc > 0
802 || ref.vval.v_partial->pt_dict != NULL)
803 {
804 if (verbose)
805 emsg(_(e_cannot_use_partial_here));
806 name = NULL;
807 }
808 else
809 {
810 name = vim_strsave(partial_name(ref.vval.v_partial));
811 *tofree = name;
812 }
813 }
814 else
815 {
816 if (verbose)
817 semsg(_(e_not_callable_type_str), name);
818 name = NULL;
819 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820
821theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823 if (evalarg != NULL)
824 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 return name;
826}
827
828/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100829 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200830 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
831 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100835call_vim_function(
836 char_u *func,
837 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200838 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000841 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200842 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000843 char_u *arg;
844 char_u *name;
845 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000846 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200849 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000850 funcexe.fe_firstline = curwin->w_cursor.lnum;
851 funcexe.fe_lastline = curwin->w_cursor.lnum;
852 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000853
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000854 // The name might be "import.Func" or "Funcref". We don't know, we need to
855 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000856 // autoload script has errors. Guess that when there is a dot in the name
857 // showing errors is the right choice.
858 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 if (name == NULL)
866 name = func;
867
868 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870 if (ret == FAIL)
871 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000872 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873
874 return ret;
875}
876
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100877/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100878 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000879 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
880 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000881 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000882 */
883 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884call_func_retstr(
885 char_u *func,
886 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200887 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888{
889 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000890 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891
Bram Moolenaarded27a12018-08-01 19:06:03 +0200892 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 return NULL;
894
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100895 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 return retval;
898}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000900/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100901 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000902 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000903 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100904 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000905 */
906 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907call_func_retlist(
908 char_u *func,
909 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911{
912 typval_T rettv;
913
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915 return NULL;
916
917 if (rettv.v_type != VAR_LIST)
918 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100919 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
920 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000921 clear_tv(&rettv);
922 return NULL;
923 }
924
925 return rettv.vval.v_list;
926}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaare70dd112022-01-21 16:31:11 +0000928#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 * Evaluate "arg", which is 'foldexpr'.
931 * Note: caller must set "curwin" to match "arg".
932 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
933 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
935 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000936eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200940 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000943 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100944 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100946 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000947 current_sctx = wp->w_p_script_ctx[WV_FDE];
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000950 if (use_sandbox)
951 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100952 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100954
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100955 // Evaluate the expression. If the expression is "FuncName()" call the
956 // function directly.
957 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 retval = 0;
959 else
960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100961 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 if (tv.v_type == VAR_NUMBER)
963 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000964 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 retval = 0;
966 else
967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // If the result is a string, check if there is a non-digit before
969 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000970 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (!VIM_ISDIGIT(*s) && *s != '-')
972 *cp = *s++;
973 retval = atol((char *)s);
974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000978 if (use_sandbox)
979 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100980 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200981 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000982 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200984 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986#endif
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 * Get an lval: variable, Dict item or List item that can be assigned a value
990 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
991 * "name.key", "name.key[expr]" etc.
992 * Indexing only works if "name" is an existing List or Dictionary.
993 * "name" points to the start of the name.
994 * If "rettv" is not NULL it points to the value to be assigned.
995 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
996 * wrong; must end in space or cmd separator.
997 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100998 * flags:
999 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001000 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 * GLV_NO_AUTOLOAD: do not use script autoloading
1002 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001004 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001007 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008get_lval(
1009 char_u *name,
1010 typval_T *rettv,
1011 lval_T *lp,
1012 int unlet,
1013 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 int flags, // GLV_ values
1015 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 char_u *expr_start, *expr_end;
1019 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001020 dictitem_T *v;
1021 typval_T var1;
1022 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001026 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001027 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001028 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001029 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001032 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001034 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001036 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001038 lp->ll_name_end = find_name_end(name, NULL, NULL,
1039 FNE_INCL_BR | fne_flags);
1040 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 }
1042
Bram Moolenaara749a422022-02-12 19:52:25 +00001043 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001044 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001045 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1046 {
1047 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1048 return NULL;
1049 }
1050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001051 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 if (expr_start != NULL)
1055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001056 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001057 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 && *p != '[' && *p != '.')
1059 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001060 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
1062 }
1063
1064 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1065 if (lp->ll_exp_name == NULL)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // Report an invalid expression in braces, unless the
1068 // expression evaluation has been cancelled due to an
1069 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 if (!aborting() && !quiet)
1071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001072 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001073 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 return NULL;
1075 }
1076 }
1077 lp->ll_name = lp->ll_exp_name;
1078 }
1079 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001081 lp->ll_name = name;
1082
Bram Moolenaar4525a572022-02-13 11:57:33 +00001083 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001085 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001086 // However, "g:[key]" is indexing a dictionary.
1087 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001088 {
1089 --p;
1090 lp->ll_name_end = p;
1091 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001092 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001093 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001094 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001095
Bram Moolenaar9510d222022-09-11 15:14:05 +01001096 if (is_scoped_variable(name))
1097 {
1098 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1099 return NULL;
1100 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001101 if (VIM_ISWHITE(*p))
1102 {
1103 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1104 return NULL;
1105 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001106 if (tp == p + 1 && !quiet)
1107 {
1108 semsg(_(e_white_space_required_after_str_str), ":", p);
1109 return NULL;
1110 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001111 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001112 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001113 semsg(_(e_using_type_not_in_script_context_str), p);
1114 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001115 }
1116
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001117 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001118 lp->ll_type = parse_type(&tp,
1119 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1120 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001121 if (lp->ll_type == NULL && !quiet)
1122 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001123 lp->ll_name_end = tp;
1124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 }
1126 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001127 if (lp->ll_name == NULL)
1128 return p;
1129
Bram Moolenaar62aec932022-01-29 21:45:34 +00001130 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001131 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001132 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001133
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001134 if (import != NULL)
1135 {
1136 ufunc_T *ufunc;
1137 type_T *type;
1138
Bram Moolenaar753885b2022-08-24 16:30:36 +01001139 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001140 lp->ll_sid = import->imp_sid;
1141 lp->ll_name = skipwhite(p + 1);
1142 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1143 lp->ll_name_end = p;
1144
1145 // check the item is exported
1146 cc = *p;
1147 *p = NUL;
1148 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001149 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001150 {
1151 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001152 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001153 }
1154 *p = cc;
1155 }
1156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001158 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001159 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 return p;
1161
Bram Moolenaar4525a572022-02-13 11:57:33 +00001162 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001163 {
1164 // using local variable
1165 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001166 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001167 }
1168 else
1169 {
1170 cc = *p;
1171 *p = NUL;
1172 // When we would write to the variable pass &ht and prevent autoload.
1173 writing = !(flags & GLV_READ_ONLY);
1174 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001175 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001176 if (v == NULL && !quiet)
1177 semsg(_(e_undefined_variable_str), lp->ll_name);
1178 *p = cc;
1179 if (v == NULL)
1180 return NULL;
1181 lp->ll_tv = &v->di_tv;
1182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183
Bram Moolenaar4525a572022-02-13 11:57:33 +00001184 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001185 {
1186 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001187 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001188 return NULL;
1189 }
1190
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 /*
1192 * Loop until no more [idx] or .key is following.
1193 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 var1.v_type = VAR_UNKNOWN;
1195 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001196 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001198 vartype_T v_type = lp->ll_tv->v_type;
1199
1200 if (*p == '.' && v_type != VAR_DICT
1201 && v_type != VAR_OBJECT
1202 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001203 {
1204 if (!quiet)
1205 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1206 return NULL;
1207 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001208 if (v_type != VAR_LIST
1209 && v_type != VAR_DICT
1210 && v_type != VAR_BLOB
1211 && v_type != VAR_OBJECT
1212 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001215 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001218
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001219 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001220 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001221 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001222 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001223 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001224 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001225 r = rettv_blob_alloc(lp->ll_tv);
1226 if (r == FAIL)
1227 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001228
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001232 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001235
Bram Moolenaar4525a572022-02-13 11:57:33 +00001236 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001237 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001238 && lp->ll_tv == &v->di_tv
1239 && ht != NULL && ht == get_script_local_ht())
1240 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001241 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001242
1243 // Vim9 script local variable: get the type
1244 if (sv != NULL)
1245 lp->ll_valtype = sv->sv_type;
1246 }
1247
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 len = -1;
1249 if (*p == '.')
1250 {
1251 key = p + 1;
1252 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1253 ;
1254 if (len == 0)
1255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001257 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 }
1260 p = key + len;
1261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 else
1263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001264 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001265 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 if (*p == ':')
1267 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 else
1269 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001271 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001273 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001275 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 clear_tv(&var1);
1277 return NULL;
1278 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001279 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001280 }
1281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001282 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001283 if (*p == ':')
1284 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001285 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001286 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001288 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001289 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001291 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 if (rettv != NULL
1293 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001299 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001300 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 }
1303 p = skipwhite(p + 1);
1304 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001306 else
1307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001309 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001310 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001314 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001315 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001317 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001318 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001319 clear_tv(&var2);
1320 return NULL;
1321 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001331 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001332 clear_tv(&var1);
1333 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 }
1336
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001337 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001338 ++p;
1339 }
1340
Bram Moolenaard505d172022-12-18 21:42:55 +00001341 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001342 {
1343 if (len == -1)
1344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001345 // "[key]": get key from "var1"
1346 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001347 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 }
1352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001354
1355 // a NULL dict is equivalent with an empty dict
1356 if (lp->ll_tv->vval.v_dict == NULL)
1357 {
1358 lp->ll_tv->vval.v_dict = dict_alloc();
1359 if (lp->ll_tv->vval.v_dict == NULL)
1360 {
1361 clear_tv(&var1);
1362 return NULL;
1363 }
1364 ++lp->ll_tv->vval.v_dict->dv_refcount;
1365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001367
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001368 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001370 // When assigning to a scope dictionary check that a function and
1371 // variable name is valid (only variable name unless it is l: or
1372 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001373 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001374 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001375 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001376
1377 if (len != -1)
1378 {
1379 prevval = key[len];
1380 key[len] = NUL;
1381 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001382 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001383 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001384 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001385 && (rettv->v_type == VAR_FUNC
1386 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001387 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001388 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001389 if (len != -1)
1390 key[len] = prevval;
1391 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001392 {
1393 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001394 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001395 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001396 }
1397
Bram Moolenaar10c65862020-10-08 21:16:42 +02001398 if (lp->ll_valtype != NULL)
1399 // use the type of the member
1400 lp->ll_valtype = lp->ll_valtype->tt_member;
1401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001402 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001404 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001405 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001406 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001407 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001408 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001409 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001410 return NULL;
1411 }
1412
Bram Moolenaar31b81602019-02-10 22:14:27 +01001413 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001415 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001417 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001418 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001420 }
1421 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001425 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001427 p = NULL;
1428 break;
1429 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001430 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001431 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001432 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1433 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001434 {
1435 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001436 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001437 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001438
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001439 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001441 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001442 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001444 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1445
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001446 /*
1447 * Get the number and item for the only or first index of the List.
1448 */
1449 if (empty1)
1450 lp->ll_n1 = 0;
1451 else
1452 // is number or string
1453 lp->ll_n1 = (long)tv_get_number(&var1);
1454 clear_tv(&var1);
1455
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001456 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001458 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 return NULL;
1460 }
1461 if (lp->ll_range && !lp->ll_empty2)
1462 {
1463 lp->ll_n2 = (long)tv_get_number(&var2);
1464 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001465 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1466 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001467 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 }
1469 lp->ll_blob = lp->ll_tv->vval.v_blob;
1470 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001471 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001473 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001474 {
1475 /*
1476 * Get the number and item for the only or first index of the List.
1477 */
1478 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001480 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001481 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001482 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001483 clear_tv(&var1);
1484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001485 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001487 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1488 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001489 if (lp->ll_li == NULL)
1490 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001491 clear_tv(&var2);
1492 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 }
1494
Bram Moolenaar10c65862020-10-08 21:16:42 +02001495 if (lp->ll_valtype != NULL)
1496 // use the type of the member
1497 lp->ll_valtype = lp->ll_valtype->tt_member;
1498
Bram Moolenaar8c711452005-01-14 21:53:12 +00001499 /*
1500 * May need to find the item or absolute index for the second
1501 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 * When no index given: "lp->ll_empty2" is TRUE.
1503 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001504 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001505 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001507 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001508 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001509 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001510 if (check_range_index_two(lp->ll_list,
1511 &lp->ll_n1, lp->ll_li,
1512 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001514 }
1515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001516 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001517 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001518 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001519 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001520 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001521 && lp->ll_tv->vval.v_object != NULL)
1522 ? lp->ll_tv->vval.v_object->obj_class
1523 : lp->ll_tv->vval.v_class;
1524 // TODO: what if class is NULL?
1525 if (cl != NULL)
1526 {
1527 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001528
1529 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001530 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001531 // First look for a function with this name.
1532 // round 1: class functions (skipped for an object)
1533 // round 2: object methods
1534 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1535 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001536 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001537 int count = round == 1
1538 ? cl->class_class_function_count
1539 : cl->class_obj_method_count;
1540 ufunc_T **funcs = round == 1
1541 ? cl->class_class_functions
1542 : cl->class_obj_methods;
1543 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001544 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001545 ufunc_T *fp = funcs[i];
1546 char_u *ufname = (char_u *)fp->uf_name;
1547 if (STRNCMP(ufname, key, p - key) == 0
1548 && ufname[p - key] == NUL)
1549 {
1550 lp->ll_ufunc = fp;
1551 lp->ll_valtype = fp->uf_func_type;
1552 round = 3;
1553 break;
1554 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001555 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001556 }
1557 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001558
1559 if (lp->ll_valtype == NULL)
1560 {
1561 int count = v_type == VAR_OBJECT
1562 ? cl->class_obj_member_count
1563 : cl->class_class_member_count;
1564 ocmember_T *members = v_type == VAR_OBJECT
1565 ? cl->class_obj_members
1566 : cl->class_class_members;
1567 for (int i = 0; i < count; ++i)
1568 {
1569 ocmember_T *om = members + i;
1570 if (STRNCMP(om->ocm_name, key, p - key) == 0
1571 && om->ocm_name[p - key] == NUL)
1572 {
1573 switch (om->ocm_access)
1574 {
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001575 case VIM_ACCESS_PRIVATE:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001576 semsg(_(e_cannot_access_private_member_str),
1577 om->ocm_name);
1578 return NULL;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001579 case VIM_ACCESS_READ:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001580 if ((flags & GLV_READ_ONLY) == 0)
1581 {
1582 semsg(_(e_member_is_not_writable_str),
1583 om->ocm_name);
1584 return NULL;
1585 }
1586 break;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001587 case VIM_ACCESS_ALL:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001588 break;
1589 }
1590
1591 lp->ll_valtype = om->ocm_type;
1592
1593 if (v_type == VAR_OBJECT)
1594 lp->ll_tv = ((typval_T *)(
1595 lp->ll_tv->vval.v_object + 1)) + i;
1596 else
1597 lp->ll_tv = &cl->class_members_tv[i];
1598 break;
1599 }
1600 }
1601 }
1602
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001603 if (lp->ll_valtype == NULL)
1604 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001605 if (v_type == VAR_OBJECT)
1606 semsg(_(e_object_member_not_found_str), key);
1607 else
1608 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001609 return NULL;
1610 }
1611 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 }
1614
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001615 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001617 return p;
1618}
1619
1620/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001621 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001625{
1626 vim_free(lp->ll_exp_name);
1627 vim_free(lp->ll_newkey);
1628}
1629
1630/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001631 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001632 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1634 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001635 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637set_var_lval(
1638 lval_T *lp,
1639 char_u *endp,
1640 typval_T *rettv,
1641 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001642 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1643 char_u *op,
1644 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001645{
1646 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001648
1649 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001651 cc = *endp;
1652 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001653 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001654 return;
1655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 if (lp->ll_blob != NULL)
1657 {
1658 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001659
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001660 if (op != NULL && *op != '=')
1661 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001662 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 return;
1664 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001665 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001666 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667
1668 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1669 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001670 if (lp->ll_empty2)
1671 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1672
Bram Moolenaar68452172021-04-12 21:21:02 +02001673 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1674 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 }
1677 else
1678 {
1679 val = (int)tv_get_number_chk(rettv, &error);
1680 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001681 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001682 }
1683 }
1684 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001686 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001688 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1689 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001690 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001691 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001692 *endp = cc;
1693 return;
1694 }
1695
Bram Moolenaarff697e62019-02-12 22:28:33 +01001696 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001697 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001698 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001699 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001700 {
1701 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001702 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1703 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001704 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001705 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001706 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001707 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001710 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001711 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001712 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1713 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001714 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001715 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001716 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001717 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001718 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001719 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001720 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001721 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001722 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001723 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001724 else if (lp->ll_range)
1725 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001726 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1727 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001728 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001729 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001730 return;
1731 }
1732
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001733 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1734 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001735 }
1736 else
1737 {
1738 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001739 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001740 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001741 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1742 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001743 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001744 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001745 return;
1746 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001747
1748 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001749 && check_typval_arg_type(lp->ll_valtype, rettv,
1750 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001751 return;
1752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001753 if (lp->ll_newkey != NULL)
1754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 if (op != NULL && *op != '=')
1756 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001757 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 return;
1759 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001760 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1761 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001762 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001764 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001765 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001766 if (di == NULL)
1767 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001768 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1769 {
1770 vim_free(di);
1771 return;
1772 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001773 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001774 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 else if (op != NULL && *op != '=')
1776 {
1777 tv_op(lp->ll_tv, rettv, op);
1778 return;
1779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001781 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001782
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001783 /*
1784 * Assign the value to the variable or list item.
1785 */
1786 if (copy)
1787 copy_tv(rettv, lp->ll_tv);
1788 else
1789 {
1790 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001791 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001792 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 }
1794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795}
1796
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001798 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1799 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 * Returns OK or FAIL.
1801 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001805 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001806 char_u numbuf[NUMBUFLEN];
1807 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001808 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001809
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001810 // Can't do anything with a Funcref or Dict on the right.
1811 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001812 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001813 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1814 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 {
1816 switch (tv1->v_type)
1817 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001818 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001819 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 case VAR_DICT:
1822 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001823 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001824 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001825 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001826 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001827 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001828 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001829 case VAR_CLASS:
1830 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 break;
1832
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001833 case VAR_BLOB:
1834 if (*op != '+' || tv2->v_type != VAR_BLOB)
1835 break;
1836 // BLOB += BLOB
1837 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1838 {
1839 blob_T *b1 = tv1->vval.v_blob;
1840 blob_T *b2 = tv2->vval.v_blob;
1841 int i, len = blob_len(b2);
1842 for (i = 0; i < len; i++)
1843 ga_append(&b1->bv_ga, blob_get(b2, i));
1844 }
1845 return OK;
1846
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 case VAR_LIST:
1848 if (*op != '+' || tv2->v_type != VAR_LIST)
1849 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001850 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001851 if (tv2->vval.v_list != NULL)
1852 {
1853 if (tv1->vval.v_list == NULL)
1854 {
1855 tv1->vval.v_list = tv2->vval.v_list;
1856 ++tv1->vval.v_list->lv_refcount;
1857 }
1858 else
1859 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001861 return OK;
1862
1863 case VAR_NUMBER:
1864 case VAR_STRING:
1865 if (tv2->v_type == VAR_LIST)
1866 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001867 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001869 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001870 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001871 if (tv2->v_type == VAR_FLOAT)
1872 {
1873 float_T f = n;
1874
Bram Moolenaarff697e62019-02-12 22:28:33 +01001875 if (*op == '%')
1876 break;
1877 switch (*op)
1878 {
1879 case '+': f += tv2->vval.v_float; break;
1880 case '-': f -= tv2->vval.v_float; break;
1881 case '*': f *= tv2->vval.v_float; break;
1882 case '/': f /= tv2->vval.v_float; break;
1883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001884 clear_tv(tv1);
1885 tv1->v_type = VAR_FLOAT;
1886 tv1->vval.v_float = f;
1887 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001889 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001890 switch (*op)
1891 {
1892 case '+': n += tv_get_number(tv2); break;
1893 case '-': n -= tv_get_number(tv2); break;
1894 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001895 case '/': n = num_divide(n, tv_get_number(tv2),
1896 &failed); break;
1897 case '%': n = num_modulus(n, tv_get_number(tv2),
1898 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001900 clear_tv(tv1);
1901 tv1->v_type = VAR_NUMBER;
1902 tv1->vval.v_number = n;
1903 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 }
1905 else
1906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001907 if (tv2->v_type == VAR_FLOAT)
1908 break;
1909
Bram Moolenaarff697e62019-02-12 22:28:33 +01001910 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001911 s = tv_get_string(tv1);
1912 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 clear_tv(tv1);
1914 tv1->v_type = VAR_STRING;
1915 tv1->vval.v_string = s;
1916 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001917 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001919 case VAR_FLOAT:
1920 {
1921 float_T f;
1922
Bram Moolenaarff697e62019-02-12 22:28:33 +01001923 if (*op == '%' || *op == '.'
1924 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001925 && tv2->v_type != VAR_NUMBER
1926 && tv2->v_type != VAR_STRING))
1927 break;
1928 if (tv2->v_type == VAR_FLOAT)
1929 f = tv2->vval.v_float;
1930 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001931 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001932 switch (*op)
1933 {
1934 case '+': tv1->vval.v_float += f; break;
1935 case '-': tv1->vval.v_float -= f; break;
1936 case '*': tv1->vval.v_float *= f; break;
1937 case '/': tv1->vval.v_float /= f; break;
1938 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001939 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001940 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 }
1942 }
1943
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001944 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 return FAIL;
1946}
1947
1948/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 * Evaluate the expression used in a ":for var in expr" command.
1950 * "arg" points to "var".
1951 * Set "*errp" to TRUE for an error, FALSE otherwise;
1952 * Return a pointer that holds the info. Null when there is an error.
1953 */
1954 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001955eval_for_line(
1956 char_u *arg,
1957 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001958 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001959 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960{
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001962 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 typval_T tv;
1965 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001966 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001968 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001970 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 if (fi == NULL)
1972 return NULL;
1973
Bram Moolenaar404557e2021-07-05 21:41:48 +02001974 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1975 &fi->fi_semicolon, FALSE);
1976 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 return fi;
1978
Bram Moolenaar404557e2021-07-05 21:41:48 +02001979 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001980 if (expr[0] != 'i' || expr[1] != 'n'
1981 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001983 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1984 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1985 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001986 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 return fi;
1988 }
1989
1990 if (skip)
1991 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001992 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1993 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 {
1995 *errp = FALSE;
1996 if (!skip)
1997 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001998 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001999 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002000 l = tv.vval.v_list;
2001 if (l == NULL)
2002 {
2003 // a null list is like an empty list: do nothing
2004 clear_tv(&tv);
2005 }
2006 else
2007 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002009 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002011 // No need to increment the refcount, it's already set for
2012 // the list being used in "tv".
2013 fi->fi_list = l;
2014 list_add_watch(l, &fi->fi_lw);
2015 fi->fi_lw.lw_item = l->lv_first;
2016 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002017 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002018 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002019 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002020 fi->fi_bi = 0;
2021 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002022 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002023 typval_T btv;
2024
2025 // Make a copy, so that the iteration still works when the
2026 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002028 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002029 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002030 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002031 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002032 else if (tv.v_type == VAR_STRING)
2033 {
2034 fi->fi_byte_idx = 0;
2035 fi->fi_string = tv.vval.v_string;
2036 tv.vval.v_string = NULL;
2037 if (fi->fi_string == NULL)
2038 fi->fi_string = vim_strsave((char_u *)"");
2039 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 else
2041 {
Sean Dewar80d73952021-08-04 19:25:54 +02002042 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002043 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 }
2045 }
2046 }
2047 if (skip)
2048 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002049 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050
2051 return fi;
2052}
2053
2054/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002055 * Used when looping over a :for line, skip the "in expr" part.
2056 */
2057 void
2058skip_for_lines(void *fi_void, evalarg_T *evalarg)
2059{
2060 forinfo_T *fi = (forinfo_T *)fi_void;
2061 int i;
2062
2063 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002064 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002065}
2066
2067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068 * Use the first item in a ":for" list. Advance to the next.
2069 * Assign the values to the variable (list). "arg" points to the first one.
2070 * Return TRUE when a valid item was found, FALSE when at end of list or
2071 * something wrong.
2072 */
2073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002076 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002078 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002079 ? (ASSIGN_FINAL
2080 // first round: error if variable exists
2081 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002082 | ASSIGN_NO_MEMBER_TYPE
2083 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002084 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002086 int skip_assign = in_vim9script() && arg[0] == '_'
2087 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002088
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002089 if (fi->fi_blob != NULL)
2090 {
2091 typval_T tv;
2092
2093 if (fi->fi_bi >= blob_len(fi->fi_blob))
2094 return FALSE;
2095 tv.v_type = VAR_NUMBER;
2096 tv.v_lock = VAR_FIXED;
2097 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2098 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002099 if (skip_assign)
2100 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002101 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002102 fi->fi_varcount, flag, NULL) == OK;
2103 }
2104
2105 if (fi->fi_string != NULL)
2106 {
2107 typval_T tv;
2108 int len;
2109
2110 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2111 if (len == 0)
2112 return FALSE;
2113 tv.v_type = VAR_STRING;
2114 tv.v_lock = VAR_FIXED;
2115 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2116 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002117 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002118 if (skip_assign)
2119 result = TRUE;
2120 else
2121 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002122 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002123 vim_free(tv.vval.v_string);
2124 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002125 }
2126
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 item = fi->fi_lw.lw_item;
2128 if (item == NULL)
2129 result = FALSE;
2130 else
2131 {
2132 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002133 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002134 if (skip_assign)
2135 result = TRUE;
2136 else
2137 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002138 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 }
2140 return result;
2141}
2142
2143/*
2144 * Free the structure used to store info used by ":for".
2145 */
2146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002147free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148{
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002151 if (fi == NULL)
2152 return;
2153 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002154 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002156 list_unref(fi->fi_list);
2157 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002158 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002159 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002160 else
2161 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 vim_free(fi);
2163}
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166set_context_for_expression(
2167 expand_T *xp,
2168 char_u *arg,
2169 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002171 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002173 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002175 if (cmdidx == CMD_let || cmdidx == CMD_var
2176 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002177 {
2178 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002181 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183 {
2184 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002185 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002186 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002187 break;
2188 }
2189 return;
2190 }
2191 }
2192 else
2193 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2194 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 while ((xp->xp_pattern = vim_strpbrk(arg,
2196 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2197 {
2198 c = *xp->xp_pattern;
2199 if (c == '&')
2200 {
2201 c = xp->xp_pattern[1];
2202 if (c == '&')
2203 {
2204 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002205 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002210 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2211 xp->xp_pattern += 2;
2212
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
2215 else if (c == '$')
2216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002217 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 xp->xp_context = EXPAND_ENV_VARS;
2219 }
2220 else if (c == '=')
2221 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002222 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 xp->xp_context = EXPAND_EXPRESSION;
2224 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002225 else if (c == '#'
2226 && xp->xp_context == EXPAND_EXPRESSION)
2227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002228 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002229 break;
2230 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002231 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 && xp->xp_context == EXPAND_FUNCTIONS
2233 && vim_strchr(xp->xp_pattern, '(') == NULL)
2234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002235 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 break;
2237 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002238 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002240 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
2242 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2243 if (c == '\\' && xp->xp_pattern[1] != NUL)
2244 ++xp->xp_pattern;
2245 xp->xp_context = EXPAND_NOTHING;
2246 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002247 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002249 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2251 /* skip */ ;
2252 xp->xp_context = EXPAND_NOTHING;
2253 }
2254 else if (c == '|')
2255 {
2256 if (xp->xp_pattern[1] == '|')
2257 {
2258 ++xp->xp_pattern;
2259 xp->xp_context = EXPAND_EXPRESSION;
2260 }
2261 else
2262 xp->xp_context = EXPAND_COMMANDS;
2263 }
2264 else
2265 xp->xp_context = EXPAND_EXPRESSION;
2266 }
2267 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002268 // Doesn't look like something valid, expand as an expression
2269 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002270 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 arg = xp->xp_pattern;
2272 if (*arg != NUL)
2273 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2274 /* skip */ ;
2275 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002276
2277 // ":exe one two" completes "two"
2278 if ((cmdidx == CMD_execute
2279 || cmdidx == CMD_echo
2280 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002281 || cmdidx == CMD_echomsg
2282 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002283 && xp->xp_context == EXPAND_EXPRESSION)
2284 {
2285 for (;;)
2286 {
2287 char_u *n = skiptowhite(arg);
2288
2289 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2290 break;
2291 arg = skipwhite(n);
2292 }
2293 }
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 xp->xp_pattern = arg;
2296}
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002299 * Return TRUE if "pat" matches "text".
2300 * Does not use 'cpo' and always uses 'magic'.
2301 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002302 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002303pattern_match(char_u *pat, char_u *text, int ic)
2304{
2305 int matches = FALSE;
2306 char_u *save_cpo;
2307 regmatch_T regmatch;
2308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002309 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002310 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002311 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002312 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2313 if (regmatch.regprog != NULL)
2314 {
2315 regmatch.rm_ic = ic;
2316 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2317 vim_regfree(regmatch.regprog);
2318 }
2319 p_cpo = save_cpo;
2320 return matches;
2321}
2322
2323/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002324 * Handle a name followed by "(". Both for just "name(arg)" and for
2325 * "expr->name(arg)".
2326 * Returns OK or FAIL.
2327 */
2328 static int
2329eval_func(
2330 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002331 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002332 char_u *name,
2333 int name_len,
2334 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002335 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002336 typval_T *basetv) // "expr" for "expr->name(arg)"
2337{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002338 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002339 char_u *s = name;
2340 int len = name_len;
2341 partial_T *partial;
2342 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002343 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002344 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002345
2346 if (!evaluate)
2347 check_vars(s, len);
2348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002349 // If "s" is the name of a variable of type VAR_FUNC
2350 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002351 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002352 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 // Need to make a copy, in case evaluating the arguments makes
2355 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002356 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002357 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002358 ret = FAIL;
2359 else
2360 {
2361 funcexe_T funcexe;
2362
2363 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002364 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002365 funcexe.fe_firstline = curwin->w_cursor.lnum;
2366 funcexe.fe_lastline = curwin->w_cursor.lnum;
2367 funcexe.fe_evaluate = evaluate;
2368 funcexe.fe_partial = partial;
2369 funcexe.fe_basetv = basetv;
2370 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002371 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002372 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002373 }
2374 vim_free(s);
2375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 // If evaluate is FALSE rettv->v_type was not set in
2377 // get_func_tv, but it's needed in handle_subscript() to parse
2378 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002379 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2380 {
2381 rettv->vval.v_string = NULL;
2382 rettv->v_type = VAR_FUNC;
2383 }
2384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002385 // Stop the expression evaluation when immediately
2386 // aborting on error, or when an interrupt occurred or
2387 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002388 if (evaluate && aborting())
2389 {
2390 if (ret == OK)
2391 clear_tv(rettv);
2392 ret = FAIL;
2393 }
2394 return ret;
2395}
2396
2397/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002398 * After a NL, skip over empty lines and comment-only lines.
2399 */
2400 static char_u *
2401newline_skip_comments(char_u *arg)
2402{
2403 char_u *p = arg + 1;
2404
2405 for (;;)
2406 {
2407 p = skipwhite(p);
2408
2409 if (*p == NUL)
2410 break;
2411 if (vim9_comment_start(p))
2412 {
2413 char_u *nl = vim_strchr(p, NL);
2414
2415 if (nl == NULL)
2416 break;
2417 p = nl;
2418 }
2419 if (*p != NL)
2420 break;
2421 ++p; // skip another NL
2422 }
2423 return p;
2424}
2425
2426/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002427 * Get the next line source line without advancing. But do skip over comment
2428 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002429 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002430 */
2431 static char_u *
2432getline_peek_skip_comments(evalarg_T *evalarg)
2433{
2434 for (;;)
2435 {
2436 char_u *next = getline_peek(evalarg->eval_getline,
2437 evalarg->eval_cookie);
2438 char_u *p;
2439
2440 if (next == NULL)
2441 break;
2442 p = skipwhite(next);
2443 if (*p != NUL && !vim9_comment_start(p))
2444 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002445 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002446 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002447 }
2448 return NULL;
2449}
2450
2451/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002452 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2453 * comment) and there is a next line, return the next line (skipping blanks)
2454 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002455 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2456 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 * "arg" must point somewhere inside a line, not at the start.
2458 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002459 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2461{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002462 char_u *p = skipwhite(arg);
2463
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002464 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002465 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002466 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002467 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2468 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002469 && (*p == NUL || *p == NL
2470 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002472 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002473
Bram Moolenaare442d592022-05-05 12:20:28 +01002474 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002475 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002476 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002477 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002479 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480
Bram Moolenaar9d489562020-07-30 20:08:50 +02002481 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002482 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002483 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002484 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002485 }
2486 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002487 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488}
2489
2490/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002491 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002492 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002493 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002494 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002495eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002496{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002497 garray_T *gap = &evalarg->eval_ga;
2498 char_u *line;
2499
Bram Moolenaar39be4982022-05-06 21:51:50 +01002500 if (arg != NULL)
2501 {
2502 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002503 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002504 // Truncate before a trailing comment, so that concatenating the lines
2505 // won't turn the rest into a comment.
2506 if (*skipwhite(arg) == '#')
2507 *arg = NUL;
2508 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002509
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002510 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002511 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2512 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002513 else
2514 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002515 if (line == NULL)
2516 return NULL;
2517
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002518 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002519 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2520 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002521 char_u *p = skipwhite(line);
2522
2523 // Going to concatenate the lines after parsing. For an empty or
2524 // comment line use an empty string.
2525 if (*p == NUL || vim9_comment_start(p))
2526 {
2527 vim_free(line);
2528 line = vim_strsave((char_u *)"");
2529 }
2530
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002531 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2532 ++gap->ga_len;
2533 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002534 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002535 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002536 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002537 evalarg->eval_tofree = line;
2538 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002539
2540 // Advanced to the next line, "arg" no longer points into the previous
2541 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002542 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002543 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002544}
2545
Bram Moolenaare6b53242020-07-01 17:28:33 +02002546/*
2547 * Call eval_next_non_blank() and get the next line if needed.
2548 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002549 char_u *
2550skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2551{
2552 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002553 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002554
Bram Moolenaar962d7212020-07-04 14:15:00 +02002555 if (evalarg == NULL)
2556 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002557 eval_next_non_blank(p, evalarg, &getnext);
2558 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002559 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002560 return p;
2561}
2562
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002563/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002564 * The "eval" functions have an "evalarg" argument: When NULL or
2565 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2566 * parsed but not executed. The functions may return OK, but the rettv will be
2567 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
2569
2570/*
2571 * Handle zero level expression.
2572 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002574 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002575 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * Return OK or FAIL.
2577 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002579eval0(
2580 char_u *arg,
2581 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002582 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002585 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2586}
2587
2588/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002589 * If "arg" is a simple function call without arguments then call it and return
2590 * the result. Otherwise return NOTDONE.
2591 */
2592 int
2593may_call_simple_func(
2594 char_u *arg,
2595 typval_T *rettv)
2596{
2597 char_u *parens = (char_u *)strstr((char *)arg, "()");
2598 int r = NOTDONE;
2599
2600 // If the expression is "FuncName()" then we can skip a lot of overhead.
2601 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2602 {
2603 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2604
2605 if (to_name_end(p, TRUE) == parens)
2606 r = call_simple_func(arg, (int)(parens - arg), rettv);
2607 }
2608 return r;
2609}
2610
2611/*
2612 * Handle zero level expression with optimization for a simple function call.
2613 * Same arguments and return value as eval0().
2614 */
2615 int
2616eval0_simple_funccal(
2617 char_u *arg,
2618 typval_T *rettv,
2619 exarg_T *eap,
2620 evalarg_T *evalarg)
2621{
2622 int r = may_call_simple_func(arg, rettv);
2623
2624 if (r == NOTDONE)
2625 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2626 return r;
2627}
2628
2629/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002630 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2631 * expression and don't check what comes after the expression.
2632 */
2633 int
2634eval0_retarg(
2635 char_u *arg,
2636 typval_T *rettv,
2637 exarg_T *eap,
2638 evalarg_T *evalarg,
2639 char_u **retarg)
2640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 int ret;
2642 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002643 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002644 int did_emsg_before = did_emsg;
2645 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002646 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002647 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002651
Bram Moolenaar79481362022-06-27 20:15:10 +01002652 if (ret != FAIL)
2653 {
2654 expr_end = p;
2655 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002656
Bram Moolenaar79481362022-06-27 20:15:10 +01002657 // In Vim9 script a command block is not split at NL characters for
2658 // commands using an expression argument. Skip over a '#' comment to
2659 // check for a following NL. Require white space before the '#'.
2660 if (in_vim9script() && p > expr_end && retarg == NULL)
2661 while (*p == '#')
2662 {
2663 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002664
Bram Moolenaar79481362022-06-27 20:15:10 +01002665 if (nl == NULL)
2666 break;
2667 p = skipwhite(nl + 1);
2668 if (eap != NULL && *p != NUL)
2669 eap->nextcmd = p;
2670 check_for_end = FALSE;
2671 }
2672
2673 if (check_for_end)
2674 end_error = !ends_excmd2(arg, p);
2675 }
2676
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002677 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 /*
2682 * Report the invalid expression unless the expression evaluation has
2683 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002684 * exception, or we already gave a more specific error.
2685 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002687 if (!aborting()
2688 && did_emsg == did_emsg_before
2689 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002690 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002691 {
2692 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002693 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002694 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002695 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002696 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002697
zeertzjqf2588b62023-05-05 17:22:35 +01002698 if (eap != NULL && p != NULL)
2699 {
2700 // Some of the expression may not have been consumed.
2701 // Only execute a next command if it cannot be a "||" operator.
2702 // The next command may be "catch".
2703 char_u *nextcmd = check_nextcmd(p);
2704 if (nextcmd != NULL && *nextcmd != '|')
2705 eap->nextcmd = nextcmd;
2706 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002709
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002710 if (retarg != NULL)
2711 *retarg = p;
2712 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002713 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return ret;
2716}
2717
2718/*
2719 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002720 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002721 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *
2723 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002724 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002726 * Note: "rettv.v_lock" is not set.
2727 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 * Return OK or FAIL.
2729 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002730 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002731eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002733 char_u *p;
2734 int getnext;
2735
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002736 CLEAR_POINTER(rettv);
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 /*
2739 * Get the first variable.
2740 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002741 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 return FAIL;
2743
Bram Moolenaar793648f2020-06-26 21:28:25 +02002744 p = eval_next_non_blank(*arg, evalarg, &getnext);
2745 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002747 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748 int result;
2749 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750 evalarg_T *evalarg_used = evalarg;
2751 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002753 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002754 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755
2756 if (evalarg == NULL)
2757 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002758 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002759 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002761 orig_flags = evalarg_used->eval_flags;
2762 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002764 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002765 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002766 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002767 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002768 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002769 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002770 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002771 clear_tv(rettv);
2772 return FAIL;
2773 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002774 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002775 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 int error = FALSE;
2781
Bram Moolenaar13106602020-10-04 16:06:05 +02002782 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002783 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002784 else if (vim9script)
2785 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002786 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002788 if (error || !op_falsy || !result)
2789 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002790 if (error)
2791 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793
2794 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002795 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002797 if (op_falsy)
2798 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002799 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002800 {
mityu4ac198c2021-05-28 17:52:40 +02002801 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002802 clear_tv(rettv);
2803 return FAIL;
2804 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002806 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002807 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002808 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002809 {
2810 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002812 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002813 if (!op_falsy || !result)
2814 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002816 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002818 /*
2819 * Check for the ":".
2820 */
2821 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2822 if (*p != ':')
2823 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002824 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002825 if (evaluate && result)
2826 clear_tv(rettv);
2827 evalarg_used->eval_flags = orig_flags;
2828 return FAIL;
2829 }
2830 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002831 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002832 else
2833 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002834 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002835 {
2836 error_white_both(p, 1);
2837 clear_tv(rettv);
2838 evalarg_used->eval_flags = orig_flags;
2839 return FAIL;
2840 }
2841 *arg = p;
2842 }
2843
2844 /*
2845 * Get the third variable. Recursive!
2846 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002847 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002848 {
mityu4ac198c2021-05-28 17:52:40 +02002849 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002850 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002851 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002852 return FAIL;
2853 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002854 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2855 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002856 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002857 if (eval1(arg, &var2, evalarg_used) == FAIL)
2858 {
2859 if (evaluate && result)
2860 clear_tv(rettv);
2861 evalarg_used->eval_flags = orig_flags;
2862 return FAIL;
2863 }
2864 if (evaluate && !result)
2865 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002867
2868 if (evalarg == NULL)
2869 clear_evalarg(&local_evalarg, NULL);
2870 else
2871 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873
2874 return OK;
2875}
2876
2877/*
2878 * Handle first level expression:
2879 * expr2 || expr2 || expr2 logical OR
2880 *
2881 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002882 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 *
2884 * Return OK or FAIL.
2885 */
2886 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002889 char_u *p;
2890 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
2892 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002893 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 return FAIL;
2897
2898 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002899 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002901 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002902 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002904 evalarg_T *evalarg_used = evalarg;
2905 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906 int evaluate;
2907 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002908 long result = FALSE;
2909 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002910 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002911 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002912
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 if (evalarg == NULL)
2914 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002915 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002916 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002918 orig_flags = evalarg_used->eval_flags;
2919 evaluate = orig_flags & EVAL_EVALUATE;
2920 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002922 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002923 result = tv_get_bool_chk(rettv, &error);
2924 else if (tv_get_number_chk(rettv, &error) != 0)
2925 result = TRUE;
2926 clear_tv(rettv);
2927 if (error)
2928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930
2931 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002932 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002934 while (p[0] == '|' && p[1] == '|')
2935 {
2936 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002937 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002938 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002939 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002940 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002941 {
2942 error_white_both(p, 2);
2943 clear_tv(rettv);
2944 return FAIL;
2945 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002946 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002947 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002948
2949 /*
2950 * Get the second variable.
2951 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002952 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002953 {
mityu4ac198c2021-05-28 17:52:40 +02002954 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002955 clear_tv(rettv);
2956 return FAIL;
2957 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002958 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2959 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002960 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002961 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002962 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002963
2964 /*
2965 * Compute the result.
2966 */
2967 if (evaluate && !result)
2968 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002969 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002970 result = tv_get_bool_chk(&var2, &error);
2971 else if (tv_get_number_chk(&var2, &error) != 0)
2972 result = TRUE;
2973 clear_tv(&var2);
2974 if (error)
2975 return FAIL;
2976 }
2977 if (evaluate)
2978 {
2979 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002980 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002981 rettv->v_type = VAR_BOOL;
2982 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002983 }
2984 else
2985 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002986 rettv->v_type = VAR_NUMBER;
2987 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002988 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002989 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002990
2991 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002993
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002994 if (evalarg == NULL)
2995 clear_evalarg(&local_evalarg, NULL);
2996 else
2997 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999
3000 return OK;
3001}
3002
3003/*
3004 * Handle second level expression:
3005 * expr3 && expr3 && expr3 logical AND
3006 *
3007 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003008 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 *
3010 * Return OK or FAIL.
3011 */
3012 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003013eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003015 char_u *p;
3016 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003019 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003021 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 return FAIL;
3023
3024 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003025 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003027 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003028 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003030 evalarg_T *evalarg_used = evalarg;
3031 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032 int orig_flags;
3033 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003034 long result = TRUE;
3035 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003036 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003037 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003038
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 if (evalarg == NULL)
3040 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003041 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003042 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003044 orig_flags = evalarg_used->eval_flags;
3045 evaluate = orig_flags & EVAL_EVALUATE;
3046 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003048 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003049 result = tv_get_bool_chk(rettv, &error);
3050 else if (tv_get_number_chk(rettv, &error) == 0)
3051 result = FALSE;
3052 clear_tv(rettv);
3053 if (error)
3054 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056
3057 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003058 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003060 while (p[0] == '&' && p[1] == '&')
3061 {
3062 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003063 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003064 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003065 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003066 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003067 {
3068 error_white_both(p, 2);
3069 clear_tv(rettv);
3070 return FAIL;
3071 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003072 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003073 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003074
3075 /*
3076 * Get the second variable.
3077 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003078 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003079 {
mityu4ac198c2021-05-28 17:52:40 +02003080 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003081 clear_tv(rettv);
3082 return FAIL;
3083 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003084 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3085 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003086 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003087 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003088 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003090
3091 /*
3092 * Compute the result.
3093 */
3094 if (evaluate && result)
3095 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003096 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003097 result = tv_get_bool_chk(&var2, &error);
3098 else if (tv_get_number_chk(&var2, &error) == 0)
3099 result = FALSE;
3100 clear_tv(&var2);
3101 if (error)
3102 return FAIL;
3103 }
3104 if (evaluate)
3105 {
3106 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003107 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003108 rettv->v_type = VAR_BOOL;
3109 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003110 }
3111 else
3112 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003113 rettv->v_type = VAR_NUMBER;
3114 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003115 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003116 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003117
3118 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003120
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003121 if (evalarg == NULL)
3122 clear_evalarg(&local_evalarg, NULL);
3123 else
3124 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
3126
3127 return OK;
3128}
3129
3130/*
3131 * Handle third level expression:
3132 * var1 == var2
3133 * var1 =~ var2
3134 * var1 != var2
3135 * var1 !~ var2
3136 * var1 > var2
3137 * var1 >= var2
3138 * var1 < var2
3139 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003140 * var1 is var2
3141 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 *
3143 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003144 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 *
3146 * Return OK or FAIL.
3147 */
3148 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003149eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003152 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003153 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003155 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003158 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003160 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return FAIL;
3162
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003163 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003164
Bram Moolenaar696ba232020-07-29 21:20:41 +02003165 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003170 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003172 typval_T var2;
3173 int ic;
3174 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003175 int evaluate = evalarg == NULL
3176 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003177 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003178
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003179 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003180 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003181 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003182 p = *arg;
3183 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003184 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3185 {
mityu4ac198c2021-05-28 17:52:40 +02003186 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003187 clear_tv(rettv);
3188 return FAIL;
3189 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003190
Bram Moolenaar696ba232020-07-29 21:20:41 +02003191 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3192 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003193 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003194 clear_tv(rettv);
3195 return FAIL;
3196 }
3197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (p[len] == '?')
3200 {
3201 ic = TRUE;
3202 ++len;
3203 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003204 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 else if (p[len] == '#')
3206 {
3207 ic = FALSE;
3208 ++len;
3209 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003210 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003212 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 /*
3215 * Get the second variable.
3216 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003217 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3218 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003219 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003220 clear_tv(rettv);
3221 return FAIL;
3222 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003223 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003226 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return FAIL;
3228 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003229 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003230 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003231 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003232
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003233 // use the line of the comparison for messages
3234 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003235 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003236 {
3237 ret = FAIL;
3238 clear_tv(rettv);
3239 }
3240 else
3241 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003242 clear_tv(&var2);
3243 return ret;
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246
3247 return OK;
3248}
3249
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003250/*
3251 * Make a copy of blob "tv1" and append blob "tv2".
3252 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 void
3254eval_addblob(typval_T *tv1, typval_T *tv2)
3255{
3256 blob_T *b1 = tv1->vval.v_blob;
3257 blob_T *b2 = tv2->vval.v_blob;
3258 blob_T *b = blob_alloc();
3259 int i;
3260
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003261 if (b == NULL)
3262 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003264 for (i = 0; i < blob_len(b1); i++)
3265 ga_append(&b->bv_ga, blob_get(b1, i));
3266 for (i = 0; i < blob_len(b2); i++)
3267 ga_append(&b->bv_ga, blob_get(b2, i));
3268
3269 clear_tv(tv1);
3270 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271}
3272
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003273/*
3274 * Make a copy of list "tv1" and append list "tv2".
3275 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 int
3277eval_addlist(typval_T *tv1, typval_T *tv2)
3278{
3279 typval_T var3;
3280
3281 // concatenate Lists
3282 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3283 {
3284 clear_tv(tv1);
3285 clear_tv(tv2);
3286 return FAIL;
3287 }
3288 clear_tv(tv1);
3289 *tv1 = var3;
3290 return OK;
3291}
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003294 * Handle the bitwise left/right shift operator expression:
3295 * var1 << var2
3296 * var1 >> var2
3297 *
3298 * "arg" must point to the first non-white of the expression.
3299 * "arg" is advanced to just after the recognized expression.
3300 *
3301 * Return OK or FAIL.
3302 */
3303 static int
3304eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3305{
3306 /*
3307 * Get the first expression.
3308 */
3309 if (eval6(arg, rettv, evalarg) == FAIL)
3310 return FAIL;
3311
3312 /*
3313 * Repeat computing, until no '<<' or '>>' is following.
3314 */
3315 for (;;)
3316 {
3317 char_u *p;
3318 int getnext;
3319 exprtype_T type;
3320 int evaluate;
3321 typval_T var2;
3322 int vim9script;
3323
3324 p = eval_next_non_blank(*arg, evalarg, &getnext);
3325 if (p[0] == '<' && p[1] == '<')
3326 type = EXPR_LSHIFT;
3327 else if (p[0] == '>' && p[1] == '>')
3328 type = EXPR_RSHIFT;
3329 else
3330 return OK;
3331
3332 // Handle a bitwise left or right shift operator
3333 if (rettv->v_type != VAR_NUMBER)
3334 {
3335 // left operand should be a number
3336 emsg(_(e_bitshift_ops_must_be_number));
3337 clear_tv(rettv);
3338 return FAIL;
3339 }
3340
3341 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3342 vim9script = in_vim9script();
3343 if (getnext)
3344 {
3345 *arg = eval_next_line(*arg, evalarg);
3346 p = *arg;
3347 }
3348 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3349 {
3350 error_white_both(*arg, 2);
3351 clear_tv(rettv);
3352 return FAIL;
3353 }
3354
3355 /*
3356 * Get the second variable.
3357 */
3358 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3359 {
3360 error_white_both(p, 2);
3361 clear_tv(rettv);
3362 return FAIL;
3363 }
3364 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3365 if (eval6(arg, &var2, evalarg) == FAIL)
3366 {
3367 clear_tv(rettv);
3368 return FAIL;
3369 }
3370
3371 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3372 {
3373 // right operand should be a positive number
3374 if (var2.v_type != VAR_NUMBER)
3375 emsg(_(e_bitshift_ops_must_be_number));
3376 else
dundargocc57b5bc2022-11-02 13:30:51 +00003377 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003378 clear_tv(rettv);
3379 clear_tv(&var2);
3380 return FAIL;
3381 }
3382
3383 if (evaluate)
3384 {
3385 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3386 // shifting more bits than we have always results in zero
3387 rettv->vval.v_number = 0;
3388 else if (type == EXPR_LSHIFT)
3389 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003390 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003391 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003392 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003393 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003394 }
3395
3396 clear_tv(&var2);
3397 }
3398
3399 return OK;
3400}
3401
3402/*
3403 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003404 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003406 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003407 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *
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 +01003415eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003418 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003420 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return FAIL;
3422
3423 /*
3424 * Repeat computing, until no '+', '-' or '.' is following.
3425 */
3426 for (;;)
3427 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003428 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 int getnext;
3430 char_u *p;
3431 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003432 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003433 int concat;
3434 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003435 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003437 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003438 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003439 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440 p = eval_next_non_blank(*arg, evalarg, &getnext);
3441 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003442 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003443 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3444 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003446 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3447 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003448
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003449 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003450 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003451 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003452 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003453 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003454 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003455 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003456 {
mityu4ac198c2021-05-28 17:52:40 +02003457 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003458 clear_tv(rettv);
3459 return FAIL;
3460 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003461 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003462 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003463 if ((op != '+' || (rettv->v_type != VAR_LIST
3464 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003465 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003466 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003467 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003468 int error = FALSE;
3469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // For "list + ...", an illegal use of the first operand as
3471 // a number cannot be determined before evaluating the 2nd
3472 // operand: if this is also a list, all is ok.
3473 // For "something . ...", "something - ..." or "non-list + ...",
3474 // we know that the first operand needs to be a string or number
3475 // without evaluating the 2nd operand. So check before to avoid
3476 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003477 if (op != '.')
3478 tv_get_number_chk(rettv, &error);
3479 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003480 {
3481 clear_tv(rettv);
3482 return FAIL;
3483 }
3484 }
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /*
3487 * Get the second variable.
3488 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003489 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003490 {
mityu89dcb4d2021-05-28 13:50:17 +02003491 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003492 clear_tv(rettv);
3493 return FAIL;
3494 }
3495 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003496 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003498 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 return FAIL;
3500 }
3501
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003502 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 /*
3505 * Compute the result.
3506 */
3507 if (op == '.')
3508 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003509 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3510 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003511 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003512
Bram Moolenaar2e086612020-08-25 22:37:48 +02003513 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003514 || var2.v_type == VAR_CHANNEL
3515 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003516 semsg(_(e_using_invalid_value_as_string_str),
3517 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003518 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003519 {
3520 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3521 var2.vval.v_float);
3522 s2 = buf2;
3523 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003524 else
3525 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003526 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003527 {
3528 clear_tv(rettv);
3529 clear_tv(&var2);
3530 return FAIL;
3531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003532 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(rettv);
3534 rettv->v_type = VAR_STRING;
3535 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003537 else if (op == '+' && rettv->v_type == VAR_BLOB
3538 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003540 else if (op == '+' && rettv->v_type == VAR_LIST
3541 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003542 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003544 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else
3547 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003548 int error = FALSE;
3549 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003550 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003551
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003552 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554 f1 = rettv->vval.v_float;
3555 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003559 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003560 if (error)
3561 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003562 // This can only happen for "list + non-list" or
3563 // "blob + non-blob". For "non-list + ..." or
3564 // "something - ...", we returned before evaluating the
3565 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003567 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 return FAIL;
3569 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 if (var2.v_type == VAR_FLOAT)
3571 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003573 if (var2.v_type == VAR_FLOAT)
3574 {
3575 f2 = var2.vval.v_float;
3576 n2 = 0;
3577 }
3578 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003580 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003581 if (error)
3582 {
3583 clear_tv(rettv);
3584 clear_tv(&var2);
3585 return FAIL;
3586 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587 if (rettv->v_type == VAR_FLOAT)
3588 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003590 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003592 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003593 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3594 {
3595 if (op == '+')
3596 f1 = f1 + f2;
3597 else
3598 f1 = f1 - f2;
3599 rettv->v_type = VAR_FLOAT;
3600 rettv->vval.v_float = f1;
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003603 {
3604 if (op == '+')
3605 n1 = n1 + n2;
3606 else
3607 n1 = n1 - n2;
3608 rettv->v_type = VAR_NUMBER;
3609 rettv->vval.v_number = n1;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 }
3615 return OK;
3616}
3617
3618/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003619 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 * * number multiplication
3621 * / number division
3622 * % number modulo
3623 *
3624 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003625 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 *
3627 * Return OK or FAIL.
3628 */
3629 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003630eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631 char_u **arg,
3632 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003633 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003636 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003639 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003641 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 return FAIL;
3643
3644 /*
3645 * Repeat computing, until no '*', '/' or '%' is following.
3646 */
3647 for (;;)
3648 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003649 int evaluate;
3650 int getnext;
3651 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003652 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003653 int op;
3654 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003655 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003656 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003657
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003658 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003659 p = eval_next_non_blank(*arg, evalarg, &getnext);
3660 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003661 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003663
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003664 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003665 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003666 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003667 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003668 {
3669 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3670 {
mityu4ac198c2021-05-28 17:52:40 +02003671 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003672 clear_tv(rettv);
3673 return FAIL;
3674 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003675 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003678 f1 = 0;
3679 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003680 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003681 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003683 if (rettv->v_type == VAR_FLOAT)
3684 {
3685 f1 = rettv->vval.v_float;
3686 use_float = TRUE;
3687 n1 = 0;
3688 }
3689 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003690 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003691 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003692 if (error)
3693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 else
3696 n1 = 0;
3697
3698 /*
3699 * Get the second variable.
3700 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003701 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3702 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003703 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003704 clear_tv(rettv);
3705 return FAIL;
3706 }
3707 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003708 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return FAIL;
3710
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003711 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713 if (var2.v_type == VAR_FLOAT)
3714 {
3715 if (!use_float)
3716 {
3717 f1 = n1;
3718 use_float = TRUE;
3719 }
3720 f2 = var2.vval.v_float;
3721 n2 = 0;
3722 }
3723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003725 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726 clear_tv(&var2);
3727 if (error)
3728 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003729 if (use_float)
3730 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 /*
3734 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003737 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003739 if (op == '*')
3740 f1 = f1 * f2;
3741 else if (op == '/')
3742 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003743#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003745 if (f2 == 0.0)
3746 {
3747 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003749 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003750 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003751 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003752 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003753 }
3754 else
3755 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003756#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003757 // We rely on the floating point library to handle divide
3758 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003759 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003760#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003763 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003764 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765 return FAIL;
3766 }
3767 rettv->v_type = VAR_FLOAT;
3768 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 else
3771 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003772 int failed = FALSE;
3773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003774 if (op == '*')
3775 n1 = n1 * n2;
3776 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003777 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003779 n1 = num_modulus(n1, n2, &failed);
3780 if (failed)
3781 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003783 rettv->v_type = VAR_NUMBER;
3784 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 }
3788
3789 return OK;
3790}
3791
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003792/*
3793 * Handle a type cast before a base level expression.
3794 * "arg" must point to the first non-white of the expression.
3795 * "arg" is advanced to just after the recognized expression.
3796 * Return OK or FAIL.
3797 */
3798 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003799eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003800 char_u **arg,
3801 typval_T *rettv,
3802 evalarg_T *evalarg,
3803 int want_string) // after "." operator
3804{
3805 type_T *want_type = NULL;
3806 garray_T type_list; // list of pointers to allocated types
3807 int res;
3808 int evaluate = evalarg == NULL ? 0
3809 : (evalarg->eval_flags & EVAL_EVALUATE);
3810
3811 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003812 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3813 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003814 {
3815 ++*arg;
3816 ga_init2(&type_list, sizeof(type_T *), 10);
3817 want_type = parse_type(arg, &type_list, TRUE);
3818 if (want_type == NULL && (evaluate || **arg != '>'))
3819 {
3820 clear_type_list(&type_list);
3821 return FAIL;
3822 }
3823
3824 if (**arg != '>')
3825 {
3826 if (*skipwhite(*arg) == '>')
3827 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3828 else
3829 emsg(_(e_missing_gt));
3830 clear_type_list(&type_list);
3831 return FAIL;
3832 }
3833 ++*arg;
3834 *arg = skipwhite_and_linebreak(*arg, evalarg);
3835 }
3836
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003837 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003838
3839 if (want_type != NULL && evaluate)
3840 {
3841 if (res == OK)
3842 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003843 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3844 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003845
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003846 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003847 {
3848 if (want_type == &t_bool && actual != &t_bool
3849 && (actual->tt_flags & TTFLAG_BOOL_OK))
3850 {
3851 int n = tv2bool(rettv);
3852
3853 // can use "0" and "1" for boolean in some places
3854 clear_tv(rettv);
3855 rettv->v_type = VAR_BOOL;
3856 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3857 }
3858 else
3859 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003860 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003861
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003862 res = check_type(want_type, actual, TRUE, where);
3863 }
3864 }
3865 }
3866 clear_type_list(&type_list);
3867 }
3868
3869 return res;
3870}
3871
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003872 int
3873eval_leader(char_u **arg, int vim9)
3874{
3875 char_u *s = *arg;
3876 char_u *p = *arg;
3877
3878 while (*p == '!' || *p == '-' || *p == '+')
3879 {
3880 char_u *n = skipwhite(p + 1);
3881
3882 // ++, --, -+ and +- are not accepted in Vim9 script
3883 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3884 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003885 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003886 return FAIL;
3887 }
3888 p = n;
3889 }
3890 *arg = p;
3891 return OK;
3892}
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003895 * Check for a predefined value "true", "false" and "null.*".
3896 * Return OK when recognized.
3897 */
3898 int
3899handle_predefined(char_u *s, int len, typval_T *rettv)
3900{
3901 switch (len)
3902 {
3903 case 4: if (STRNCMP(s, "true", 4) == 0)
3904 {
3905 rettv->v_type = VAR_BOOL;
3906 rettv->vval.v_number = VVAL_TRUE;
3907 return OK;
3908 }
3909 if (STRNCMP(s, "null", 4) == 0)
3910 {
3911 rettv->v_type = VAR_SPECIAL;
3912 rettv->vval.v_number = VVAL_NULL;
3913 return OK;
3914 }
3915 break;
3916 case 5: if (STRNCMP(s, "false", 5) == 0)
3917 {
3918 rettv->v_type = VAR_BOOL;
3919 rettv->vval.v_number = VVAL_FALSE;
3920 return OK;
3921 }
3922 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003923 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3924 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003925#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003926 rettv->v_type = VAR_JOB;
3927 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003928#else
3929 rettv->v_type = VAR_SPECIAL;
3930 rettv->vval.v_number = VVAL_NULL;
3931#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003932 return OK;
3933 }
3934 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003935 case 9:
3936 if (STRNCMP(s, "null_", 5) != 0)
3937 break;
3938 if (STRNCMP(s + 5, "list", 4) == 0)
3939 {
3940 rettv->v_type = VAR_LIST;
3941 rettv->vval.v_list = NULL;
3942 return OK;
3943 }
3944 if (STRNCMP(s + 5, "dict", 4) == 0)
3945 {
3946 rettv->v_type = VAR_DICT;
3947 rettv->vval.v_dict = NULL;
3948 return OK;
3949 }
3950 if (STRNCMP(s + 5, "blob", 4) == 0)
3951 {
3952 rettv->v_type = VAR_BLOB;
3953 rettv->vval.v_blob = NULL;
3954 return OK;
3955 }
3956 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003957 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3958 {
3959 rettv->v_type = VAR_CLASS;
3960 rettv->vval.v_class = NULL;
3961 return OK;
3962 }
3963 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003964 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3965 {
3966 rettv->v_type = VAR_STRING;
3967 rettv->vval.v_string = NULL;
3968 return OK;
3969 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003970 if (STRNCMP(s, "null_object", 11) == 0)
3971 {
3972 rettv->v_type = VAR_OBJECT;
3973 rettv->vval.v_object = NULL;
3974 return OK;
3975 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003976 break;
3977 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003978 if (STRNCMP(s, "null_channel", 12) == 0)
3979 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003980#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003981 rettv->v_type = VAR_CHANNEL;
3982 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003983#else
3984 rettv->v_type = VAR_SPECIAL;
3985 rettv->vval.v_number = VVAL_NULL;
3986#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003987 return OK;
3988 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003989 if (STRNCMP(s, "null_partial", 12) == 0)
3990 {
3991 rettv->v_type = VAR_PARTIAL;
3992 rettv->vval.v_partial = NULL;
3993 return OK;
3994 }
3995 break;
3996 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3997 {
3998 rettv->v_type = VAR_FUNC;
3999 rettv->vval.v_string = NULL;
4000 return OK;
4001 }
4002 break;
4003 }
4004 return FAIL;
4005}
4006
4007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 * Handle sixth level expression:
4009 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004010 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004011 * "string" string constant
4012 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * &option-name option value
4014 * @r register contents
4015 * identifier variable value
4016 * function() function call
4017 * $VAR environment variable
4018 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004019 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004021 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004022 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 *
4024 * Also handle:
4025 * ! in front logical NOT
4026 * - in front unary minus
4027 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004028 * trailing [] subscript in String or List
4029 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004030 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004033 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *
4035 * Return OK or FAIL.
4036 */
4037 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004038eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004039 char_u **arg,
4040 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004041 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004044 int evaluate = evalarg != NULL
4045 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int len;
4047 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004048 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *start_leader, *end_leader;
4050 int ret = OK;
4051 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004052 static int recurse = 0;
4053 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004057 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004062 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
4064 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004065 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004066 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 end_leader = *arg;
4068
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004069 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004070 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004071 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004072 ++*arg;
4073 return FAIL;
4074 }
4075
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004076 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004077 // and crash. With MSVC the stack is smaller.
4078 if (recurse ==
4079#ifdef _MSC_VER
4080 300
4081#else
4082 1000
4083#endif
4084 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004085 {
4086 semsg(_(e_expression_too_recursive_str), *arg);
4087 return FAIL;
4088 }
4089 ++recurse;
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 switch (**arg)
4092 {
4093 /*
4094 * Number constant.
4095 */
4096 case '0':
4097 case '1':
4098 case '2':
4099 case '3':
4100 case '4':
4101 case '5':
4102 case '6':
4103 case '7':
4104 case '8':
4105 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004106 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004107
4108 // Apply prefixed "-" and "+" now. Matters especially when
4109 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004110 if (ret == OK && evaluate && end_leader > start_leader
4111 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004112 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 break;
4114
4115 /*
4116 * String constant: "string".
4117 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004118 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 break;
4120
4121 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004124 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004125 break;
4126
4127 /*
4128 * List: [expr, expr]
4129 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004130 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 break;
4132
4133 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004134 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004135 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004136 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004137 {
4138 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4139 }
4140 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004141 {
4142 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004143 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004144 }
4145 else
4146 ret = NOTDONE;
4147 break;
4148
4149 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004150 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004151 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004153 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004154 ret = NOTDONE;
4155 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004156 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004157 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004158 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004159 break;
4160
4161 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004162 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004164 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166
4167 /*
4168 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004169 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
LemonBoy2eaef102022-05-06 13:14:50 +01004171 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4172 ret = eval_interp_string(arg, rettv, evaluate);
4173 else
4174 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 break;
4176
4177 /*
4178 * Register contents: @r.
4179 */
4180 case '@': ++*arg;
4181 if (evaluate)
4182 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004183 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004184 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004185 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004186 emsg_invreg(**arg);
4187 else
4188 {
4189 rettv->v_type = VAR_STRING;
4190 rettv->vval.v_string = get_reg_contents(**arg,
4191 GREG_EXPR_SRC);
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 if (**arg != NUL)
4195 ++*arg;
4196 break;
4197
4198 /*
4199 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004200 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004202 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004203 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004204 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004205 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004206 if (ret == OK && evaluate)
4207 {
4208 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4209
Bram Moolenaara9931532021-06-12 15:58:16 +02004210 // Compile it here to get the return type. The return
4211 // type is optional, when it's missing use t_unknown.
4212 // This is recognized in compile_return().
4213 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4214 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004215 if (compile_def_function(ufunc, FALSE,
4216 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004217 {
4218 clear_tv(rettv);
4219 ret = FAIL;
4220 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004221 }
4222 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004223 if (ret == NOTDONE)
4224 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004225 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004226 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004227
Bram Moolenaar9215f012020-06-27 21:18:00 +02004228 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004229 if (**arg == ')')
4230 ++*arg;
4231 else if (ret == OK)
4232 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004233 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004234 clear_tv(rettv);
4235 ret = FAIL;
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238 break;
4239
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 break;
4242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243
4244 if (ret == NOTDONE)
4245 {
4246 /*
4247 * Must be a variable or function name.
4248 * Can also be a curly-braces kind of name: {expr}.
4249 */
4250 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004251 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 if (alias != NULL)
4253 s = alias;
4254
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004255 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004256 ret = FAIL;
4257 else
4258 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004259 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4260
Bram Moolenaar4525a572022-02-13 11:57:33 +00004261 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004262 {
4263 emsg(_(e_cannot_use_underscore_here));
4264 ret = FAIL;
4265 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004266 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004267 && s[0] == 's' && s[1] == ':')
4268 {
4269 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4270 ret = FAIL;
4271 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004272 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004273 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004274 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004275 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004276 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004278 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004279 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004280 // get the value of "true", "false", etc. or a variable
4281 ret = FAIL;
4282 if (vim9script)
4283 ret = handle_predefined(s, len, rettv);
4284 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004285 {
4286 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004287 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004288 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004289 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004290 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004291 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004292 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004293 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004294 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004295 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004296 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004297 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004298 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004299 }
4300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004301 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4302 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004303 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004304 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4308 */
4309 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004310 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004311
4312 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004313 return ret;
4314}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004316/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004317 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004318 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004319 * Adjusts "end_leaderp" until it is at "start_leader".
4320 */
4321 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004322eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004323 typval_T *rettv,
4324 int numeric_only,
4325 char_u *start_leader,
4326 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004327{
4328 char_u *end_leader = *end_leaderp;
4329 int ret = OK;
4330 int error = FALSE;
4331 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004332 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004333 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004334 float_T f = 0.0;
4335
4336 if (rettv->v_type == VAR_FLOAT)
4337 f = rettv->vval.v_float;
4338 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004339 {
4340 while (VIM_ISWHITE(end_leader[-1]))
4341 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004342 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004343 val = tv2bool(rettv);
4344 else
4345 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004346 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004347 if (error)
4348 {
4349 clear_tv(rettv);
4350 ret = FAIL;
4351 }
4352 else
4353 {
4354 while (end_leader > start_leader)
4355 {
4356 --end_leader;
4357 if (*end_leader == '!')
4358 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004359 if (numeric_only)
4360 {
4361 ++end_leader;
4362 break;
4363 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004364 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004365 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004366 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004367 {
4368 rettv->v_type = VAR_BOOL;
4369 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4370 }
4371 else
4372 f = !f;
4373 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004374 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004375 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004376 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004377 type = VAR_BOOL;
4378 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004379 }
4380 else if (*end_leader == '-')
4381 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004382 if (rettv->v_type == VAR_FLOAT)
4383 f = -f;
4384 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004385 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004386 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004387 type = VAR_NUMBER;
4388 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004389 }
4390 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004391 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004394 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 else
4397 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004398 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004399 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004400 rettv->v_type = type;
4401 else
4402 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004403 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004406 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return ret;
4408}
4409
4410/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004411 * Call the function referred to in "rettv".
4412 */
4413 static int
4414call_func_rettv(
4415 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004416 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004417 typval_T *rettv,
4418 int evaluate,
4419 dict_T *selfdict,
4420 typval_T *basetv)
4421{
4422 partial_T *pt = NULL;
4423 funcexe_T funcexe;
4424 typval_T functv;
4425 char_u *s;
4426 int ret;
4427
4428 // need to copy the funcref so that we can clear rettv
4429 if (evaluate)
4430 {
4431 functv = *rettv;
4432 rettv->v_type = VAR_UNKNOWN;
4433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004434 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004435 if (functv.v_type == VAR_PARTIAL)
4436 {
4437 pt = functv.vval.v_partial;
4438 s = partial_name(pt);
4439 }
4440 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004441 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004442 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004443 if (s == NULL || *s == NUL)
4444 {
4445 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004446 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004447 goto theend;
4448 }
4449 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004450 }
4451 else
4452 s = (char_u *)"";
4453
Bram Moolenaara80faa82020-04-12 19:37:17 +02004454 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004455 funcexe.fe_firstline = curwin->w_cursor.lnum;
4456 funcexe.fe_lastline = curwin->w_cursor.lnum;
4457 funcexe.fe_evaluate = evaluate;
4458 funcexe.fe_partial = pt;
4459 funcexe.fe_selfdict = selfdict;
4460 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004461 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004462
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004463theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // Clear the funcref afterwards, so that deleting it while
4465 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004466 if (evaluate)
4467 clear_tv(&functv);
4468
4469 return ret;
4470}
4471
4472/*
4473 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004474 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004475 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4476 */
4477 static int
4478eval_lambda(
4479 char_u **arg,
4480 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004481 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004483{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004484 int evaluate = evalarg != NULL
4485 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004486 typval_T base = *rettv;
4487 int ret;
4488
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004489 rettv->v_type = VAR_UNKNOWN;
4490
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004491 if (**arg == '{')
4492 {
4493 // ->{lambda}()
4494 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4495 }
4496 else
4497 {
4498 // ->(lambda)()
4499 ++*arg;
4500 ret = eval1(arg, rettv, evalarg);
4501 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004502 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004503 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004504 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004505 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004506 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004507 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4508 && rettv->v_type != VAR_PARTIAL)
4509 {
4510 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4511 return FAIL;
4512 }
4513 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004514 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004515 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004516 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004517
4518 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004519 {
4520 if (verbose)
4521 {
4522 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004523 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004524 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004525 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004526 }
4527 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004528 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004529 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004530 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004531 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004532
4533 // Clear the funcref afterwards, so that deleting it while
4534 // evaluating the arguments is possible (see test55).
4535 if (evaluate)
4536 clear_tv(&base);
4537
4538 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004539}
4540
4541/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004542 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004543 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004544 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4545 */
4546 static int
4547eval_method(
4548 char_u **arg,
4549 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004550 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004552{
4553 char_u *name;
4554 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004555 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004556 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004557 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004558 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004559 int evaluate = evalarg != NULL
4560 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004561
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004562 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004563
Bram Moolenaarac92e252019-08-03 21:58:38 +02004564 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004565 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004566 if (alias != NULL)
4567 name = alias;
4568
4569 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004570 {
4571 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004572 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004573 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004574 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004575 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004576 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004577 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004578
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004579 // If there is no "(" immediately following, but there is further on,
4580 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4581 // Does not handle anything where "(" is part of the expression.
4582 *arg = skipwhite(*arg);
4583
4584 if (**arg != '(' && alias == NULL
4585 && (paren = vim_strchr(*arg, '(')) != NULL)
4586 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004587 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004588
4589 // Truncate the name a the "(". Avoid trying to get another line
4590 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004591 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004592 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4593 if (evalarg != NULL)
4594 {
4595 getline = evalarg->eval_getline;
4596 evalarg->eval_getline = NULL;
4597 }
4598
4599 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004600 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004601 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004602 *arg = name + len;
4603 ret = FAIL;
4604 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004605 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004606 {
4607 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004608 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004609 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004610
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004611 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004612 if (getline != NULL)
4613 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004614 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004615
4616 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004617 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004618 *arg = skipwhite(*arg);
4619
4620 if (**arg != '(')
4621 {
4622 if (verbose)
4623 semsg(_(e_missing_parenthesis_str), name);
4624 ret = FAIL;
4625 }
4626 else if (VIM_ISWHITE((*arg)[-1]))
4627 {
4628 if (verbose)
4629 emsg(_(e_no_white_space_allowed_before_parenthesis));
4630 ret = FAIL;
4631 }
4632 else
4633 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004634 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004635 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004636 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004637
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004638 // Clear the funcref afterwards, so that deleting it while
4639 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004640 if (evaluate)
4641 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004642 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004643
Bram Moolenaarac92e252019-08-03 21:58:38 +02004644 return ret;
4645}
4646
4647/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004648 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4649 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4651 */
4652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004653eval_index(
4654 char_u **arg,
4655 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004656 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004659 int evaluate = evalarg != NULL
4660 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004662 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004665 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004666 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004668 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4669 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004671 init_tv(&var1);
4672 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 /*
4676 * dict.name
4677 */
4678 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004681 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004683 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 }
4685 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 /*
4688 * something[idx]
4689 *
4690 * Get the (first) variable from inside the [].
4691 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004692 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 if (**arg == ':')
4694 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004695 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004697 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004698 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004699 semsg(_(e_white_space_required_before_and_after_str_at_str),
4700 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004701 clear_tv(&var1);
4702 return FAIL;
4703 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004704 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004706 int error = FALSE;
4707
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004708 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004709 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004710 && var1.v_type == VAR_FLOAT)
4711 {
4712 var1.vval.v_string = typval_tostring(&var1, TRUE);
4713 var1.v_type = VAR_STRING;
4714 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004715
Bram Moolenaar4525a572022-02-13 11:57:33 +00004716 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004717 tv_get_number_chk(&var1, &error);
4718 else
4719 error = tv_get_string_chk(&var1) == NULL;
4720 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004721 {
4722 // not a number or string
4723 clear_tv(&var1);
4724 return FAIL;
4725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727
4728 /*
4729 * Get the second variable from inside the [:].
4730 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004731 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 if (**arg == ':')
4733 {
4734 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004735 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004736 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004737 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004738 semsg(_(e_white_space_required_before_and_after_str_at_str),
4739 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004740 if (!empty1)
4741 clear_tv(&var1);
4742 return FAIL;
4743 }
4744 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 if (**arg == ']')
4746 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004747 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 if (!empty1)
4750 clear_tv(&var1);
4751 return FAIL;
4752 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004753 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 if (!empty1)
4757 clear_tv(&var1);
4758 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 return FAIL;
4760 }
4761 }
4762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004764 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765 if (**arg != ']')
4766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004768 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 clear_tv(&var1);
4770 if (range)
4771 clear_tv(&var2);
4772 return FAIL;
4773 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004774 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 }
4776
4777 if (evaluate)
4778 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004779 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004780 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004781 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004782
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004786 clear_tv(&var2);
4787 return res;
4788 }
4789 return OK;
4790}
4791
4792/*
4793 * Check if "rettv" can have an [index] or [sli:ce]
4794 */
4795 int
4796check_can_index(typval_T *rettv, int evaluate, int verbose)
4797{
4798 switch (rettv->v_type)
4799 {
4800 case VAR_FUNC:
4801 case VAR_PARTIAL:
4802 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004803 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004804 return FAIL;
4805 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004806 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004807 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004808 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809 case VAR_BOOL:
4810 case VAR_SPECIAL:
4811 case VAR_JOB:
4812 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004813 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004814 case VAR_CLASS:
4815 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004816 if (verbose)
4817 emsg(_(e_cannot_index_special_variable));
4818 return FAIL;
4819 case VAR_UNKNOWN:
4820 case VAR_ANY:
4821 case VAR_VOID:
4822 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004824 emsg(_(e_cannot_index_special_variable));
4825 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004827 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004829 case VAR_STRING:
4830 case VAR_LIST:
4831 case VAR_DICT:
4832 case VAR_BLOB:
4833 break;
4834 case VAR_NUMBER:
4835 if (in_vim9script())
4836 emsg(_(e_cannot_index_number));
4837 break;
4838 }
4839 return OK;
4840}
4841
4842/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004843 * slice() function
4844 */
4845 void
4846f_slice(typval_T *argvars, typval_T *rettv)
4847{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004848 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004849 && ((argvars[0].v_type != VAR_STRING
4850 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004851 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004852 && check_for_list_arg(argvars, 0) == FAIL)
4853 || check_for_number_arg(argvars, 1) == FAIL
4854 || check_for_opt_number_arg(argvars, 2) == FAIL))
4855 return;
4856
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004857 if (check_can_index(argvars, TRUE, FALSE) != OK)
4858 return;
4859
4860 copy_tv(argvars, rettv);
4861 eval_index_inner(rettv, TRUE, argvars + 1,
4862 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4863 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004864}
4865
4866/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004867 * Apply index or range to "rettv".
4868 * "var1" is the first index, NULL for [:expr].
4869 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004870 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4871 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4873 */
4874 int
4875eval_index_inner(
4876 typval_T *rettv,
4877 int is_range,
4878 typval_T *var1,
4879 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004880 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004881 char_u *key,
4882 int keylen,
4883 int verbose)
4884{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004885 varnumber_T n1, n2 = 0;
4886 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004887
4888 n1 = 0;
4889 if (var1 != NULL && rettv->v_type != VAR_DICT)
4890 n1 = tv_get_number(var1);
4891
4892 if (is_range)
4893 {
4894 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004896 if (verbose)
4897 emsg(_(e_cannot_slice_dictionary));
4898 return FAIL;
4899 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004900 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004901 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004902 else
4903 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004904 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004905
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004906 switch (rettv->v_type)
4907 {
4908 case VAR_UNKNOWN:
4909 case VAR_ANY:
4910 case VAR_VOID:
4911 case VAR_FUNC:
4912 case VAR_PARTIAL:
4913 case VAR_FLOAT:
4914 case VAR_BOOL:
4915 case VAR_SPECIAL:
4916 case VAR_JOB:
4917 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004918 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004919 case VAR_CLASS:
4920 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004921 break; // not evaluating, skipping over subscript
4922
4923 case VAR_NUMBER:
4924 case VAR_STRING:
4925 {
4926 char_u *s = tv_get_string(rettv);
4927
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004928 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004929 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004930 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004931 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004932 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004933 else
4934 s = char_from_string(s, n1);
4935 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004936 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004938 // The resulting variable is a substring. If the indexes
4939 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 if (n1 < 0)
4941 {
4942 n1 = len + n1;
4943 if (n1 < 0)
4944 n1 = 0;
4945 }
4946 if (n2 < 0)
4947 n2 = len + n2;
4948 else if (n2 >= len)
4949 n2 = len;
4950 if (n1 >= len || n2 < 0 || n1 > n2)
4951 s = NULL;
4952 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004953 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004954 }
4955 else
4956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // The resulting variable is a string of a single
4958 // character. If the index is too big or negative the
4959 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 if (n1 >= len || n1 < 0)
4961 s = NULL;
4962 else
4963 s = vim_strnsave(s + n1, 1);
4964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 clear_tv(rettv);
4966 rettv->v_type = VAR_STRING;
4967 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004968 }
4969 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004970
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004971 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004972 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4973 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004974 break;
4975
4976 case VAR_LIST:
4977 if (var1 == NULL)
4978 n1 = 0;
4979 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004980 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004981 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004982 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004983 return FAIL;
4984 break;
4985
4986 case VAR_DICT:
4987 {
4988 dictitem_T *item;
4989 typval_T tmp;
4990
4991 if (key == NULL)
4992 {
4993 key = tv_get_string_chk(var1);
4994 if (key == NULL)
4995 return FAIL;
4996 }
4997
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004998 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004999
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005000 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005001 {
5002 if (verbose)
5003 {
5004 if (keylen > 0)
5005 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005006 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005007 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005008 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005009 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005010
5011 copy_tv(&item->di_tv, &tmp);
5012 clear_tv(rettv);
5013 *rettv = tmp;
5014 }
5015 break;
5016 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005017 return OK;
5018}
5019
5020/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005021 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005022 */
5023 char_u *
5024partial_name(partial_T *pt)
5025{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005026 if (pt != NULL)
5027 {
5028 if (pt->pt_name != NULL)
5029 return pt->pt_name;
5030 if (pt->pt_func != NULL)
5031 return pt->pt_func->uf_name;
5032 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005033 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005034}
5035
Bram Moolenaarddecc252016-04-06 22:59:37 +02005036 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005037partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005038{
5039 int i;
5040
5041 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005042 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005043 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005044 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005045 if (pt->pt_name != NULL)
5046 {
5047 func_unref(pt->pt_name);
5048 vim_free(pt->pt_name);
5049 }
5050 else
5051 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005052
Bram Moolenaar54656012021-06-09 20:50:46 +02005053 // "out_up" is no longer used, decrement refcount on partial that owns it.
5054 partial_unref(pt->pt_outer.out_up_partial);
5055
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005056 // Using pt_outer from another partial.
5057 partial_unref(pt->pt_outer_partial);
5058
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005059 // Decrease the reference count for the context of a closure. If down
5060 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005061 if (pt->pt_funcstack != NULL)
5062 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005063 --pt->pt_funcstack->fs_refcount;
5064 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005065 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005066 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005067 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5068 if (pt->pt_loopvars[i] != NULL)
5069 {
5070 --pt->pt_loopvars[i]->lvs_refcount;
5071 loopvars_check_refcount(pt->pt_loopvars[i]);
5072 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005073
Bram Moolenaarddecc252016-04-06 22:59:37 +02005074 vim_free(pt);
5075}
5076
5077/*
5078 * Unreference a closure: decrement the reference count and free it when it
5079 * becomes zero.
5080 */
5081 void
5082partial_unref(partial_T *pt)
5083{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005084 if (pt == NULL)
5085 return;
5086
5087 int done = FALSE;
5088
5089 if (--pt->pt_refcount <= 0)
5090 partial_free(pt);
5091
5092 // If the reference count goes down to one, the funcstack may be the
5093 // only reference and can be freed if no other partials reference it.
5094 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005095 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005096 // careful: if the funcstack is freed it may contain this partial
5097 // and it gets freed as well
5098 if (pt->pt_funcstack != NULL)
5099 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005100
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005101 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005102 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005103 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005104
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005105 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5106 if (pt->pt_loopvars[depth] != NULL
5107 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005108 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005109 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005110 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005111}
5112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005113/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005114 * Return the next (unique) copy ID.
5115 * Used for serializing nested structures.
5116 */
5117 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005118get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005119{
5120 current_copyID += COPYID_INC;
5121 return current_copyID;
5122}
5123
5124/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005125 * Garbage collection for lists and dictionaries.
5126 *
5127 * We use reference counts to be able to free most items right away when they
5128 * are no longer used. But for composite items it's possible that it becomes
5129 * unused while the reference count is > 0: When there is a recursive
5130 * reference. Example:
5131 * :let l = [1, 2, 3]
5132 * :let d = {9: l}
5133 * :let l[1] = d
5134 *
5135 * Since this is quite unusual we handle this with garbage collection: every
5136 * once in a while find out which lists and dicts are not referenced from any
5137 * variable.
5138 *
5139 * Here is a good reference text about garbage collection (refers to Python
5140 * but it applies to all reference-counting mechanisms):
5141 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005142 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005143
5144/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005145 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005146 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005147 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005148 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005150garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005151{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005152 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005153 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154 buf_T *buf;
5155 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005156 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005157 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005158
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005159 if (!testing)
5160 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005161 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005162 want_garbage_collect = FALSE;
5163 may_garbage_collect = FALSE;
5164 garbage_collect_at_exit = FALSE;
5165 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005166
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005167 // The execution stack can grow big, limit the size.
5168 if (exestack.ga_maxlen - exestack.ga_len > 500)
5169 {
5170 size_t new_len;
5171 char_u *pp;
5172 int n;
5173
5174 // Keep 150% of the current size, with a minimum of the growth size.
5175 n = exestack.ga_len / 2;
5176 if (n < exestack.ga_growsize)
5177 n = exestack.ga_growsize;
5178
5179 // Don't make it bigger though.
5180 if (exestack.ga_len + n < exestack.ga_maxlen)
5181 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005182 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005183 pp = vim_realloc(exestack.ga_data, new_len);
5184 if (pp == NULL)
5185 return FAIL;
5186 exestack.ga_maxlen = exestack.ga_len + n;
5187 exestack.ga_data = pp;
5188 }
5189 }
5190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 // We advance by two because we add one for items referenced through
5192 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005193 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005194
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195 /*
5196 * 1. Go through all accessible variables and mark all lists and dicts
5197 * with copyID.
5198 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // Don't free variables in the previous_funccal list unless they are only
5201 // referenced through previous_funccal. This must be first, because if
5202 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005206 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005208 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005209 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005210 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5211 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005213 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005214 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005215 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5216 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005217 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005218 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005219 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005220 abort = abort || set_ref_in_item(
5221 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005222#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005223 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005224 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5225 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005226 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005227 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005228 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5229 NULL, NULL);
5230#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005233 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005234 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5235 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005237 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005240 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005242 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005243 abort = abort || set_ref_in_functions(copyID);
5244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005245 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005247
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005248 // funcstacks keep variables for closures
5249 abort = abort || set_ref_in_funcstacks(copyID);
5250
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005251 // loopvars keep variables for loop blocks
5252 abort = abort || set_ref_in_loopvars(copyID);
5253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005255 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005256
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005257 // callbacks in buffers
5258 abort = abort || set_ref_in_buffers(copyID);
5259
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005260 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5261 abort = abort || set_ref_in_insexpand_funcs(copyID);
5262
5263 // 'operatorfunc' callback
5264 abort = abort || set_ref_in_opfunc(copyID);
5265
5266 // 'tagfunc' callback
5267 abort = abort || set_ref_in_tagfunc(copyID);
5268
5269 // 'imactivatefunc' and 'imstatusfunc' callbacks
5270 abort = abort || set_ref_in_im_funcs(copyID);
5271
Bram Moolenaar1dced572012-04-05 16:54:08 +02005272#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005274#endif
5275
Bram Moolenaardb913952012-06-29 12:54:53 +02005276#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005278#endif
5279
5280#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005282#endif
5283
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005284#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005285 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005286 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005287#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005288#ifdef FEAT_NETBEANS_INTG
5289 abort = abort || set_ref_in_nb_channel(copyID);
5290#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005291
Bram Moolenaare3188e22016-05-31 21:13:04 +02005292#ifdef FEAT_TIMERS
5293 abort = abort || set_ref_in_timer(copyID);
5294#endif
5295
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005296#ifdef FEAT_QUICKFIX
5297 abort = abort || set_ref_in_quickfix(copyID);
5298#endif
5299
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005300#ifdef FEAT_TERMINAL
5301 abort = abort || set_ref_in_term(copyID);
5302#endif
5303
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005304#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005305 abort = abort || set_ref_in_popups(copyID);
5306#endif
5307
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005308 abort = abort || set_ref_in_classes(copyID);
5309
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005310 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005311 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005312 /*
5313 * 2. Free lists and dictionaries that are not referenced.
5314 */
5315 did_free = free_unref_items(copyID);
5316
5317 /*
5318 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005320 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005321 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005322 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005323 else if (p_verbose > 0)
5324 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005325 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005326 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005327
5328 return did_free;
5329}
5330
5331/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005332 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005333 */
5334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005335free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005336{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005337 int did_free = FALSE;
5338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005339 // Let all "free" functions know that we are here. This means no
5340 // dictionaries, lists, channels or jobs are to be freed, because we will
5341 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005342 in_free_unref_items = TRUE;
5343
5344 /*
5345 * PASS 1: free the contents of the items. We don't free the items
5346 * themselves yet, so that it is possible to decrement refcount counters
5347 */
5348
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005349 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005350 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005351
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005352 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005353 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005354
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005355 // Go through the list of objects and free items without this copyID.
5356 did_free |= object_free_nonref(copyID);
5357
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005358 // Go through the list of classes and free items without this copyID.
5359 did_free |= class_free_nonref(copyID);
5360
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 // Go through the list of jobs and free items without the copyID. This
5363 // must happen before doing channels, because jobs refer to channels, but
5364 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005365 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005368 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5369#endif
5370
5371 /*
5372 * PASS 2: free the items themselves.
5373 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005374 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005375 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 // Go through the list of jobs and free items without the copyID. This
5379 // must happen before doing channels, because jobs refer to channels, but
5380 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005381 free_unused_jobs(copyID, COPYID_MASK);
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005384 free_unused_channels(copyID, COPYID_MASK);
5385#endif
5386
5387 in_free_unref_items = FALSE;
5388
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 return did_free;
5390}
5391
5392/*
5393 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 * "list_stack" is used to add lists to be marked. Can be NULL.
5395 *
5396 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400{
5401 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005402 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005403 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005404 hashtab_T *cur_ht;
5405 ht_stack_T *ht_stack = NULL;
5406 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005407
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005408 cur_ht = ht;
5409 for (;;)
5410 {
5411 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // Mark each item in the hashtab. If the item contains a hashtab
5414 // it is added to ht_stack, if it contains a list it is added to
5415 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005416 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005417 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 if (!HASHITEM_EMPTY(hi))
5419 {
5420 --todo;
5421 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5422 &ht_stack, list_stack);
5423 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005424 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005425
5426 if (ht_stack == NULL)
5427 break;
5428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005430 cur_ht = ht_stack->ht;
5431 tempitem = ht_stack;
5432 ht_stack = ht_stack->prev;
5433 free(tempitem);
5434 }
5435
5436 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005437}
5438
Dominique Pelle748b3082022-01-08 12:41:16 +00005439#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5440 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005442 * Mark a dict and its items with "copyID".
5443 * Returns TRUE if setting references failed somehow.
5444 */
5445 int
5446set_ref_in_dict(dict_T *d, int copyID)
5447{
5448 if (d != NULL && d->dv_copyID != copyID)
5449 {
5450 d->dv_copyID = copyID;
5451 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5452 }
5453 return FALSE;
5454}
Dominique Pelle748b3082022-01-08 12:41:16 +00005455#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005456
5457/*
5458 * Mark a list and its items with "copyID".
5459 * Returns TRUE if setting references failed somehow.
5460 */
5461 int
5462set_ref_in_list(list_T *ll, int copyID)
5463{
5464 if (ll != NULL && ll->lv_copyID != copyID)
5465 {
5466 ll->lv_copyID = copyID;
5467 return set_ref_in_list_items(ll, copyID, NULL);
5468 }
5469 return FALSE;
5470}
5471
5472/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005473 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005474 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5475 *
5476 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005479set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005480{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005481 listitem_T *li;
5482 int abort = FALSE;
5483 list_T *cur_l;
5484 list_stack_T *list_stack = NULL;
5485 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005486
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 cur_l = l;
5488 for (;;)
5489 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005490 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // Mark each item in the list. If the item contains a hashtab
5492 // it is added to ht_stack, if it contains a list it is added to
5493 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5495 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5496 ht_stack, &list_stack);
5497 if (list_stack == NULL)
5498 break;
5499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 cur_l = list_stack->list;
5502 tempitem = list_stack;
5503 list_stack = list_stack->prev;
5504 free(tempitem);
5505 }
5506
5507 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005508}
5509
5510/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005511 * Mark the partial in callback 'cb' with "copyID".
5512 */
5513 int
5514set_ref_in_callback(callback_T *cb, int copyID)
5515{
5516 typval_T tv;
5517
5518 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5519 return FALSE;
5520
5521 tv.v_type = VAR_PARTIAL;
5522 tv.vval.v_partial = cb->cb_partial;
5523 return set_ref_in_item(&tv, copyID, NULL, NULL);
5524}
5525
5526/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005527 * Mark the dict "dd" with "copyID".
5528 * Also see set_ref_in_item().
5529 */
5530 static int
5531set_ref_in_item_dict(
5532 dict_T *dd,
5533 int copyID,
5534 ht_stack_T **ht_stack,
5535 list_stack_T **list_stack)
5536{
5537 if (dd == NULL || dd->dv_copyID == copyID)
5538 return FALSE;
5539
5540 // Didn't see this dict yet.
5541 dd->dv_copyID = copyID;
5542 if (ht_stack == NULL)
5543 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5544
5545 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5546 if (newitem == NULL)
5547 return TRUE;
5548
5549 newitem->ht = &dd->dv_hashtab;
5550 newitem->prev = *ht_stack;
5551 *ht_stack = newitem;
5552
5553 return FALSE;
5554}
5555
5556/*
5557 * Mark the list "ll" with "copyID".
5558 * Also see set_ref_in_item().
5559 */
5560 static int
5561set_ref_in_item_list(
5562 list_T *ll,
5563 int copyID,
5564 ht_stack_T **ht_stack,
5565 list_stack_T **list_stack)
5566{
5567 if (ll == NULL || ll->lv_copyID == copyID)
5568 return FALSE;
5569
5570 // Didn't see this list yet.
5571 ll->lv_copyID = copyID;
5572 if (list_stack == NULL)
5573 return set_ref_in_list_items(ll, copyID, ht_stack);
5574
5575 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5576 if (newitem == NULL)
5577 return TRUE;
5578
5579 newitem->list = ll;
5580 newitem->prev = *list_stack;
5581 *list_stack = newitem;
5582
5583 return FALSE;
5584}
5585
5586/*
5587 * Mark the partial "pt" with "copyID".
5588 * Also see set_ref_in_item().
5589 */
5590 static int
5591set_ref_in_item_partial(
5592 partial_T *pt,
5593 int copyID,
5594 ht_stack_T **ht_stack,
5595 list_stack_T **list_stack)
5596{
5597 if (pt == NULL || pt->pt_copyID == copyID)
5598 return FALSE;
5599
5600 // Didn't see this partial yet.
5601 pt->pt_copyID = copyID;
5602
5603 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5604
5605 if (pt->pt_dict != NULL)
5606 {
5607 typval_T dtv;
5608
5609 dtv.v_type = VAR_DICT;
5610 dtv.vval.v_dict = pt->pt_dict;
5611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5612 }
5613
5614 for (int i = 0; i < pt->pt_argc; ++i)
5615 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5616 ht_stack, list_stack);
5617 // pt_funcstack is handled in set_ref_in_funcstacks()
5618 // pt_loopvars is handled in set_ref_in_loopvars()
5619
5620 return abort;
5621}
5622
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005623#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005624/*
5625 * Mark the job "pt" with "copyID".
5626 * Also see set_ref_in_item().
5627 */
5628 static int
5629set_ref_in_item_job(
5630 job_T *job,
5631 int copyID,
5632 ht_stack_T **ht_stack,
5633 list_stack_T **list_stack)
5634{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005635 typval_T dtv;
5636
5637 if (job == NULL || job->jv_copyID == copyID)
5638 return FALSE;
5639
5640 job->jv_copyID = copyID;
5641 if (job->jv_channel != NULL)
5642 {
5643 dtv.v_type = VAR_CHANNEL;
5644 dtv.vval.v_channel = job->jv_channel;
5645 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5646 }
5647 if (job->jv_exit_cb.cb_partial != NULL)
5648 {
5649 dtv.v_type = VAR_PARTIAL;
5650 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5651 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5652 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005653
5654 return FALSE;
5655}
5656
5657/*
5658 * Mark the channel "ch" with "copyID".
5659 * Also see set_ref_in_item().
5660 */
5661 static int
5662set_ref_in_item_channel(
5663 channel_T *ch,
5664 int copyID,
5665 ht_stack_T **ht_stack,
5666 list_stack_T **list_stack)
5667{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005668 typval_T dtv;
5669
5670 if (ch == NULL || ch->ch_copyID == copyID)
5671 return FALSE;
5672
5673 ch->ch_copyID = copyID;
5674 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5675 {
5676 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5677 jq != NULL; jq = jq->jq_next)
5678 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5679 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5680 cq = cq->cq_next)
5681 if (cq->cq_callback.cb_partial != NULL)
5682 {
5683 dtv.v_type = VAR_PARTIAL;
5684 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5685 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5686 }
5687 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5688 {
5689 dtv.v_type = VAR_PARTIAL;
5690 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5691 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5692 }
5693 }
5694 if (ch->ch_callback.cb_partial != NULL)
5695 {
5696 dtv.v_type = VAR_PARTIAL;
5697 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5698 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5699 }
5700 if (ch->ch_close_cb.cb_partial != NULL)
5701 {
5702 dtv.v_type = VAR_PARTIAL;
5703 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5704 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5705 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005706
5707 return FALSE;
5708}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005709#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005710
5711/*
5712 * Mark the class "cl" with "copyID".
5713 * Also see set_ref_in_item().
5714 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005715 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005716set_ref_in_item_class(
5717 class_T *cl,
5718 int copyID,
5719 ht_stack_T **ht_stack,
5720 list_stack_T **list_stack)
5721{
5722 int abort = FALSE;
5723
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005724 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005725 return FALSE;
5726
5727 cl->class_copyID = copyID;
5728 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5729 abort = abort || set_ref_in_item(
5730 &cl->class_members_tv[i],
5731 copyID, ht_stack, list_stack);
5732
5733 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5734 abort = abort || set_ref_in_func(NULL,
5735 cl->class_class_functions[i], copyID);
5736
5737 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5738 abort = abort || set_ref_in_func(NULL,
5739 cl->class_obj_methods[i], copyID);
5740
5741 return abort;
5742}
5743
5744/*
5745 * Mark the object "cl" with "copyID".
5746 * Also see set_ref_in_item().
5747 */
5748 static int
5749set_ref_in_item_object(
5750 object_T *obj,
5751 int copyID,
5752 ht_stack_T **ht_stack,
5753 list_stack_T **list_stack)
5754{
5755 int abort = FALSE;
5756
5757 if (obj == NULL || obj->obj_copyID == copyID)
5758 return FALSE;
5759
5760 obj->obj_copyID = copyID;
5761
5762 // The typval_T array is right after the object_T.
5763 typval_T *mtv = (typval_T *)(obj + 1);
5764 for (int i = 0; !abort
5765 && i < obj->obj_class->class_obj_member_count; ++i)
5766 abort = abort || set_ref_in_item(mtv + i, copyID,
5767 ht_stack, list_stack);
5768
5769 return abort;
5770}
5771
5772/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005773 * Mark all lists, dicts and other container types referenced through typval
5774 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005775 * "list_stack" is used to add lists to be marked. Can be NULL.
5776 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5777 *
5778 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781set_ref_in_item(
5782 typval_T *tv,
5783 int copyID,
5784 ht_stack_T **ht_stack,
5785 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005786{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005787 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005788
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005789 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005790 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005791 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005792 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5793 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005794
5795 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005796 return set_ref_in_item_list(tv->vval.v_list, copyID,
5797 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005798
5799 case VAR_FUNC:
5800 {
5801 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5802 break;
5803 }
5804
5805 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005806 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5807 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005808
5809 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005810#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005811 return set_ref_in_item_job(tv->vval.v_job, copyID,
5812 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005813#else
5814 break;
5815#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005816
5817 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005818#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005819 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005820 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005821#else
5822 break;
5823#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005824
5825 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005826 return set_ref_in_item_class(tv->vval.v_class, copyID,
5827 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005828
5829 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005830 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005831 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005832
5833 case VAR_UNKNOWN:
5834 case VAR_ANY:
5835 case VAR_VOID:
5836 case VAR_BOOL:
5837 case VAR_SPECIAL:
5838 case VAR_NUMBER:
5839 case VAR_FLOAT:
5840 case VAR_STRING:
5841 case VAR_BLOB:
5842 case VAR_INSTR:
5843 // Types that do not contain any other item
5844 break;
5845 }
5846
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005847 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005848}
5849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 * Return a string with the string representation of a variable.
5852 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005853 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005854 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005855 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005856 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005857 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005858 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5859 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005860 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005862 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005863echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864 typval_T *tv,
5865 char_u **tofree,
5866 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005867 int copyID,
5868 int echo_style,
5869 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005870 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 static int recurse = 0;
5873 char_u *r = NULL;
5874
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005877 if (!did_echo_string_emsg)
5878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 // Only give this message once for a recursive call to avoid
5880 // flooding the user with errors. And stop iterating over lists
5881 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005882 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005883 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005884 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005885 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005886 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887 }
5888 ++recurse;
5889
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 switch (tv->v_type)
5891 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005892 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005893 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005894 {
5895 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005896 r = tv->vval.v_string;
5897 if (r == NULL)
5898 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005899 }
5900 else
5901 {
5902 *tofree = string_quote(tv->vval.v_string, FALSE);
5903 r = *tofree;
5904 }
5905 break;
5906
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005908 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005909 char_u buf[MAX_FUNC_NAME_LEN];
5910
5911 if (echo_style)
5912 {
LemonBoya5d35902022-04-29 21:15:02 +01005913 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5914 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005915 buf, MAX_FUNC_NAME_LEN);
5916 if (r == buf)
5917 {
5918 r = vim_strsave(buf);
5919 *tofree = r;
5920 }
5921 else
5922 *tofree = NULL;
5923 }
5924 else
5925 {
5926 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5927 : make_ufunc_name_readable(
5928 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5929 TRUE);
5930 r = *tofree;
5931 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005934
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005935 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005936 {
5937 partial_T *pt = tv->vval.v_partial;
5938 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005939 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005940 garray_T ga;
5941 int i;
5942 char_u *tf;
5943
5944 ga_init2(&ga, 1, 100);
5945 ga_concat(&ga, (char_u *)"function(");
5946 if (fname != NULL)
5947 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005948 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005949 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005950 && vim_isupper(fname[1]))
5951 {
5952 ga_concat(&ga, (char_u *)"'g:");
5953 ga_concat(&ga, fname + 1);
5954 }
5955 else
5956 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005957 vim_free(fname);
5958 }
5959 if (pt != NULL && pt->pt_argc > 0)
5960 {
5961 ga_concat(&ga, (char_u *)", [");
5962 for (i = 0; i < pt->pt_argc; ++i)
5963 {
5964 if (i > 0)
5965 ga_concat(&ga, (char_u *)", ");
5966 ga_concat(&ga,
5967 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5968 vim_free(tf);
5969 }
5970 ga_concat(&ga, (char_u *)"]");
5971 }
5972 if (pt != NULL && pt->pt_dict != NULL)
5973 {
5974 typval_T dtv;
5975
5976 ga_concat(&ga, (char_u *)", ");
5977 dtv.v_type = VAR_DICT;
5978 dtv.vval.v_dict = pt->pt_dict;
5979 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5980 vim_free(tf);
5981 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005982 // terminate with ')' and a NUL
5983 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005984
5985 *tofree = ga.ga_data;
5986 r = *tofree;
5987 break;
5988 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005989
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005990 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005991 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005992 break;
5993
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005995 if (tv->vval.v_list == NULL)
5996 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005997 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005999 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006000 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006001 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6002 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006003 {
6004 *tofree = NULL;
6005 r = (char_u *)"[...]";
6006 }
6007 else
6008 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006009 int old_copyID = tv->vval.v_list->lv_copyID;
6010
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006012 *tofree = list2string(tv, copyID, restore_copyID);
6013 if (restore_copyID)
6014 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015 r = *tofree;
6016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006017 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006018
Bram Moolenaar8c711452005-01-14 21:53:12 +00006019 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006020 if (tv->vval.v_dict == NULL)
6021 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006022 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006023 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006024 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006026 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6027 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006028 {
6029 *tofree = NULL;
6030 r = (char_u *)"{...}";
6031 }
6032 else
6033 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006034 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006035
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006037 *tofree = dict2string(tv, copyID, restore_copyID);
6038 if (restore_copyID)
6039 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006040 r = *tofree;
6041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006042 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006044 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006045 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006046 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006048 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006050 break;
6051
Bram Moolenaar835dc632016-02-07 14:27:38 +01006052 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006053 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006054#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006056 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6057 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006058 if (composite_val)
6059 {
6060 *tofree = string_quote(r, FALSE);
6061 r = *tofree;
6062 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006063#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006065
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006066 case VAR_INSTR:
6067 *tofree = NULL;
6068 r = (char_u *)"instructions";
6069 break;
6070
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006071 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006072 {
6073 class_T *cl = tv->vval.v_class;
6074 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6075 r = *tofree = alloc(len);
6076 vim_snprintf((char *)r, len, "class %s",
6077 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6078 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006079 break;
6080
6081 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006082 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006083 garray_T ga;
6084 ga_init2(&ga, 1, 50);
6085 ga_concat(&ga, (char_u *)"object of ");
6086 object_T *obj = tv->vval.v_object;
6087 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6088 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6089 : cl->class_name);
6090 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006091 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006092 ga_concat(&ga, (char_u *)" {");
6093 for (int i = 0; i < cl->class_obj_member_count; ++i)
6094 {
6095 if (i > 0)
6096 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006097 ocmember_T *m = &cl->class_obj_members[i];
6098 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006099 ga_concat(&ga, (char_u *)": ");
6100 char_u *tf = NULL;
6101 ga_concat(&ga, echo_string_core(
6102 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006103 &tf, numbuf, copyID, echo_style,
6104 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006105 vim_free(tf);
6106 }
6107 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006108 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006109
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006110 *tofree = r = ga.ga_data;
6111 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006112 break;
6113
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006114 case VAR_FLOAT:
6115 *tofree = NULL;
6116 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6117 r = numbuf;
6118 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006119
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006120 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006121 case VAR_SPECIAL:
6122 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006123 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006124 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006125 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006126
Bram Moolenaar8502c702014-06-17 12:51:16 +02006127 if (--recurse == 0)
6128 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006130}
6131
6132/*
6133 * Return a string with the string representation of a variable.
6134 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6135 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006136 * Does not put quotes around strings, as ":echo" displays values.
6137 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6138 * May return NULL.
6139 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006140 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006141echo_string(
6142 typval_T *tv,
6143 char_u **tofree,
6144 char_u *numbuf,
6145 int copyID)
6146{
6147 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6148}
6149
6150/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006151 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6152 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006153 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006154 */
6155 int
6156buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6157{
6158 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006159 char_u *t;
6160 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006161
6162 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6163 return -1;
6164
6165 if (lnum > buf->b_ml.ml_line_count)
6166 lnum = buf->b_ml.ml_line_count;
6167
6168 str = ml_get_buf(buf, lnum, FALSE);
6169 if (str == NULL)
6170 return -1;
6171
6172 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006173 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006174
Bram Moolenaar91458462021-01-13 20:08:38 +01006175 // count the number of characters
6176 t = str;
6177 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6178 t += mb_ptr2len(t);
6179
6180 // In insert mode, when the cursor is at the end of a non-empty line,
6181 // byteidx points to the NUL character immediately past the end of the
6182 // string. In this case, add one to the character count.
6183 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6184 count++;
6185
6186 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006187}
6188
6189/*
6190 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006191 * byte index. Works only for loaded buffers. Returns -1 on failure.
6192 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006193 */
6194 int
6195buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6196{
6197 char_u *str;
6198 char_u *t;
6199
6200 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6201 return -1;
6202
6203 if (lnum > buf->b_ml.ml_line_count)
6204 lnum = buf->b_ml.ml_line_count;
6205
6206 str = ml_get_buf(buf, lnum, FALSE);
6207 if (str == NULL)
6208 return -1;
6209
6210 // Convert the character offset to a byte offset
6211 t = str;
6212 while (*t != NUL && --charidx > 0)
6213 t += mb_ptr2len(t);
6214
Bram Moolenaar91458462021-01-13 20:08:38 +01006215 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006216}
6217
6218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006220 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223var2fpos(
6224 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006226 int *fnum, // set to fnum for '0, 'A, etc.
6227 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006229 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006231 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006234 if (varp->v_type == VAR_LIST)
6235 {
6236 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006237 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006238 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006239 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006240
6241 l = varp->vval.v_list;
6242 if (l == NULL)
6243 return NULL;
6244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006245 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006246 pos.lnum = list_find_nr(l, 0L, &error);
6247 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006248 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006249 if (charcol)
6250 len = (long)mb_charlen(ml_get(pos.lnum));
6251 else
6252 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006253
Bram Moolenaarec65d772020-08-20 22:29:12 +02006254 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006255 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006256 li = list_find(l, 1L);
6257 if (li != NULL && li->li_tv.v_type == VAR_STRING
6258 && li->li_tv.vval.v_string != NULL
6259 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006260 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006261 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006262 }
6263 else
6264 {
6265 pos.col = list_find_nr(l, 1L, &error);
6266 if (error)
6267 return NULL;
6268 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006270 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006271 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006272 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006273 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006276 pos.coladd = list_find_nr(l, 2L, &error);
6277 if (error)
6278 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006279
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006280 return &pos;
6281 }
6282
Bram Moolenaarc5809432021-03-27 21:23:30 +01006283 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6284 return NULL;
6285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006286 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006287 if (name == NULL)
6288 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006289
6290 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006291 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006292 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006293 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006294 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006295 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006296 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006297 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006298 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006299 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006300 pos = VIsual;
6301 else
6302 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006303 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006304 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006305 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006307 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006308 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6310 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006311 pos = *pp;
6312 }
6313 if (pos.lnum != 0)
6314 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006315 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006316 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6317 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006319
Bram Moolenaara5525202006-03-02 22:52:09 +00006320 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006321
Bram Moolenaar477933c2007-07-17 14:32:23 +00006322 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006323 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006324 // the "w_valid" flags are not reset when moving the cursor, but they
6325 // do matter for update_topline() and validate_botline().
6326 check_cursor_moved(curwin);
6327
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006328 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006329 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006330 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006331 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006332 // In silent Ex mode topline is zero, but that's not a valid line
6333 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006334 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006335 return &pos;
6336 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006337 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006338 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006339 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006340 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006341 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006342 return &pos;
6343 }
6344 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006345 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006347 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
6349 pos.lnum = curbuf->b_ml.ml_line_count;
6350 pos.col = 0;
6351 }
6352 else
6353 {
6354 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006355 if (charcol)
6356 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6357 else
6358 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360 return &pos;
6361 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006362 if (in_vim9script())
6363 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 return NULL;
6365}
6366
6367/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006368 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006369 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006370 * Note that the column is passed on as-is, the caller may want to decrement
6371 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006372 * If "charcol" is TRUE use the column as the character index instead of the
6373 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006374 * Return FAIL when conversion is not possible, doesn't check the position for
6375 * validity.
6376 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list2fpos(
6379 typval_T *arg,
6380 pos_T *posp,
6381 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006382 colnr_T *curswantp,
6383 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006384{
6385 list_T *l = arg->vval.v_list;
6386 long i = 0;
6387 long n;
6388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006389 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6390 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006391 if (arg->v_type != VAR_LIST
6392 || l == NULL
6393 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006394 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006395 return FAIL;
6396
6397 if (fnump != NULL)
6398 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006399 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006400 if (n < 0)
6401 return FAIL;
6402 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006403 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006404 *fnump = n;
6405 }
6406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006407 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006408 if (n < 0)
6409 return FAIL;
6410 posp->lnum = n;
6411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006412 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006413 if (n < 0)
6414 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006415 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006416 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006417 if (charcol)
6418 {
6419 buf_T *buf;
6420
6421 // Get the text for the specified line in a loaded buffer
6422 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6423 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6424 return FAIL;
6425
Bram Moolenaar79f23442022-10-10 12:42:57 +01006426 n = buf_charidx_to_byteidx(buf,
6427 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006428 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006429 posp->col = n;
6430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006432 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006433 posp->coladd = 0;
6434 else
6435 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006436
Bram Moolenaar493c1782014-05-28 14:34:46 +02006437 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006438 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006439
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006440 return OK;
6441}
6442
6443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 * Get the length of an environment variable name.
6445 * Advance "arg" to the first character after the name.
6446 * Return 0 for error.
6447 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 char_u *p;
6452 int len;
6453
6454 for (p = *arg; vim_isIDc(*p); ++p)
6455 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 return 0;
6458
6459 len = (int)(p - *arg);
6460 *arg = p;
6461 return len;
6462}
6463
6464/*
6465 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006466 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 * Return 0 if something is wrong.
6468 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006470get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 char_u *p;
6473 int len;
6474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006477 {
6478 if (*p == ':')
6479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006480 // "s:" is start of "s:var", but "n:" is not and can be used in
6481 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006482 len = (int)(p - *arg);
6483 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6484 || len > 1)
6485 break;
6486 }
6487 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 return 0;
6490
6491 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006492 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 return len;
6495}
6496
6497/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006498 * Get the length of the name of a variable or function.
6499 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006501 * Return -1 if curly braces expansion failed.
6502 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 * If the name contains 'magic' {}'s, expand them and return the
6504 * expanded name in an allocated string via 'alias' - caller must free.
6505 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006507get_name_len(
6508 char_u **arg,
6509 char_u **alias,
6510 int evaluate,
6511 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 char_u *p;
6515 char_u *expr_start;
6516 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
6520 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6521 && (*arg)[2] == (int)KE_SNR)
6522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006523 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 *arg += 3;
6525 return get_id_len(arg) + 3;
6526 }
6527 len = eval_fname_script(*arg);
6528 if (len > 0)
6529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006530 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 *arg += len;
6532 }
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006537 p = find_name_end(*arg, &expr_start, &expr_end,
6538 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (expr_start != NULL)
6540 {
6541 char_u *temp_string;
6542
6543 if (!evaluate)
6544 {
6545 len += (int)(p - *arg);
6546 *arg = skipwhite(p);
6547 return len;
6548 }
6549
6550 /*
6551 * Include any <SID> etc in the expanded string:
6552 * Thus the -len here.
6553 */
6554 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6555 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006556 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 *alias = temp_string;
6558 *arg = skipwhite(p);
6559 return (int)STRLEN(temp_string);
6560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
6562 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006563 // Only give an error when there is something, otherwise it will be
6564 // reported at a higher level.
6565 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006566 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 return len;
6569}
6570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006571/*
6572 * Find the end of a variable or function name, taking care of magic braces.
6573 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6574 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006575 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 * Return a pointer to just after the name. Equal to "arg" if there is no
6577 * valid name.
6578 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580find_name_end(
6581 char_u *arg,
6582 char_u **expr_start,
6583 char_u **expr_end,
6584 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 int mb_nest = 0;
6587 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006589 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006590 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006592 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006594 *expr_start = NULL;
6595 *expr_end = NULL;
6596 }
6597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006599 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006600 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006601 return arg;
6602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006603 for (p = arg; *p != NUL
6604 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006605 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006606 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006607 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006608 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006609 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006611 if (*p == '\'')
6612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006613 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006614 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006615 ;
6616 if (*p == NUL)
6617 break;
6618 }
6619 else if (*p == '"')
6620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006621 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006622 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006623 if (*p == '\\' && p[1] != NUL)
6624 ++p;
6625 if (*p == NUL)
6626 break;
6627 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006628 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006630 // "s:" is start of "s:var", but "n:" is not and can be used in
6631 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006632 len = (int)(p - arg);
6633 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006634 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006635 break;
6636 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006640 if (*p == '[')
6641 ++br_nest;
6642 else if (*p == ']')
6643 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006645
zeertzjqa93d9cd2023-05-02 16:25:47 +01006646 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 if (*p == '{')
6649 {
6650 mb_nest++;
6651 if (expr_start != NULL && *expr_start == NULL)
6652 *expr_start = p;
6653 }
6654 else if (*p == '}')
6655 {
6656 mb_nest--;
6657 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6658 *expr_end = p;
6659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662
6663 return p;
6664}
6665
6666/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006667 * Expands out the 'magic' {}'s in a variable/function name.
6668 * Note that this can call itself recursively, to deal with
6669 * constructs like foo{bar}{baz}{bam}
6670 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6671 * "in_start" ^
6672 * "expr_start" ^
6673 * "expr_end" ^
6674 * "in_end" ^
6675 *
6676 * Returns a new allocated string, which the caller must free.
6677 * Returns NULL for failure.
6678 */
6679 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006680make_expanded_name(
6681 char_u *in_start,
6682 char_u *expr_start,
6683 char_u *expr_end,
6684 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006685{
6686 char_u c1;
6687 char_u *retval = NULL;
6688 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006689
6690 if (expr_end == NULL || in_end == NULL)
6691 return NULL;
6692 *expr_start = NUL;
6693 *expr_end = NUL;
6694 c1 = *in_end;
6695 *in_end = NUL;
6696
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006697 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006698 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006699 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006700 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6701 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006702 if (retval != NULL)
6703 {
6704 STRCPY(retval, in_start);
6705 STRCAT(retval, temp_result);
6706 STRCAT(retval, expr_end + 1);
6707 }
6708 }
6709 vim_free(temp_result);
6710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006712 *expr_start = '{';
6713 *expr_end = '}';
6714
6715 if (retval != NULL)
6716 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006717 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006718 if (expr_start != NULL)
6719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006720 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006721 temp_result = make_expanded_name(retval, expr_start,
6722 expr_end, temp_result);
6723 vim_free(retval);
6724 retval = temp_result;
6725 }
6726 }
6727
6728 return retval;
6729}
6730
6731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006733 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006736eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006738 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006739}
6740
6741/*
6742 * Return TRUE if character "c" can be used as the first character in a
6743 * variable or function name (excluding '{' and '}').
6744 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006746eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006747{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006748 return ASCII_ISALPHA(c) || c == '_';
6749}
6750
6751/*
6752 * Return TRUE if character "c" can be used as the first character of a
6753 * dictionary key.
6754 */
6755 int
6756eval_isdictc(int c)
6757{
6758 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
6760
6761/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006762 * Handle:
6763 * - expr[expr], expr[expr:expr] subscript
6764 * - ".name" lookup
6765 * - function call with Funcref variable: func(expr)
6766 * - method call: var->method()
6767 *
6768 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006769 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006770 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006771 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006772handle_subscript(
6773 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006774 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006775 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006776 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006777 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006778{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006779 int evaluate = evalarg != NULL
6780 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006781 int ret = OK;
6782 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006783 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006784 int getnext;
6785 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006786
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006787 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006788 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006789 // When at the end of the line and ".name" or "->{" or "->X" follows in
6790 // the next line then consume the line break.
6791 p = eval_next_non_blank(*arg, evalarg, &getnext);
6792 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006793 && ((*p == '.'
6794 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6795 || rettv->v_type == VAR_CLASS
6796 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006797 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6798 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6799 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006800 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006801 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006802 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006803 check_white = FALSE;
6804 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006805
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006806 if (rettv->v_type == VAR_ANY)
6807 {
6808 char_u *exp_name;
6809 int cc;
6810 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006811 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006812 type_T *type;
6813
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006814 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006815 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006816 if (**arg != '.')
6817 {
6818 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006819 semsg(_(e_expected_dot_after_name_str),
6820 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006821 ret = FAIL;
6822 break;
6823 }
6824 ++*arg;
6825 if (IS_WHITE_OR_NUL(**arg))
6826 {
6827 if (verbose)
6828 emsg(_(e_no_white_space_allowed_after_dot));
6829 ret = FAIL;
6830 break;
6831 }
6832
6833 // isolate the name
6834 exp_name = *arg;
6835 while (eval_isnamec(**arg))
6836 ++*arg;
6837 cc = **arg;
6838 **arg = NUL;
6839
6840 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006841 evalarg == NULL ? NULL : evalarg->eval_cctx,
6842 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006843 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006844
6845 if (idx < 0 && ufunc == NULL)
6846 {
6847 ret = FAIL;
6848 break;
6849 }
6850 if (idx >= 0)
6851 {
6852 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6853 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6854
6855 copy_tv(sv->sv_tv, rettv);
6856 }
6857 else
6858 {
6859 rettv->v_type = VAR_FUNC;
6860 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6861 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006862 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006863 }
6864
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006865 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6866 || rettv->v_type == VAR_PARTIAL))
6867 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006869 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6870 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006871
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006872 // Stop the expression evaluation when immediately aborting on
6873 // error, or when an interrupt occurred or an exception was thrown
6874 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006875 if (aborting())
6876 {
6877 if (ret == OK)
6878 clear_tv(rettv);
6879 ret = FAIL;
6880 }
6881 dict_unref(selfdict);
6882 selfdict = NULL;
6883 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006884 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006885 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006886 if (in_vim9script())
6887 *arg = skipwhite(p + 2);
6888 else
6889 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006890 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006891 {
zeertzjqc481ad32023-03-11 16:18:51 +00006892 emsg(_(e_no_white_space_allowed_before_parenthesis));
6893 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006894 }
zeertzjqc481ad32023-03-11 16:18:51 +00006895 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6896 // expr->{lambda}() or expr->(lambda)()
6897 ret = eval_lambda(arg, rettv, evalarg, verbose);
6898 else
6899 // expr->name()
6900 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006901 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006902 // "." is ".name" lookup when we found a dict or when evaluating and
6903 // scriptversion is at least 2, where string concatenation is "..".
6904 else if (**arg == '['
6905 || (**arg == '.' && (rettv->v_type == VAR_DICT
6906 || (!evaluate
6907 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006908 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006909 {
6910 dict_unref(selfdict);
6911 if (rettv->v_type == VAR_DICT)
6912 {
6913 selfdict = rettv->vval.v_dict;
6914 if (selfdict != NULL)
6915 ++selfdict->dv_refcount;
6916 }
6917 else
6918 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006919 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006920 {
6921 clear_tv(rettv);
6922 ret = FAIL;
6923 }
6924 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006925 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6926 || rettv->v_type == VAR_OBJECT))
6927 {
6928 // class member: SomeClass.varname
6929 // class method: SomeClass.SomeMethod()
6930 // class constructor: SomeClass.new()
6931 // object member: someObject.varname
6932 // object method: someObject.SomeMethod()
6933 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6934 {
6935 clear_tv(rettv);
6936 ret = FAIL;
6937 }
6938 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006939 else
6940 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006941 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6944 // Don't do this when "Func" is already a partial that was bound
6945 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006946 if (selfdict != NULL
6947 && (rettv->v_type == VAR_FUNC
6948 || (rettv->v_type == VAR_PARTIAL
6949 && (rettv->vval.v_partial->pt_auto
6950 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006951 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006952
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006953 dict_unref(selfdict);
6954 return ret;
6955}
6956
6957/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006958 * Make a copy of an item.
6959 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006960 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006961 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6962 * reference to an already copied list/dict can be used.
6963 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006966item_copy(
6967 typval_T *from,
6968 typval_T *to,
6969 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006970 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972{
6973 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006974 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006978 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006979 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 }
6981 ++recurse;
6982
6983 switch (from->v_type)
6984 {
6985 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006986 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006987 case VAR_STRING:
6988 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006989 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006990 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006991 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006992 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006993 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006994 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006995 case VAR_CLASS:
6996 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 copy_tv(from, to);
6998 break;
6999 case VAR_LIST:
7000 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007001 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 if (from->vval.v_list == NULL)
7003 to->vval.v_list = NULL;
7004 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7005 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007006 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007007 to->vval.v_list = from->vval.v_list->lv_copylist;
7008 ++to->vval.v_list->lv_refcount;
7009 }
7010 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007011 to->vval.v_list = list_copy(from->vval.v_list,
7012 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007013 if (to->vval.v_list == NULL)
7014 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007016 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007018 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019 case VAR_DICT:
7020 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007021 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007022 if (from->vval.v_dict == NULL)
7023 to->vval.v_dict = NULL;
7024 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007027 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7028 ++to->vval.v_dict->dv_refcount;
7029 }
7030 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007031 to->vval.v_dict = dict_copy(from->vval.v_dict,
7032 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033 if (to->vval.v_dict == NULL)
7034 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007036 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007037 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007039 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007040 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007041 }
7042 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007043 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044}
7045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 void
7047echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7048{
7049 char_u *tofree;
7050 char_u numbuf[NUMBUFLEN];
7051 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7052
7053 if (*atstart)
7054 {
7055 *atstart = FALSE;
7056 // Call msg_start() after eval1(), evaluating the expression
7057 // may cause a message to appear.
7058 if (with_space)
7059 {
7060 // Mark the saved text as finishing the line, so that what
7061 // follows is displayed on a new line when scrolling back
7062 // at the more prompt.
7063 msg_sb_eol();
7064 msg_start();
7065 }
7066 }
7067 else if (with_space)
7068 msg_puts_attr(" ", echo_attr);
7069
7070 if (p != NULL)
7071 for ( ; *p != NUL && !got_int; ++p)
7072 {
7073 if (*p == '\n' || *p == '\r' || *p == TAB)
7074 {
7075 if (*p != TAB && *needclr)
7076 {
7077 // remove any text still there from the command
7078 msg_clr_eos();
7079 *needclr = FALSE;
7080 }
7081 msg_putchar_attr(*p, echo_attr);
7082 }
7083 else
7084 {
7085 if (has_mbyte)
7086 {
7087 int i = (*mb_ptr2len)(p);
7088
7089 (void)msg_outtrans_len_attr(p, i, echo_attr);
7090 p += i - 1;
7091 }
7092 else
7093 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7094 }
7095 }
7096 vim_free(tofree);
7097}
7098
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * ":echo expr1 ..." print each argument separated with a space, add a
7101 * newline at the end.
7102 * ":echon expr1 ..." print each argument plain.
7103 */
7104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007109 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 int needclr = TRUE;
7111 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007112 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007113 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007114 evalarg_T evalarg;
7115
Bram Moolenaare707c882020-07-01 13:07:37 +02007116 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 if (eap->skip)
7119 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007120 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007122 // If eval1() causes an error message the text from the command may
7123 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007124 need_clr_eos = needclr;
7125
Bram Moolenaar68db9962021-05-09 23:19:22 +02007126 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007127 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {
7129 /*
7130 * Report the invalid expression unless the expression evaluation
7131 * has been cancelled due to an aborting error, an interrupt, or an
7132 * exception.
7133 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007134 if (!aborting() && did_emsg == did_emsg_before
7135 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007136 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007137 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 break;
7139 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007140 need_clr_eos = FALSE;
7141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007143 {
7144 if (rettv.v_type == VAR_VOID)
7145 {
7146 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7147 break;
7148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007150 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 arg = skipwhite(arg);
7154 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007155 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007156 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 if (eap->skip)
7159 --emsg_skip;
7160 else
7161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007162 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (needclr)
7164 msg_clr_eos();
7165 if (eap->cmdidx == CMD_echo)
7166 msg_end();
7167 }
7168}
7169
7170/*
7171 * ":echohl {name}".
7172 */
7173 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007176 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177}
7178
7179/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007180 * Returns the :echo attribute
7181 */
7182 int
7183get_echo_attr(void)
7184{
7185 return echo_attr;
7186}
7187
7188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 * ":execute expr1 ..." execute the result of an expression.
7190 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007191 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007193 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 * Each gets spaces around each argument and a newline at the end for
7195 * echo commands
7196 */
7197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007198ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 int ret = OK;
7203 char_u *p;
7204 garray_T ga;
7205 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007206 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208 ga_init2(&ga, 1, 80);
7209
7210 if (eap->skip)
7211 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007212 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007214 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007215 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
7218 if (!eap->skip)
7219 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007220 char_u buf[NUMBUFLEN];
7221
7222 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007223 {
7224 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7225 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007226 semsg(_(e_using_invalid_value_as_string_str),
7227 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007228 p = NULL;
7229 }
7230 else
7231 p = tv_get_string_buf(&rettv, buf);
7232 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007233 else
7234 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007235 if (p == NULL)
7236 {
7237 clear_tv(&rettv);
7238 ret = FAIL;
7239 break;
7240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 len = (int)STRLEN(p);
7242 if (ga_grow(&ga, len + 2) == FAIL)
7243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 ret = FAIL;
7246 break;
7247 }
7248 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 ga.ga_len += len;
7252 }
7253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 arg = skipwhite(arg);
7256 }
7257
7258 if (ret != FAIL && ga.ga_data != NULL)
7259 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007260 // use the first line of continuation lines for messages
7261 SOURCING_LNUM = start_lnum;
7262
Bram Moolenaar37fef162022-08-29 18:16:32 +01007263 if (eap->cmdidx == CMD_echomsg
7264 || eap->cmdidx == CMD_echowindow
7265 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007267 // Mark the already saved text as finishing the line, so that what
7268 // follows is displayed on a new line when scrolling back at the
7269 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007270 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007271 }
7272
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007273 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007274 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007275 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007276 out_flush();
7277 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007278 else if (eap->cmdidx == CMD_echowindow)
7279 {
7280#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007281 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007282#endif
7283 msg_attr(ga.ga_data, echo_attr);
7284#ifdef HAS_MESSAGE_WINDOW
7285 end_echowindow();
7286#endif
7287 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007288 else if (eap->cmdidx == CMD_echoconsole)
7289 {
7290 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7291 ui_write((char_u *)"\r\n", 2, TRUE);
7292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 else if (eap->cmdidx == CMD_echoerr)
7294 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007295 int save_did_emsg = did_emsg;
7296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007297 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007298 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (!force_abort)
7300 did_emsg = save_did_emsg;
7301 }
7302 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007303 {
7304 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7305
7306 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7307 sticky_cmdmod_flags = cmdmod.cmod_flags
7308 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 do_cmdline((char_u *)ga.ga_data,
7310 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007311 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 }
7314
7315 ga_clear(&ga);
7316
7317 if (eap->skip)
7318 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007319 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
7322/*
7323 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7324 * "arg" points to the "&" or '+' when called, to "option" when returning.
7325 * Returns NULL when no option name found. Otherwise pointer to the char
7326 * after the option name.
7327 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007328 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007329find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 char_u *p = *arg;
7332
7333 ++p;
7334 if (*p == 'g' && p[1] == ':')
7335 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007336 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 p += 2;
7338 }
7339 else if (*p == 'l' && p[1] == ':')
7340 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007341 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 p += 2;
7343 }
7344 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007345 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 if (!ASCII_ISALPHA(*p))
7348 return NULL;
7349 *arg = p;
7350
7351 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007352 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 else
7354 while (ASCII_ISALPHA(*p))
7355 ++p;
7356 return p;
7357}
7358
7359/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007360 * Display script name where an item was last set.
7361 * Should only be invoked when 'verbose' is non-zero.
7362 */
7363 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007364last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007365{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007366 char_u *p;
7367
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007368 if (script_ctx.sc_sid == 0)
7369 return;
7370
7371 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7372 if (p == NULL)
7373 return;
7374
7375 verbose_enter();
7376 msg_puts(_("\n\tLast set from "));
7377 msg_puts((char *)p);
7378 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007379 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007380 msg_puts(_(line_msg));
7381 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007382 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007383 verbose_leave();
7384 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007385}
7386
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007387#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
7389/*
7390 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007391 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 * "flags" can be "g" to do a global substitute.
7393 * Returns an allocated string, NULL for error.
7394 */
7395 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007396do_string_sub(
7397 char_u *str,
7398 char_u *pat,
7399 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007400 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 int sublen;
7404 regmatch_T regmatch;
7405 int i;
7406 int do_all;
7407 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007408 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 garray_T ga;
7410 char_u *ret;
7411 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007412 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007414 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007416 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 ga_init2(&ga, 1, 200);
7419
7420 do_all = (flags[0] == 'g');
7421
7422 regmatch.rm_ic = p_ic;
7423 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7424 if (regmatch.regprog != NULL)
7425 {
7426 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007427 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007430 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007431 if (regmatch.startp[0] == regmatch.endp[0])
7432 {
7433 if (zero_width == regmatch.startp[0])
7434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007435 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007436 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007437 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7438 (size_t)i);
7439 ga.ga_len += i;
7440 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007441 continue;
7442 }
7443 zero_width = regmatch.startp[0];
7444 }
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 /*
7447 * Get some space for a temporary buffer to do the substitution
7448 * into. It will contain:
7449 * - The text up to where the match is.
7450 * - The substituted text.
7451 * - The text after the match.
7452 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007453 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007454 if (sublen <= 0)
7455 {
7456 ga_clear(&ga);
7457 break;
7458 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007459 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7461 {
7462 ga_clear(&ga);
7463 break;
7464 }
7465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007466 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 i = (int)(regmatch.startp[0] - tail);
7468 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007469 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007470 (void)vim_regsub(&regmatch, sub, expr,
7471 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7472 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007474 tail = regmatch.endp[0];
7475 if (*tail == NUL)
7476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (!do_all)
7478 break;
7479 }
7480
7481 if (ga.ga_data != NULL)
7482 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7483
Bram Moolenaar473de612013-06-08 18:19:48 +02007484 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486
7487 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7488 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007489 if (p_cpo == empty_option)
7490 p_cpo = save_cpo;
7491 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007493 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007494 // If it's still empty it was changed and restored, need to restore in
7495 // the complicated way.
7496 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007497 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007498 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 return ret;
7502}