blob: ae48a6085d45a5113d3008d6fd4810ec91d796eb [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/*
Ernie Raelee865f32023-09-29 19:53:55 +0200989 * Fill in "lp" using "root". This is used in a special case when
990 * "get_lval()" parses a bare word when "lval_root" is not NULL.
991 *
992 * This is typically called with "lval_root" as "root". For a class, find
993 * the name from lp in the class from root, fill in lval_T if found. For a
994 * complex type, list/dict use it as the result; just put the root into
995 * ll_tv.
996 *
997 * "lval_root" is a hack used during run-time/instr-execution to provide the
998 * starting point for "get_lval()" to traverse a chain of indexes. In some
999 * cases get_lval sees a bare name and uses this function to populate the
1000 * lval_T.
1001 *
1002 * For setting up "lval_root" (currently only used with lockvar)
1003 * compile_lock_unlock - pushes object on stack (which becomes lval_root)
1004 * execute_instructions: ISN_LOCKUNLOCK - sets lval_root from stack.
1005 */
1006 static void
1007get_lval_root(lval_T *lp, typval_T *root, int is_arg)
1008{
1009#ifdef LOG_LOCKVAR
1010 ch_log(NULL, "LKVAR: get_lvalroot(): name %s", lp->ll_name);
1011#endif
1012 if (!is_arg && root->v_type == VAR_CLASS)
1013 {
1014 if (root->vval.v_class != NULL)
1015 {
1016 // Special special case. Look for a bare class variable reference.
1017 class_T *cl = root->vval.v_class;
1018 int m_idx;
1019 ocmember_T *m = class_member_lookup(cl, lp->ll_name,
1020 lp->ll_name_end - lp->ll_name, &m_idx);
1021 if (m != NULL)
1022 {
1023 // Assuming "inside class" since bare reference.
1024 lp->ll_class = root->vval.v_class;
1025 lp->ll_oi = m_idx;
1026 lp->ll_valtype = m->ocm_type;
1027 lp->ll_tv = &lp->ll_class->class_members_tv[m_idx];
1028#ifdef LOG_LOCKVAR
1029 ch_log(NULL, "LKVAR: get_lvalroot() class member: name %s",
1030 lp->ll_name);
1031#endif
1032 return;
1033 }
1034 }
1035 }
1036
1037#ifdef LOG_LOCKVAR
1038 ch_log(NULL, "LKVAR: get_lvalroot() any type");
1039#endif
1040 lp->ll_tv = root;
1041 lp->ll_is_root = TRUE;
1042}
1043
1044/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 * Get an lval: variable, Dict item or List item that can be assigned a value
1046 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1047 * "name.key", "name.key[expr]" etc.
1048 * Indexing only works if "name" is an existing List or Dictionary.
1049 * "name" points to the start of the name.
1050 * If "rettv" is not NULL it points to the value to be assigned.
1051 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1052 * wrong; must end in space or cmd separator.
1053 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001054 * flags:
1055 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001056 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001057 * GLV_NO_AUTOLOAD: do not use script autoloading
1058 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001060 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001062 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064get_lval(
1065 char_u *name,
1066 typval_T *rettv,
1067 lval_T *lp,
1068 int unlet,
1069 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001070 int flags, // GLV_ values
1071 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001073 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 char_u *expr_start, *expr_end;
1075 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 dictitem_T *v;
1077 typval_T var1;
1078 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001080 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001082 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001083 int quiet = flags & GLV_QUIET;
Ernie Rael86eda072023-09-05 07:33:48 +02001084 int writing = 0;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001085 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086
Ernie Raelee865f32023-09-29 19:53:55 +02001087#ifdef LOG_LOCKVAR
1088 ch_log(NULL, "LKVAR: get_lval(): name %s, lval_root %p",
1089 name, (void*)lval_root);
1090#endif
1091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001093 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001095 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001097 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001099 lp->ll_name_end = find_name_end(name, NULL, NULL,
1100 FNE_INCL_BR | fne_flags);
1101 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001102 }
1103
Bram Moolenaara749a422022-02-12 19:52:25 +00001104 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001105 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001106 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1107 {
1108 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1109 return NULL;
1110 }
1111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001112 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 if (expr_start != NULL)
1116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001118 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001119 && *p != '[' && *p != '.')
1120 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001121 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 return NULL;
1123 }
1124
1125 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1126 if (lp->ll_exp_name == NULL)
1127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001128 // Report an invalid expression in braces, unless the
1129 // expression evaluation has been cancelled due to an
1130 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001131 if (!aborting() && !quiet)
1132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001133 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001134 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 return NULL;
1136 }
1137 }
1138 lp->ll_name = lp->ll_exp_name;
1139 }
1140 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_name = name;
1143
Bram Moolenaar4525a572022-02-13 11:57:33 +00001144 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001146 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001147 // However, "g:[key]" is indexing a dictionary.
1148 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001149 {
1150 --p;
1151 lp->ll_name_end = p;
1152 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001153 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001154 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001155 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001156
Bram Moolenaar9510d222022-09-11 15:14:05 +01001157 if (is_scoped_variable(name))
1158 {
1159 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1160 return NULL;
1161 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001162 if (VIM_ISWHITE(*p))
1163 {
1164 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1165 return NULL;
1166 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001167 if (tp == p + 1 && !quiet)
1168 {
1169 semsg(_(e_white_space_required_after_str_str), ":", p);
1170 return NULL;
1171 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001172 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001173 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001174 semsg(_(e_using_type_not_in_script_context_str), p);
1175 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001176 }
1177
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001178 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001179 lp->ll_type = parse_type(&tp,
1180 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1181 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001182 if (lp->ll_type == NULL && !quiet)
1183 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001184 lp->ll_name_end = tp;
1185 }
Ernie Raelee865f32023-09-29 19:53:55 +02001186 // TODO: check inside class?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 }
1188 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001189 if (lp->ll_name == NULL)
1190 return p;
1191
Bram Moolenaar62aec932022-01-29 21:45:34 +00001192 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001193 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001194 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001195
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001196 if (import != NULL)
1197 {
1198 ufunc_T *ufunc;
1199 type_T *type;
1200
Bram Moolenaar753885b2022-08-24 16:30:36 +01001201 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001202 lp->ll_sid = import->imp_sid;
1203 lp->ll_name = skipwhite(p + 1);
1204 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1205 lp->ll_name_end = p;
1206
1207 // check the item is exported
1208 cc = *p;
1209 *p = NUL;
1210 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001211 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001212 {
1213 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001214 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001215 }
1216 *p = cc;
1217 }
1218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001220 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001221 if ((*p != '[' && *p != '.'))
Ernie Raelee865f32023-09-29 19:53:55 +02001222 {
1223 if (lval_root != NULL)
1224 get_lval_root(lp, lval_root, lval_root_is_arg);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 return p;
Ernie Raelee865f32023-09-29 19:53:55 +02001226 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227
Bram Moolenaar4525a572022-02-13 11:57:33 +00001228 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001229 {
1230 // using local variable
1231 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001232 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001233 }
1234 else
1235 {
1236 cc = *p;
1237 *p = NUL;
1238 // When we would write to the variable pass &ht and prevent autoload.
1239 writing = !(flags & GLV_READ_ONLY);
1240 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001241 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001242 if (v == NULL && !quiet)
1243 semsg(_(e_undefined_variable_str), lp->ll_name);
1244 *p = cc;
1245 if (v == NULL)
1246 return NULL;
1247 lp->ll_tv = &v->di_tv;
1248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249
Bram Moolenaar4525a572022-02-13 11:57:33 +00001250 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001251 {
1252 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001253 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001254 return NULL;
1255 }
1256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 /*
1258 * Loop until no more [idx] or .key is following.
1259 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 var1.v_type = VAR_UNKNOWN;
1261 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001262 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001264 vartype_T v_type = lp->ll_tv->v_type;
1265
1266 if (*p == '.' && v_type != VAR_DICT
1267 && v_type != VAR_OBJECT
1268 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001269 {
1270 if (!quiet)
1271 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1272 return NULL;
1273 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001274 if (v_type != VAR_LIST
1275 && v_type != VAR_DICT
1276 && v_type != VAR_BLOB
1277 && v_type != VAR_OBJECT
1278 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001281 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001284
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001285 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001286 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001287 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001288 r = rettv_list_alloc(lp->ll_tv);
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +02001289 else if (v_type == VAR_BLOB && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001290 r = rettv_blob_alloc(lp->ll_tv);
1291 if (r == FAIL)
1292 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001293
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001297 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001300
Bram Moolenaar4525a572022-02-13 11:57:33 +00001301 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001302 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001303 && lp->ll_tv == &v->di_tv
1304 && ht != NULL && ht == get_script_local_ht())
1305 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001306 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001307
1308 // Vim9 script local variable: get the type
1309 if (sv != NULL)
1310 lp->ll_valtype = sv->sv_type;
1311 }
1312
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 len = -1;
1314 if (*p == '.')
1315 {
1316 key = p + 1;
1317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1318 ;
1319 if (len == 0)
1320 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001322 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 }
1325 p = key + len;
1326 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 else
1328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001329 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001330 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 if (*p == ':')
1332 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 else
1334 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001336 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001338 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001340 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001341 clear_tv(&var1);
1342 return NULL;
1343 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001344 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 }
1346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001347 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 if (*p == ':')
1349 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001350 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001351 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001353 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001354 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001356 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001357 if (rettv != NULL
1358 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001359 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001360 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001361 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001364 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001365 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001367 }
1368 p = skipwhite(p + 1);
1369 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001370 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001371 else
1372 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001374 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001375 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001376 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001377 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001379 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001380 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001383 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001384 clear_tv(&var2);
1385 return NULL;
1386 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001387 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001392
Bram Moolenaar8c711452005-01-14 21:53:12 +00001393 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001396 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001397 clear_tv(&var1);
1398 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001400 }
1401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001403 ++p;
1404 }
1405
Bram Moolenaard505d172022-12-18 21:42:55 +00001406 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001407 {
1408 if (len == -1)
1409 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001410 // "[key]": get key from "var1"
1411 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001412 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 }
1417 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 lp->ll_list = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001419 lp->ll_object = NULL;
1420 lp->ll_class = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001421
1422 // a NULL dict is equivalent with an empty dict
1423 if (lp->ll_tv->vval.v_dict == NULL)
1424 {
1425 lp->ll_tv->vval.v_dict = dict_alloc();
1426 if (lp->ll_tv->vval.v_dict == NULL)
1427 {
1428 clear_tv(&var1);
1429 return NULL;
1430 }
1431 ++lp->ll_tv->vval.v_dict->dv_refcount;
1432 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001434
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001435 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001437 // When assigning to a scope dictionary check that a function and
1438 // variable name is valid (only variable name unless it is l: or
1439 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001440 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001441 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001442 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001443
1444 if (len != -1)
1445 {
1446 prevval = key[len];
1447 key[len] = NUL;
1448 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001449 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001450 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001451 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001452 && (rettv->v_type == VAR_FUNC
1453 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001454 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001455 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001456 if (len != -1)
1457 key[len] = prevval;
1458 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001459 {
1460 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001461 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001462 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001463 }
1464
Bram Moolenaar10c65862020-10-08 21:16:42 +02001465 if (lp->ll_valtype != NULL)
1466 // use the type of the member
1467 lp->ll_valtype = lp->ll_valtype->tt_member;
1468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001471 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001472 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001473 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001474 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001475 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001476 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001477 return NULL;
1478 }
1479
Bram Moolenaar31b81602019-02-10 22:14:27 +01001480 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001484 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001485 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001487 }
1488 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001490 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001492 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001494 p = NULL;
1495 break;
1496 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001498 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001499 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1500 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001501 {
1502 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001503 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001504 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001505
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001506 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001508 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001509 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001510 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001511 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1512
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001513 /*
1514 * Get the number and item for the only or first index of the List.
1515 */
1516 if (empty1)
1517 lp->ll_n1 = 0;
1518 else
1519 // is number or string
1520 lp->ll_n1 = (long)tv_get_number(&var1);
1521 clear_tv(&var1);
1522
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001523 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001524 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001525 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001526 return NULL;
1527 }
1528 if (lp->ll_range && !lp->ll_empty2)
1529 {
1530 lp->ll_n2 = (long)tv_get_number(&var2);
1531 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001532 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1533 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001534 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001535 }
1536 lp->ll_blob = lp->ll_tv->vval.v_blob;
1537 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001538 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001539 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001540 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001541 {
1542 /*
1543 * Get the number and item for the only or first index of the List.
1544 */
1545 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001546 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001547 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001548 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001550 clear_tv(&var1);
1551
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 lp->ll_dict = NULL;
Ernie Raelee865f32023-09-29 19:53:55 +02001553 lp->ll_object = NULL;
1554 lp->ll_class = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001555 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001556 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1557 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001558 if (lp->ll_li == NULL)
1559 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001560 clear_tv(&var2);
1561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001562 }
1563
Bram Moolenaar10c65862020-10-08 21:16:42 +02001564 if (lp->ll_valtype != NULL)
1565 // use the type of the member
1566 lp->ll_valtype = lp->ll_valtype->tt_member;
1567
Bram Moolenaar8c711452005-01-14 21:53:12 +00001568 /*
1569 * May need to find the item or absolute index for the second
1570 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001571 * When no index given: "lp->ll_empty2" is TRUE.
1572 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001573 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001574 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001576 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001578 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001579 if (check_range_index_two(lp->ll_list,
1580 &lp->ll_n1, lp->ll_li,
1581 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001582 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001583 }
1584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001585 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001586 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001587 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001588 {
Ernie Raelee865f32023-09-29 19:53:55 +02001589 lp->ll_dict = NULL;
1590 lp->ll_list = NULL;
1591
1592 class_T *cl;
1593 if (v_type == VAR_OBJECT && lp->ll_tv->vval.v_object != NULL)
1594 {
1595 cl = lp->ll_tv->vval.v_object->obj_class;
1596 lp->ll_object = lp->ll_tv->vval.v_object;
1597 }
1598 else
1599 {
1600 cl = lp->ll_tv->vval.v_class;
1601 lp->ll_object = NULL;
1602 }
1603 lp->ll_class = cl;
1604
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001605 // TODO: what if class is NULL?
1606 if (cl != NULL)
1607 {
1608 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001609
1610 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001611 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001612 // First look for a function with this name.
1613 // round 1: class functions (skipped for an object)
1614 // round 2: object methods
1615 for (int round = v_type == VAR_OBJECT ? 2 : 1;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001616 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001617 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001618 int m_idx;
1619 ufunc_T *fp;
1620
1621 fp = method_lookup(cl,
1622 round == 1 ? VAR_CLASS : VAR_OBJECT,
1623 key, p - key, &m_idx);
Ernie Raelee865f32023-09-29 19:53:55 +02001624 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001625 if (fp != NULL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001626 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001627 lp->ll_ufunc = fp;
1628 lp->ll_valtype = fp->uf_func_type;
1629 break;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001630 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001631 }
1632 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001633
Ernie Raelee865f32023-09-29 19:53:55 +02001634 // TODO: dont' check access if inside class
1635 // TODO: is GLV_READ_ONLY the right thing to use
1636 // for class/object member access?
1637 // Probably in some cases. Need inside class check
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001638 if (lp->ll_valtype == NULL)
1639 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001640 int m_idx;
Ernie Raelee865f32023-09-29 19:53:55 +02001641 ocmember_T *om
1642 = member_lookup(cl, v_type, key, p - key, &m_idx);
1643 lp->ll_oi = m_idx;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001644 if (om != NULL)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001645 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001646 switch (om->ocm_access)
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001647 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001648 case VIM_ACCESS_PRIVATE:
RestorerZ7fe8f432023-09-24 23:21:24 +02001649 semsg(_(e_cannot_access_private_variable_str),
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001650 om->ocm_name);
1651 return NULL;
1652 case VIM_ACCESS_READ:
Ernie Rael98e68c02023-09-20 20:13:06 +02001653 // If [idx] or .key following, read only OK.
1654 if (*p == '[' || *p == '.')
1655 break;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001656 if ((flags & GLV_READ_ONLY) == 0)
1657 {
RestorerZ7fe8f432023-09-24 23:21:24 +02001658 semsg(_(e_variable_is_not_writable_str),
Ernie Rael696270b2023-09-21 16:42:28 +02001659 om->ocm_name, cl->class_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001660 return NULL;
1661 }
1662 break;
1663 case VIM_ACCESS_ALL:
1664 break;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001665 }
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001666
1667 lp->ll_valtype = om->ocm_type;
1668
1669 if (v_type == VAR_OBJECT)
1670 lp->ll_tv = ((typval_T *)(
1671 lp->ll_tv->vval.v_object + 1)) + m_idx;
1672 else
1673 lp->ll_tv = &cl->class_members_tv[m_idx];
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001674 }
1675 }
1676
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001677 if (lp->ll_valtype == NULL)
1678 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02001679 member_not_found_msg(cl, v_type, key, p - key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001680 return NULL;
1681 }
1682 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 }
1685
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001686 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001688 return p;
1689}
1690
1691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001693 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001696{
1697 vim_free(lp->ll_exp_name);
1698 vim_free(lp->ll_newkey);
1699}
1700
1701/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001702 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001704 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1705 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001706 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708set_var_lval(
1709 lval_T *lp,
1710 char_u *endp,
1711 typval_T *rettv,
1712 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001713 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1714 char_u *op,
1715 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001716{
1717 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001718 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001719
1720 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001722 cc = *endp;
1723 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001724 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001725 return;
1726
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001727 if (lp->ll_blob != NULL)
1728 {
1729 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001730
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 if (op != NULL && *op != '=')
1732 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001733 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 return;
1735 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001736 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001737 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738
1739 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1740 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001741 if (lp->ll_empty2)
1742 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1743
Bram Moolenaar68452172021-04-12 21:21:02 +02001744 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1745 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 }
1748 else
1749 {
1750 val = (int)tv_get_number_chk(rettv, &error);
1751 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001752 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 }
1754 }
1755 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001757 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001759 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1760 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001761 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001762 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001763 *endp = cc;
1764 return;
1765 }
1766
Bram Moolenaarff697e62019-02-12 22:28:33 +01001767 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001768 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001769 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001770 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001771 {
1772 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001773 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1774 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001775 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001776 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001777 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001778 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001781 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001782 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001783 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1784 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001785 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001786 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001787 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001788 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001789 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001791 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001792 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001793 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001794 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001795 else if (lp->ll_range)
1796 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001797 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1798 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001799 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001800 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001801 return;
1802 }
1803
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001804 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1805 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 }
1807 else
1808 {
1809 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001810 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001811 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001812 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1813 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001814 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001815 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001816 return;
1817 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001818
1819 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001820 && check_typval_arg_type(lp->ll_valtype, rettv,
1821 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001822 return;
1823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001824 if (lp->ll_newkey != NULL)
1825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 if (op != NULL && *op != '=')
1827 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001828 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 return;
1830 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001831 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1832 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001833 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001835 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001836 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001837 if (di == NULL)
1838 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001839 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1840 {
1841 vim_free(di);
1842 return;
1843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001844 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001846 else if (op != NULL && *op != '=')
1847 {
1848 tv_op(lp->ll_tv, rettv, op);
1849 return;
1850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001854 /*
1855 * Assign the value to the variable or list item.
1856 */
1857 if (copy)
1858 copy_tv(rettv, lp->ll_tv);
1859 else
1860 {
1861 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001862 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001863 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 }
1865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866}
1867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001869 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1870 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 * Returns OK or FAIL.
1872 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001876 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 char_u numbuf[NUMBUFLEN];
1878 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001879 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001881 // Can't do anything with a Funcref or Dict on the right.
1882 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001883 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001884 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1885 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 {
1887 switch (tv1->v_type)
1888 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001889 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001890 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 case VAR_DICT:
1893 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001894 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001895 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001896 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001897 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001898 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001899 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001900 case VAR_CLASS:
1901 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 break;
1903
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001904 case VAR_BLOB:
1905 if (*op != '+' || tv2->v_type != VAR_BLOB)
1906 break;
1907 // BLOB += BLOB
1908 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1909 {
1910 blob_T *b1 = tv1->vval.v_blob;
1911 blob_T *b2 = tv2->vval.v_blob;
1912 int i, len = blob_len(b2);
1913 for (i = 0; i < len; i++)
1914 ga_append(&b1->bv_ga, blob_get(b2, i));
1915 }
1916 return OK;
1917
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 case VAR_LIST:
1919 if (*op != '+' || tv2->v_type != VAR_LIST)
1920 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001921 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001922 if (tv2->vval.v_list != NULL)
1923 {
1924 if (tv1->vval.v_list == NULL)
1925 {
1926 tv1->vval.v_list = tv2->vval.v_list;
1927 ++tv1->vval.v_list->lv_refcount;
1928 }
1929 else
1930 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1931 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 return OK;
1933
1934 case VAR_NUMBER:
1935 case VAR_STRING:
1936 if (tv2->v_type == VAR_LIST)
1937 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001938 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001940 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001941 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001942 if (tv2->v_type == VAR_FLOAT)
1943 {
1944 float_T f = n;
1945
Bram Moolenaarff697e62019-02-12 22:28:33 +01001946 if (*op == '%')
1947 break;
1948 switch (*op)
1949 {
1950 case '+': f += tv2->vval.v_float; break;
1951 case '-': f -= tv2->vval.v_float; break;
1952 case '*': f *= tv2->vval.v_float; break;
1953 case '/': f /= tv2->vval.v_float; break;
1954 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001955 clear_tv(tv1);
1956 tv1->v_type = VAR_FLOAT;
1957 tv1->vval.v_float = f;
1958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001960 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001961 switch (*op)
1962 {
1963 case '+': n += tv_get_number(tv2); break;
1964 case '-': n -= tv_get_number(tv2); break;
1965 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001966 case '/': n = num_divide(n, tv_get_number(tv2),
1967 &failed); break;
1968 case '%': n = num_modulus(n, tv_get_number(tv2),
1969 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001971 clear_tv(tv1);
1972 tv1->v_type = VAR_NUMBER;
1973 tv1->vval.v_number = n;
1974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 }
1976 else
1977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001978 if (tv2->v_type == VAR_FLOAT)
1979 break;
1980
Bram Moolenaarff697e62019-02-12 22:28:33 +01001981 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001982 s = tv_get_string(tv1);
1983 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 clear_tv(tv1);
1985 tv1->v_type = VAR_STRING;
1986 tv1->vval.v_string = s;
1987 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001988 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001989
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001990 case VAR_FLOAT:
1991 {
1992 float_T f;
1993
Bram Moolenaarff697e62019-02-12 22:28:33 +01001994 if (*op == '%' || *op == '.'
1995 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001996 && tv2->v_type != VAR_NUMBER
1997 && tv2->v_type != VAR_STRING))
1998 break;
1999 if (tv2->v_type == VAR_FLOAT)
2000 f = tv2->vval.v_float;
2001 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002002 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002003 switch (*op)
2004 {
2005 case '+': tv1->vval.v_float += f; break;
2006 case '-': tv1->vval.v_float -= f; break;
2007 case '*': tv1->vval.v_float *= f; break;
2008 case '/': tv1->vval.v_float /= f; break;
2009 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002010 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002011 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002012 }
2013 }
2014
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002015 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016 return FAIL;
2017}
2018
2019/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 * Evaluate the expression used in a ":for var in expr" command.
2021 * "arg" points to "var".
2022 * Set "*errp" to TRUE for an error, FALSE otherwise;
2023 * Return a pointer that holds the info. Null when there is an error.
2024 */
2025 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002026eval_for_line(
2027 char_u *arg,
2028 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002029 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002030 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031{
Bram Moolenaar33570922005-01-25 22:26:29 +00002032 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02002033 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 typval_T tv;
2036 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002037 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002041 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 if (fi == NULL)
2043 return NULL;
2044
Bram Moolenaar404557e2021-07-05 21:41:48 +02002045 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
2046 &fi->fi_semicolon, FALSE);
2047 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return fi;
2049
Bram Moolenaar404557e2021-07-05 21:41:48 +02002050 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002051 if (expr[0] != 'i' || expr[1] != 'n'
2052 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02002054 if (in_vim9script() && *expr == ':' && expr != var_list_end)
2055 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
2056 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002057 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 return fi;
2059 }
2060
2061 if (skip)
2062 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002063 expr = skipwhite_and_linebreak(expr + 2, evalarg);
2064 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 {
2066 *errp = FALSE;
2067 if (!skip)
2068 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002069 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002070 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002071 l = tv.vval.v_list;
2072 if (l == NULL)
2073 {
2074 // a null list is like an empty list: do nothing
2075 clear_tv(&tv);
2076 }
2077 else
2078 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002080 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002082 // No need to increment the refcount, it's already set for
2083 // the list being used in "tv".
2084 fi->fi_list = l;
2085 list_add_watch(l, &fi->fi_lw);
2086 fi->fi_lw.lw_item = l->lv_first;
2087 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002088 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002089 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002090 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002091 fi->fi_bi = 0;
2092 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002093 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002094 typval_T btv;
2095
2096 // Make a copy, so that the iteration still works when the
2097 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002099 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002100 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002101 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002102 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002103 else if (tv.v_type == VAR_STRING)
2104 {
2105 fi->fi_byte_idx = 0;
2106 fi->fi_string = tv.vval.v_string;
2107 tv.vval.v_string = NULL;
2108 if (fi->fi_string == NULL)
2109 fi->fi_string = vim_strsave((char_u *)"");
2110 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111 else
2112 {
Sean Dewar80d73952021-08-04 19:25:54 +02002113 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002114 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 }
2116 }
2117 }
2118 if (skip)
2119 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002120 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121
2122 return fi;
2123}
2124
2125/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002126 * Used when looping over a :for line, skip the "in expr" part.
2127 */
2128 void
2129skip_for_lines(void *fi_void, evalarg_T *evalarg)
2130{
2131 forinfo_T *fi = (forinfo_T *)fi_void;
2132 int i;
2133
2134 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002135 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002136}
2137
2138/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 * Use the first item in a ":for" list. Advance to the next.
2140 * Assign the values to the variable (list). "arg" points to the first one.
2141 * Return TRUE when a valid item was found, FALSE when at end of list or
2142 * something wrong.
2143 */
2144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002145next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002147 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002149 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002150 ? (ASSIGN_FINAL
2151 // first round: error if variable exists
2152 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002153 | ASSIGN_NO_MEMBER_TYPE
2154 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002155 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002156 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002157 int skip_assign = in_vim9script() && arg[0] == '_'
2158 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002160 if (fi->fi_blob != NULL)
2161 {
2162 typval_T tv;
2163
2164 if (fi->fi_bi >= blob_len(fi->fi_blob))
2165 return FALSE;
2166 tv.v_type = VAR_NUMBER;
2167 tv.v_lock = VAR_FIXED;
2168 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2169 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002170 if (skip_assign)
2171 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002172 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002173 fi->fi_varcount, flag, NULL) == OK;
2174 }
2175
2176 if (fi->fi_string != NULL)
2177 {
2178 typval_T tv;
2179 int len;
2180
2181 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2182 if (len == 0)
2183 return FALSE;
2184 tv.v_type = VAR_STRING;
2185 tv.v_lock = VAR_FIXED;
2186 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2187 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002188 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002189 if (skip_assign)
2190 result = TRUE;
2191 else
2192 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002193 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002194 vim_free(tv.vval.v_string);
2195 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002196 }
2197
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002198 item = fi->fi_lw.lw_item;
2199 if (item == NULL)
2200 result = FALSE;
2201 else
2202 {
2203 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002204 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002205 if (skip_assign)
2206 result = TRUE;
2207 else
2208 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002209 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002210 }
2211 return result;
2212}
2213
2214/*
2215 * Free the structure used to store info used by ":for".
2216 */
2217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002219{
Bram Moolenaar33570922005-01-25 22:26:29 +00002220 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002221
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002222 if (fi == NULL)
2223 return;
2224 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002225 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002226 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002227 list_unref(fi->fi_list);
2228 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002229 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002230 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002231 else
2232 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002233 vim_free(fi);
2234}
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237set_context_for_expression(
2238 expand_T *xp,
2239 char_u *arg,
2240 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002242 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002244 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002246 if (cmdidx == CMD_let || cmdidx == CMD_var
2247 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002248 {
2249 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002252 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002254 {
2255 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002256 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002257 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002258 break;
2259 }
2260 return;
2261 }
2262 }
2263 else
2264 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2265 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 while ((xp->xp_pattern = vim_strpbrk(arg,
2267 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2268 {
2269 c = *xp->xp_pattern;
2270 if (c == '&')
2271 {
2272 c = xp->xp_pattern[1];
2273 if (c == '&')
2274 {
2275 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002276 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002281 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2282 xp->xp_pattern += 2;
2283
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286 else if (c == '$')
2287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002288 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 xp->xp_context = EXPAND_ENV_VARS;
2290 }
2291 else if (c == '=')
2292 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002293 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 xp->xp_context = EXPAND_EXPRESSION;
2295 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002296 else if (c == '#'
2297 && xp->xp_context == EXPAND_EXPRESSION)
2298 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002299 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002300 break;
2301 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002302 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 && xp->xp_context == EXPAND_FUNCTIONS
2304 && vim_strchr(xp->xp_pattern, '(') == NULL)
2305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002306 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 break;
2308 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002309 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002311 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2314 if (c == '\\' && xp->xp_pattern[1] != NUL)
2315 ++xp->xp_pattern;
2316 xp->xp_context = EXPAND_NOTHING;
2317 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002320 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2322 /* skip */ ;
2323 xp->xp_context = EXPAND_NOTHING;
2324 }
2325 else if (c == '|')
2326 {
2327 if (xp->xp_pattern[1] == '|')
2328 {
2329 ++xp->xp_pattern;
2330 xp->xp_context = EXPAND_EXPRESSION;
2331 }
2332 else
2333 xp->xp_context = EXPAND_COMMANDS;
2334 }
2335 else
2336 xp->xp_context = EXPAND_EXPRESSION;
2337 }
2338 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002339 // Doesn't look like something valid, expand as an expression
2340 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 arg = xp->xp_pattern;
2343 if (*arg != NUL)
2344 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2345 /* skip */ ;
2346 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002347
2348 // ":exe one two" completes "two"
2349 if ((cmdidx == CMD_execute
2350 || cmdidx == CMD_echo
2351 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002352 || cmdidx == CMD_echomsg
2353 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002354 && xp->xp_context == EXPAND_EXPRESSION)
2355 {
2356 for (;;)
2357 {
2358 char_u *n = skiptowhite(arg);
2359
2360 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2361 break;
2362 arg = skipwhite(n);
2363 }
2364 }
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 xp->xp_pattern = arg;
2367}
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002370 * Return TRUE if "pat" matches "text".
2371 * Does not use 'cpo' and always uses 'magic'.
2372 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002373 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002374pattern_match(char_u *pat, char_u *text, int ic)
2375{
2376 int matches = FALSE;
2377 char_u *save_cpo;
2378 regmatch_T regmatch;
2379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002380 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002381 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002382 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002383 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2384 if (regmatch.regprog != NULL)
2385 {
2386 regmatch.rm_ic = ic;
2387 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2388 vim_regfree(regmatch.regprog);
2389 }
2390 p_cpo = save_cpo;
2391 return matches;
2392}
2393
2394/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002395 * Handle a name followed by "(". Both for just "name(arg)" and for
2396 * "expr->name(arg)".
2397 * Returns OK or FAIL.
2398 */
2399 static int
2400eval_func(
2401 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002402 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002403 char_u *name,
2404 int name_len,
2405 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002406 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002407 typval_T *basetv) // "expr" for "expr->name(arg)"
2408{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002409 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002410 char_u *s = name;
2411 int len = name_len;
2412 partial_T *partial;
2413 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002414 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002415 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002416
2417 if (!evaluate)
2418 check_vars(s, len);
2419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // If "s" is the name of a variable of type VAR_FUNC
2421 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002422 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002423 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002425 // Need to make a copy, in case evaluating the arguments makes
2426 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002427 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002428 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002429 ret = FAIL;
2430 else
2431 {
2432 funcexe_T funcexe;
2433
2434 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002435 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002436 funcexe.fe_firstline = curwin->w_cursor.lnum;
2437 funcexe.fe_lastline = curwin->w_cursor.lnum;
2438 funcexe.fe_evaluate = evaluate;
2439 funcexe.fe_partial = partial;
2440 funcexe.fe_basetv = basetv;
2441 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002442 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002443 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002444 }
2445 vim_free(s);
2446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002447 // If evaluate is FALSE rettv->v_type was not set in
2448 // get_func_tv, but it's needed in handle_subscript() to parse
2449 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002450 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2451 {
2452 rettv->vval.v_string = NULL;
2453 rettv->v_type = VAR_FUNC;
2454 }
2455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002456 // Stop the expression evaluation when immediately
2457 // aborting on error, or when an interrupt occurred or
2458 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002459 if (evaluate && aborting())
2460 {
2461 if (ret == OK)
2462 clear_tv(rettv);
2463 ret = FAIL;
2464 }
2465 return ret;
2466}
2467
2468/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002469 * After a NL, skip over empty lines and comment-only lines.
2470 */
2471 static char_u *
2472newline_skip_comments(char_u *arg)
2473{
2474 char_u *p = arg + 1;
2475
2476 for (;;)
2477 {
2478 p = skipwhite(p);
2479
2480 if (*p == NUL)
2481 break;
2482 if (vim9_comment_start(p))
2483 {
2484 char_u *nl = vim_strchr(p, NL);
2485
2486 if (nl == NULL)
2487 break;
2488 p = nl;
2489 }
2490 if (*p != NL)
2491 break;
2492 ++p; // skip another NL
2493 }
2494 return p;
2495}
2496
2497/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002498 * Get the next line source line without advancing. But do skip over comment
2499 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002500 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002501 */
2502 static char_u *
2503getline_peek_skip_comments(evalarg_T *evalarg)
2504{
2505 for (;;)
2506 {
2507 char_u *next = getline_peek(evalarg->eval_getline,
2508 evalarg->eval_cookie);
2509 char_u *p;
2510
2511 if (next == NULL)
2512 break;
2513 p = skipwhite(next);
2514 if (*p != NUL && !vim9_comment_start(p))
2515 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002516 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002517 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002518 }
2519 return NULL;
2520}
2521
2522/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002523 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2524 * comment) and there is a next line, return the next line (skipping blanks)
2525 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002526 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2527 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 * "arg" must point somewhere inside a line, not at the start.
2529 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002530 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2532{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002533 char_u *p = skipwhite(arg);
2534
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002536 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002537 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002538 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2539 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002540 && (*p == NUL || *p == NL
2541 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002543 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002544
Bram Moolenaare442d592022-05-05 12:20:28 +01002545 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002546 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002547 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002548 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002549 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002550 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551
Bram Moolenaar9d489562020-07-30 20:08:50 +02002552 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002554 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002555 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002556 }
2557 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002558 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559}
2560
2561/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002562 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002563 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002564 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002565 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002566eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002567{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002568 garray_T *gap = &evalarg->eval_ga;
2569 char_u *line;
2570
Bram Moolenaar39be4982022-05-06 21:51:50 +01002571 if (arg != NULL)
2572 {
2573 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002574 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002575 // Truncate before a trailing comment, so that concatenating the lines
2576 // won't turn the rest into a comment.
2577 if (*skipwhite(arg) == '#')
2578 *arg = NUL;
2579 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002580
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002581 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002582 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2583 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002584 else
2585 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002586 if (line == NULL)
2587 return NULL;
2588
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002589 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002590 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2591 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002592 char_u *p = skipwhite(line);
2593
2594 // Going to concatenate the lines after parsing. For an empty or
2595 // comment line use an empty string.
2596 if (*p == NUL || vim9_comment_start(p))
2597 {
2598 vim_free(line);
2599 line = vim_strsave((char_u *)"");
2600 }
2601
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002602 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2603 ++gap->ga_len;
2604 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002605 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002606 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002607 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002608 evalarg->eval_tofree = line;
2609 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002610
2611 // Advanced to the next line, "arg" no longer points into the previous
2612 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002613 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002614 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002615}
2616
Bram Moolenaare6b53242020-07-01 17:28:33 +02002617/*
2618 * Call eval_next_non_blank() and get the next line if needed.
2619 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002620 char_u *
2621skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2622{
2623 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002624 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002625
Bram Moolenaar962d7212020-07-04 14:15:00 +02002626 if (evalarg == NULL)
2627 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002628 eval_next_non_blank(p, evalarg, &getnext);
2629 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002630 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002631 return p;
2632}
2633
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002634/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002635 * The "eval" functions have an "evalarg" argument: When NULL or
2636 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2637 * parsed but not executed. The functions may return OK, but the rettv will be
2638 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 */
2640
2641/*
2642 * Handle zero level expression.
2643 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002645 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Return OK or FAIL.
2648 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002650eval0(
2651 char_u *arg,
2652 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002653 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002656 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2657}
2658
2659/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002660 * If "arg" is a simple function call without arguments then call it and return
2661 * the result. Otherwise return NOTDONE.
2662 */
2663 int
2664may_call_simple_func(
2665 char_u *arg,
2666 typval_T *rettv)
2667{
2668 char_u *parens = (char_u *)strstr((char *)arg, "()");
2669 int r = NOTDONE;
2670
2671 // If the expression is "FuncName()" then we can skip a lot of overhead.
2672 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2673 {
2674 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2675
2676 if (to_name_end(p, TRUE) == parens)
2677 r = call_simple_func(arg, (int)(parens - arg), rettv);
2678 }
2679 return r;
2680}
2681
2682/*
2683 * Handle zero level expression with optimization for a simple function call.
2684 * Same arguments and return value as eval0().
2685 */
2686 int
2687eval0_simple_funccal(
2688 char_u *arg,
2689 typval_T *rettv,
2690 exarg_T *eap,
2691 evalarg_T *evalarg)
2692{
2693 int r = may_call_simple_func(arg, rettv);
2694
2695 if (r == NOTDONE)
2696 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2697 return r;
2698}
2699
2700/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002701 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2702 * expression and don't check what comes after the expression.
2703 */
2704 int
2705eval0_retarg(
2706 char_u *arg,
2707 typval_T *rettv,
2708 exarg_T *eap,
2709 evalarg_T *evalarg,
2710 char_u **retarg)
2711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 int ret;
2713 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002714 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002715 int did_emsg_before = did_emsg;
2716 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002717 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002718 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002721 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002722
Bram Moolenaar79481362022-06-27 20:15:10 +01002723 if (ret != FAIL)
2724 {
2725 expr_end = p;
2726 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002727
Bram Moolenaar79481362022-06-27 20:15:10 +01002728 // In Vim9 script a command block is not split at NL characters for
2729 // commands using an expression argument. Skip over a '#' comment to
2730 // check for a following NL. Require white space before the '#'.
2731 if (in_vim9script() && p > expr_end && retarg == NULL)
2732 while (*p == '#')
2733 {
2734 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002735
Bram Moolenaar79481362022-06-27 20:15:10 +01002736 if (nl == NULL)
2737 break;
2738 p = skipwhite(nl + 1);
2739 if (eap != NULL && *p != NUL)
2740 eap->nextcmd = p;
2741 check_for_end = FALSE;
2742 }
2743
2744 if (check_for_end)
2745 end_error = !ends_excmd2(arg, p);
2746 }
2747
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002748 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 /*
2753 * Report the invalid expression unless the expression evaluation has
2754 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002755 * exception, or we already gave a more specific error.
2756 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002758 if (!aborting()
2759 && did_emsg == did_emsg_before
2760 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002761 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002762 {
2763 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002764 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002765 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002766 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002767 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002768
zeertzjqf2588b62023-05-05 17:22:35 +01002769 if (eap != NULL && p != NULL)
2770 {
2771 // Some of the expression may not have been consumed.
2772 // Only execute a next command if it cannot be a "||" operator.
2773 // The next command may be "catch".
2774 char_u *nextcmd = check_nextcmd(p);
2775 if (nextcmd != NULL && *nextcmd != '|')
2776 eap->nextcmd = nextcmd;
2777 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002778 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002780
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002781 if (retarg != NULL)
2782 *retarg = p;
2783 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002784 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 return ret;
2787}
2788
2789/*
2790 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002791 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002792 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 *
2794 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002795 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002797 * Note: "rettv.v_lock" is not set.
2798 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 * Return OK or FAIL.
2800 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002801 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002804 char_u *p;
2805 int getnext;
2806
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002807 CLEAR_POINTER(rettv);
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 /*
2810 * Get the first variable.
2811 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 return FAIL;
2814
Bram Moolenaar793648f2020-06-26 21:28:25 +02002815 p = eval_next_non_blank(*arg, evalarg, &getnext);
2816 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002818 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 int result;
2820 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 evalarg_T *evalarg_used = evalarg;
2822 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002824 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002825 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826
2827 if (evalarg == NULL)
2828 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002829 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002830 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002831 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002832 orig_flags = evalarg_used->eval_flags;
2833 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002835 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002836 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002837 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002838 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002839 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002840 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002841 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002842 clear_tv(rettv);
2843 return FAIL;
2844 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002845 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002846 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002849 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 int error = FALSE;
2852
Bram Moolenaar13106602020-10-04 16:06:05 +02002853 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002854 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002855 else if (vim9script)
2856 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002857 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002859 if (error || !op_falsy || !result)
2860 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 if (error)
2862 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864
2865 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002866 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002868 if (op_falsy)
2869 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002870 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002871 {
mityu4ac198c2021-05-28 17:52:40 +02002872 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002873 clear_tv(rettv);
2874 return FAIL;
2875 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002876 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002877 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002878 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002879 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002880 {
2881 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002883 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002884 if (!op_falsy || !result)
2885 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002887 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002889 /*
2890 * Check for the ":".
2891 */
2892 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2893 if (*p != ':')
2894 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002895 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002896 if (evaluate && result)
2897 clear_tv(rettv);
2898 evalarg_used->eval_flags = orig_flags;
2899 return FAIL;
2900 }
2901 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002902 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002903 else
2904 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002905 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002906 {
2907 error_white_both(p, 1);
2908 clear_tv(rettv);
2909 evalarg_used->eval_flags = orig_flags;
2910 return FAIL;
2911 }
2912 *arg = p;
2913 }
2914
2915 /*
2916 * Get the third variable. Recursive!
2917 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002918 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002919 {
mityu4ac198c2021-05-28 17:52:40 +02002920 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002921 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002922 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002923 return FAIL;
2924 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002925 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2926 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002927 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002928 if (eval1(arg, &var2, evalarg_used) == FAIL)
2929 {
2930 if (evaluate && result)
2931 clear_tv(rettv);
2932 evalarg_used->eval_flags = orig_flags;
2933 return FAIL;
2934 }
2935 if (evaluate && !result)
2936 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002938
2939 if (evalarg == NULL)
2940 clear_evalarg(&local_evalarg, NULL);
2941 else
2942 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944
2945 return OK;
2946}
2947
2948/*
2949 * Handle first level expression:
2950 * expr2 || expr2 || expr2 logical OR
2951 *
2952 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002953 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 *
2955 * Return OK or FAIL.
2956 */
2957 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002960 char_u *p;
2961 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002964 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 return FAIL;
2968
2969 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002970 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002972 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002973 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002975 evalarg_T *evalarg_used = evalarg;
2976 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002977 int evaluate;
2978 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002979 long result = FALSE;
2980 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002981 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002982 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002983
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 if (evalarg == NULL)
2985 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002986 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002987 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002989 orig_flags = evalarg_used->eval_flags;
2990 evaluate = orig_flags & EVAL_EVALUATE;
2991 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002993 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002994 result = tv_get_bool_chk(rettv, &error);
2995 else if (tv_get_number_chk(rettv, &error) != 0)
2996 result = TRUE;
2997 clear_tv(rettv);
2998 if (error)
2999 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001
3002 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003003 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003005 while (p[0] == '|' && p[1] == '|')
3006 {
3007 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003008 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003009 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003010 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003011 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003012 {
3013 error_white_both(p, 2);
3014 clear_tv(rettv);
3015 return FAIL;
3016 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003017 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003018 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003019
3020 /*
3021 * Get the second variable.
3022 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003023 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003024 {
mityu4ac198c2021-05-28 17:52:40 +02003025 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003026 clear_tv(rettv);
3027 return FAIL;
3028 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003029 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3030 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003031 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003032 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003034
3035 /*
3036 * Compute the result.
3037 */
3038 if (evaluate && !result)
3039 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003040 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003041 result = tv_get_bool_chk(&var2, &error);
3042 else if (tv_get_number_chk(&var2, &error) != 0)
3043 result = TRUE;
3044 clear_tv(&var2);
3045 if (error)
3046 return FAIL;
3047 }
3048 if (evaluate)
3049 {
3050 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003051 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003052 rettv->v_type = VAR_BOOL;
3053 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003054 }
3055 else
3056 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003057 rettv->v_type = VAR_NUMBER;
3058 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003059 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003060 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003061
3062 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003064
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003065 if (evalarg == NULL)
3066 clear_evalarg(&local_evalarg, NULL);
3067 else
3068 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
3070
3071 return OK;
3072}
3073
3074/*
3075 * Handle second level expression:
3076 * expr3 && expr3 && expr3 logical AND
3077 *
3078 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003079 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 *
3081 * Return OK or FAIL.
3082 */
3083 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003084eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003086 char_u *p;
3087 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003090 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003092 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return FAIL;
3094
3095 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003096 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003098 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003099 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003101 evalarg_T *evalarg_used = evalarg;
3102 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003103 int orig_flags;
3104 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003105 long result = TRUE;
3106 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003107 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003108 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003109
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110 if (evalarg == NULL)
3111 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003112 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003113 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003115 orig_flags = evalarg_used->eval_flags;
3116 evaluate = orig_flags & EVAL_EVALUATE;
3117 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003118 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003119 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003120 result = tv_get_bool_chk(rettv, &error);
3121 else if (tv_get_number_chk(rettv, &error) == 0)
3122 result = FALSE;
3123 clear_tv(rettv);
3124 if (error)
3125 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127
3128 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003129 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003131 while (p[0] == '&' && p[1] == '&')
3132 {
3133 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003134 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003135 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003136 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003137 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003138 {
3139 error_white_both(p, 2);
3140 clear_tv(rettv);
3141 return FAIL;
3142 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003143 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003144 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003145
3146 /*
3147 * Get the second variable.
3148 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003149 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003150 {
mityu4ac198c2021-05-28 17:52:40 +02003151 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003152 clear_tv(rettv);
3153 return FAIL;
3154 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003155 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3156 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003157 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003158 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003159 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003160 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003161
3162 /*
3163 * Compute the result.
3164 */
3165 if (evaluate && result)
3166 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003167 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003168 result = tv_get_bool_chk(&var2, &error);
3169 else if (tv_get_number_chk(&var2, &error) == 0)
3170 result = FALSE;
3171 clear_tv(&var2);
3172 if (error)
3173 return FAIL;
3174 }
3175 if (evaluate)
3176 {
3177 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003178 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003179 rettv->v_type = VAR_BOOL;
3180 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003181 }
3182 else
3183 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003184 rettv->v_type = VAR_NUMBER;
3185 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003186 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003187 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003188
3189 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003191
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003192 if (evalarg == NULL)
3193 clear_evalarg(&local_evalarg, NULL);
3194 else
3195 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197
3198 return OK;
3199}
3200
3201/*
3202 * Handle third level expression:
3203 * var1 == var2
3204 * var1 =~ var2
3205 * var1 != var2
3206 * var1 !~ var2
3207 * var1 > var2
3208 * var1 >= var2
3209 * var1 < var2
3210 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003211 * var1 is var2
3212 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 *
3214 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003215 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 *
3217 * Return OK or FAIL.
3218 */
3219 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003223 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003224 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003226 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003229 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003231 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 return FAIL;
3233
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003234 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003235
Bram Moolenaar696ba232020-07-29 21:20:41 +02003236 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003241 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003243 typval_T var2;
3244 int ic;
3245 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003246 int evaluate = evalarg == NULL
3247 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003248 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003249
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003250 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003251 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003252 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003253 p = *arg;
3254 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003255 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3256 {
mityu4ac198c2021-05-28 17:52:40 +02003257 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003258 clear_tv(rettv);
3259 return FAIL;
3260 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003261
Bram Moolenaar696ba232020-07-29 21:20:41 +02003262 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3263 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003264 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003265 clear_tv(rettv);
3266 return FAIL;
3267 }
3268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (p[len] == '?')
3271 {
3272 ic = TRUE;
3273 ++len;
3274 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 else if (p[len] == '#')
3277 {
3278 ic = FALSE;
3279 ++len;
3280 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003281 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003283 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285 /*
3286 * Get the second variable.
3287 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003288 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3289 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003290 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003291 clear_tv(rettv);
3292 return FAIL;
3293 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003294 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003295 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 return FAIL;
3299 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003300 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003301 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003302 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003303
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003304 // use the line of the comparison for messages
3305 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003306 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003307 {
3308 ret = FAIL;
3309 clear_tv(rettv);
3310 }
3311 else
3312 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003313 clear_tv(&var2);
3314 return ret;
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317
3318 return OK;
3319}
3320
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003321/*
3322 * Make a copy of blob "tv1" and append blob "tv2".
3323 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 void
3325eval_addblob(typval_T *tv1, typval_T *tv2)
3326{
3327 blob_T *b1 = tv1->vval.v_blob;
3328 blob_T *b2 = tv2->vval.v_blob;
3329 blob_T *b = blob_alloc();
3330 int i;
3331
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003332 if (b == NULL)
3333 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003335 for (i = 0; i < blob_len(b1); i++)
3336 ga_append(&b->bv_ga, blob_get(b1, i));
3337 for (i = 0; i < blob_len(b2); i++)
3338 ga_append(&b->bv_ga, blob_get(b2, i));
3339
3340 clear_tv(tv1);
3341 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342}
3343
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003344/*
3345 * Make a copy of list "tv1" and append list "tv2".
3346 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 int
3348eval_addlist(typval_T *tv1, typval_T *tv2)
3349{
3350 typval_T var3;
3351
3352 // concatenate Lists
3353 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3354 {
3355 clear_tv(tv1);
3356 clear_tv(tv2);
3357 return FAIL;
3358 }
3359 clear_tv(tv1);
3360 *tv1 = var3;
3361 return OK;
3362}
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003365 * Handle the bitwise left/right shift operator expression:
3366 * var1 << var2
3367 * var1 >> var2
3368 *
3369 * "arg" must point to the first non-white of the expression.
3370 * "arg" is advanced to just after the recognized expression.
3371 *
3372 * Return OK or FAIL.
3373 */
3374 static int
3375eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3376{
3377 /*
3378 * Get the first expression.
3379 */
3380 if (eval6(arg, rettv, evalarg) == FAIL)
3381 return FAIL;
3382
3383 /*
3384 * Repeat computing, until no '<<' or '>>' is following.
3385 */
3386 for (;;)
3387 {
3388 char_u *p;
3389 int getnext;
3390 exprtype_T type;
3391 int evaluate;
3392 typval_T var2;
3393 int vim9script;
3394
3395 p = eval_next_non_blank(*arg, evalarg, &getnext);
3396 if (p[0] == '<' && p[1] == '<')
3397 type = EXPR_LSHIFT;
3398 else if (p[0] == '>' && p[1] == '>')
3399 type = EXPR_RSHIFT;
3400 else
3401 return OK;
3402
3403 // Handle a bitwise left or right shift operator
3404 if (rettv->v_type != VAR_NUMBER)
3405 {
3406 // left operand should be a number
3407 emsg(_(e_bitshift_ops_must_be_number));
3408 clear_tv(rettv);
3409 return FAIL;
3410 }
3411
3412 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3413 vim9script = in_vim9script();
3414 if (getnext)
3415 {
3416 *arg = eval_next_line(*arg, evalarg);
3417 p = *arg;
3418 }
3419 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3420 {
3421 error_white_both(*arg, 2);
3422 clear_tv(rettv);
3423 return FAIL;
3424 }
3425
3426 /*
3427 * Get the second variable.
3428 */
3429 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3430 {
3431 error_white_both(p, 2);
3432 clear_tv(rettv);
3433 return FAIL;
3434 }
3435 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3436 if (eval6(arg, &var2, evalarg) == FAIL)
3437 {
3438 clear_tv(rettv);
3439 return FAIL;
3440 }
3441
3442 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3443 {
3444 // right operand should be a positive number
3445 if (var2.v_type != VAR_NUMBER)
3446 emsg(_(e_bitshift_ops_must_be_number));
3447 else
dundargocc57b5bc2022-11-02 13:30:51 +00003448 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003449 clear_tv(rettv);
3450 clear_tv(&var2);
3451 return FAIL;
3452 }
3453
3454 if (evaluate)
3455 {
3456 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3457 // shifting more bits than we have always results in zero
3458 rettv->vval.v_number = 0;
3459 else if (type == EXPR_LSHIFT)
3460 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003461 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003462 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003463 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003464 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003465 }
3466
3467 clear_tv(&var2);
3468 }
3469
3470 return OK;
3471}
3472
3473/*
3474 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003475 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003477 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003478 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 *
3480 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003481 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 *
3483 * Return OK or FAIL.
3484 */
3485 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003486eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003489 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003491 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return FAIL;
3493
3494 /*
3495 * Repeat computing, until no '+', '-' or '.' is following.
3496 */
3497 for (;;)
3498 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003499 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003500 int getnext;
3501 char_u *p;
3502 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003503 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003504 int concat;
3505 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003506 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003507
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003508 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003509 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003510 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003511 p = eval_next_non_blank(*arg, evalarg, &getnext);
3512 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003513 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003514 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3515 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003517 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3518 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003519
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003520 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003521 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003522 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003523 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003524 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003525 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003526 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003527 {
mityu4ac198c2021-05-28 17:52:40 +02003528 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003529 clear_tv(rettv);
3530 return FAIL;
3531 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003532 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003533 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003534 if ((op != '+' || (rettv->v_type != VAR_LIST
3535 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003536 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003537 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003538 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003539 int error = FALSE;
3540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003541 // For "list + ...", an illegal use of the first operand as
3542 // a number cannot be determined before evaluating the 2nd
3543 // operand: if this is also a list, all is ok.
3544 // For "something . ...", "something - ..." or "non-list + ...",
3545 // we know that the first operand needs to be a string or number
3546 // without evaluating the 2nd operand. So check before to avoid
3547 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003548 if (op != '.')
3549 tv_get_number_chk(rettv, &error);
3550 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003551 {
3552 clear_tv(rettv);
3553 return FAIL;
3554 }
3555 }
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /*
3558 * Get the second variable.
3559 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003560 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003561 {
mityu89dcb4d2021-05-28 13:50:17 +02003562 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003563 clear_tv(rettv);
3564 return FAIL;
3565 }
3566 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003567 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003569 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return FAIL;
3571 }
3572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003573 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 /*
3576 * Compute the result.
3577 */
3578 if (op == '.')
3579 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003580 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3581 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003582 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003583
Bram Moolenaar2e086612020-08-25 22:37:48 +02003584 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003585 || var2.v_type == VAR_CHANNEL
3586 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003587 semsg(_(e_using_invalid_value_as_string_str),
3588 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003589 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003590 {
3591 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3592 var2.vval.v_float);
3593 s2 = buf2;
3594 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003595 else
3596 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003597 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 {
3599 clear_tv(rettv);
3600 clear_tv(&var2);
3601 return FAIL;
3602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003603 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604 clear_tv(rettv);
3605 rettv->v_type = VAR_STRING;
3606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003608 else if (op == '+' && rettv->v_type == VAR_BLOB
3609 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003611 else if (op == '+' && rettv->v_type == VAR_LIST
3612 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003613 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003615 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else
3618 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003619 int error = FALSE;
3620 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003621 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003623 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003625 f1 = rettv->vval.v_float;
3626 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003627 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003628 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003630 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003631 if (error)
3632 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003633 // This can only happen for "list + non-list" or
3634 // "blob + non-blob". For "non-list + ..." or
3635 // "something - ...", we returned before evaluating the
3636 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003637 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003638 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003639 return FAIL;
3640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003641 if (var2.v_type == VAR_FLOAT)
3642 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003643 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003644 if (var2.v_type == VAR_FLOAT)
3645 {
3646 f2 = var2.vval.v_float;
3647 n2 = 0;
3648 }
3649 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003650 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003651 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003652 if (error)
3653 {
3654 clear_tv(rettv);
3655 clear_tv(&var2);
3656 return FAIL;
3657 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003658 if (rettv->v_type == VAR_FLOAT)
3659 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003663 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003664 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3665 {
3666 if (op == '+')
3667 f1 = f1 + f2;
3668 else
3669 f1 = f1 - f2;
3670 rettv->v_type = VAR_FLOAT;
3671 rettv->vval.v_float = f1;
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003674 {
3675 if (op == '+')
3676 n1 = n1 + n2;
3677 else
3678 n1 = n1 - n2;
3679 rettv->v_type = VAR_NUMBER;
3680 rettv->vval.v_number = n1;
3681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 }
3686 return OK;
3687}
3688
3689/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003690 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 * * number multiplication
3692 * / number division
3693 * % number modulo
3694 *
3695 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003696 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 *
3698 * Return OK or FAIL.
3699 */
3700 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003701eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003702 char_u **arg,
3703 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003704 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003707 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003710 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003712 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return FAIL;
3714
3715 /*
3716 * Repeat computing, until no '*', '/' or '%' is following.
3717 */
3718 for (;;)
3719 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003720 int evaluate;
3721 int getnext;
3722 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003723 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003724 int op;
3725 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003726 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003727 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003728
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003729 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003730 p = eval_next_non_blank(*arg, evalarg, &getnext);
3731 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003732 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003734
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003735 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003736 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003737 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003738 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003739 {
3740 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3741 {
mityu4ac198c2021-05-28 17:52:40 +02003742 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003743 clear_tv(rettv);
3744 return FAIL;
3745 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003746 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003749 f1 = 0;
3750 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003751 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003752 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003754 if (rettv->v_type == VAR_FLOAT)
3755 {
3756 f1 = rettv->vval.v_float;
3757 use_float = TRUE;
3758 n1 = 0;
3759 }
3760 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003761 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (error)
3764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 else
3767 n1 = 0;
3768
3769 /*
3770 * Get the second variable.
3771 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003772 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3773 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003774 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003775 clear_tv(rettv);
3776 return FAIL;
3777 }
3778 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003779 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003782 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003784 if (var2.v_type == VAR_FLOAT)
3785 {
3786 if (!use_float)
3787 {
3788 f1 = n1;
3789 use_float = TRUE;
3790 }
3791 f2 = var2.vval.v_float;
3792 n2 = 0;
3793 }
3794 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003797 clear_tv(&var2);
3798 if (error)
3799 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003800 if (use_float)
3801 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804 /*
3805 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003806 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003808 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003810 if (op == '*')
3811 f1 = f1 * f2;
3812 else if (op == '/')
3813 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003814#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003815 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003816 if (f2 == 0.0)
3817 {
3818 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003819 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003820 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003821 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003822 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003823 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003824 }
3825 else
3826 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003827#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 // We rely on the floating point library to handle divide
3829 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003830 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003831#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003834 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003835 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003836 return FAIL;
3837 }
3838 rettv->v_type = VAR_FLOAT;
3839 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 else
3842 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003843 int failed = FALSE;
3844
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003845 if (op == '*')
3846 n1 = n1 * n2;
3847 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003848 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003850 n1 = num_modulus(n1, n2, &failed);
3851 if (failed)
3852 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003854 rettv->v_type = VAR_NUMBER;
3855 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
3859
3860 return OK;
3861}
3862
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003863/*
3864 * Handle a type cast before a base level expression.
3865 * "arg" must point to the first non-white of the expression.
3866 * "arg" is advanced to just after the recognized expression.
3867 * Return OK or FAIL.
3868 */
3869 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003870eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003871 char_u **arg,
3872 typval_T *rettv,
3873 evalarg_T *evalarg,
3874 int want_string) // after "." operator
3875{
3876 type_T *want_type = NULL;
3877 garray_T type_list; // list of pointers to allocated types
3878 int res;
3879 int evaluate = evalarg == NULL ? 0
3880 : (evalarg->eval_flags & EVAL_EVALUATE);
3881
3882 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003883 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3884 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003885 {
3886 ++*arg;
3887 ga_init2(&type_list, sizeof(type_T *), 10);
3888 want_type = parse_type(arg, &type_list, TRUE);
3889 if (want_type == NULL && (evaluate || **arg != '>'))
3890 {
3891 clear_type_list(&type_list);
3892 return FAIL;
3893 }
3894
3895 if (**arg != '>')
3896 {
3897 if (*skipwhite(*arg) == '>')
3898 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3899 else
3900 emsg(_(e_missing_gt));
3901 clear_type_list(&type_list);
3902 return FAIL;
3903 }
3904 ++*arg;
3905 *arg = skipwhite_and_linebreak(*arg, evalarg);
3906 }
3907
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003908 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003909
3910 if (want_type != NULL && evaluate)
3911 {
3912 if (res == OK)
3913 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003914 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3915 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003916
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003917 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003918 {
3919 if (want_type == &t_bool && actual != &t_bool
3920 && (actual->tt_flags & TTFLAG_BOOL_OK))
3921 {
3922 int n = tv2bool(rettv);
3923
3924 // can use "0" and "1" for boolean in some places
3925 clear_tv(rettv);
3926 rettv->v_type = VAR_BOOL;
3927 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3928 }
3929 else
3930 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003931 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003932
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003933 res = check_type(want_type, actual, TRUE, where);
3934 }
3935 }
3936 }
3937 clear_type_list(&type_list);
3938 }
3939
3940 return res;
3941}
3942
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003943 int
3944eval_leader(char_u **arg, int vim9)
3945{
3946 char_u *s = *arg;
3947 char_u *p = *arg;
3948
3949 while (*p == '!' || *p == '-' || *p == '+')
3950 {
3951 char_u *n = skipwhite(p + 1);
3952
3953 // ++, --, -+ and +- are not accepted in Vim9 script
3954 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3955 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003956 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003957 return FAIL;
3958 }
3959 p = n;
3960 }
3961 *arg = p;
3962 return OK;
3963}
3964
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003966 * Check for a predefined value "true", "false" and "null.*".
3967 * Return OK when recognized.
3968 */
3969 int
3970handle_predefined(char_u *s, int len, typval_T *rettv)
3971{
3972 switch (len)
3973 {
3974 case 4: if (STRNCMP(s, "true", 4) == 0)
3975 {
3976 rettv->v_type = VAR_BOOL;
3977 rettv->vval.v_number = VVAL_TRUE;
3978 return OK;
3979 }
3980 if (STRNCMP(s, "null", 4) == 0)
3981 {
3982 rettv->v_type = VAR_SPECIAL;
3983 rettv->vval.v_number = VVAL_NULL;
3984 return OK;
3985 }
3986 break;
3987 case 5: if (STRNCMP(s, "false", 5) == 0)
3988 {
3989 rettv->v_type = VAR_BOOL;
3990 rettv->vval.v_number = VVAL_FALSE;
3991 return OK;
3992 }
3993 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003994 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3995 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003996#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003997 rettv->v_type = VAR_JOB;
3998 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003999#else
4000 rettv->v_type = VAR_SPECIAL;
4001 rettv->vval.v_number = VVAL_NULL;
4002#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004003 return OK;
4004 }
4005 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004006 case 9:
4007 if (STRNCMP(s, "null_", 5) != 0)
4008 break;
4009 if (STRNCMP(s + 5, "list", 4) == 0)
4010 {
4011 rettv->v_type = VAR_LIST;
4012 rettv->vval.v_list = NULL;
4013 return OK;
4014 }
4015 if (STRNCMP(s + 5, "dict", 4) == 0)
4016 {
4017 rettv->v_type = VAR_DICT;
4018 rettv->vval.v_dict = NULL;
4019 return OK;
4020 }
4021 if (STRNCMP(s + 5, "blob", 4) == 0)
4022 {
4023 rettv->v_type = VAR_BLOB;
4024 rettv->vval.v_blob = NULL;
4025 return OK;
4026 }
4027 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004028 case 10: if (STRNCMP(s, "null_class", 10) == 0)
4029 {
4030 rettv->v_type = VAR_CLASS;
4031 rettv->vval.v_class = NULL;
4032 return OK;
4033 }
4034 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004035 case 11: if (STRNCMP(s, "null_string", 11) == 0)
4036 {
4037 rettv->v_type = VAR_STRING;
4038 rettv->vval.v_string = NULL;
4039 return OK;
4040 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004041 if (STRNCMP(s, "null_object", 11) == 0)
4042 {
4043 rettv->v_type = VAR_OBJECT;
4044 rettv->vval.v_object = NULL;
4045 return OK;
4046 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004047 break;
4048 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004049 if (STRNCMP(s, "null_channel", 12) == 0)
4050 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004052 rettv->v_type = VAR_CHANNEL;
4053 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00004054#else
4055 rettv->v_type = VAR_SPECIAL;
4056 rettv->vval.v_number = VVAL_NULL;
4057#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004058 return OK;
4059 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004060 if (STRNCMP(s, "null_partial", 12) == 0)
4061 {
4062 rettv->v_type = VAR_PARTIAL;
4063 rettv->vval.v_partial = NULL;
4064 return OK;
4065 }
4066 break;
4067 case 13: if (STRNCMP(s, "null_function", 13) == 0)
4068 {
4069 rettv->v_type = VAR_FUNC;
4070 rettv->vval.v_string = NULL;
4071 return OK;
4072 }
4073 break;
4074 }
4075 return FAIL;
4076}
4077
4078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 * Handle sixth level expression:
4080 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004081 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004082 * "string" string constant
4083 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * &option-name option value
4085 * @r register contents
4086 * identifier variable value
4087 * function() function call
4088 * $VAR environment variable
4089 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004090 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004092 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004093 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 *
4095 * Also handle:
4096 * ! in front logical NOT
4097 * - in front unary minus
4098 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004099 * trailing [] subscript in String or List
4100 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004101 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 *
4103 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004104 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 *
4106 * Return OK or FAIL.
4107 */
4108 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004109eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004110 char_u **arg,
4111 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004112 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004115 int evaluate = evalarg != NULL
4116 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 int len;
4118 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004119 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 char_u *start_leader, *end_leader;
4121 int ret = OK;
4122 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004123 static int recurse = 0;
4124 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004128 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004133 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
4135 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004136 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 end_leader = *arg;
4139
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004140 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004141 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004142 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004143 ++*arg;
4144 return FAIL;
4145 }
4146
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004147 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004148 // and crash. With MSVC the stack is smaller.
4149 if (recurse ==
4150#ifdef _MSC_VER
4151 300
4152#else
4153 1000
4154#endif
4155 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004156 {
4157 semsg(_(e_expression_too_recursive_str), *arg);
4158 return FAIL;
4159 }
4160 ++recurse;
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 switch (**arg)
4163 {
4164 /*
4165 * Number constant.
4166 */
4167 case '0':
4168 case '1':
4169 case '2':
4170 case '3':
4171 case '4':
4172 case '5':
4173 case '6':
4174 case '7':
4175 case '8':
4176 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004177 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004178
4179 // Apply prefixed "-" and "+" now. Matters especially when
4180 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004181 if (ret == OK && evaluate && end_leader > start_leader
4182 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004183 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 break;
4185
4186 /*
4187 * String constant: "string".
4188 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004189 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 break;
4191
4192 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004193 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004195 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 break;
4197
4198 /*
4199 * List: [expr, expr]
4200 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004201 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 break;
4203
4204 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004205 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004206 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004207 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004208 {
4209 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4210 }
4211 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004212 {
4213 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004214 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004215 }
4216 else
4217 ret = NOTDONE;
4218 break;
4219
4220 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004221 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004222 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004223 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004224 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004225 ret = NOTDONE;
4226 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004227 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004228 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004229 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004230 break;
4231
4232 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004233 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004235 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 break;
4237
4238 /*
4239 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004240 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
LemonBoy2eaef102022-05-06 13:14:50 +01004242 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4243 ret = eval_interp_string(arg, rettv, evaluate);
4244 else
4245 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247
4248 /*
4249 * Register contents: @r.
4250 */
4251 case '@': ++*arg;
4252 if (evaluate)
4253 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004254 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004255 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004256 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004257 emsg_invreg(**arg);
4258 else
4259 {
4260 rettv->v_type = VAR_STRING;
4261 rettv->vval.v_string = get_reg_contents(**arg,
4262 GREG_EXPR_SRC);
4263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 if (**arg != NUL)
4266 ++*arg;
4267 break;
4268
4269 /*
4270 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004271 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004273 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004274 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004275 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004276 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004277 if (ret == OK && evaluate)
4278 {
4279 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4280
Bram Moolenaara9931532021-06-12 15:58:16 +02004281 // Compile it here to get the return type. The return
4282 // type is optional, when it's missing use t_unknown.
4283 // This is recognized in compile_return().
4284 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4285 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004286 if (compile_def_function(ufunc, FALSE,
4287 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004288 {
4289 clear_tv(rettv);
4290 ret = FAIL;
4291 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004292 }
4293 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004294 if (ret == NOTDONE)
4295 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004296 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004297 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004298
Bram Moolenaar9215f012020-06-27 21:18:00 +02004299 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004300 if (**arg == ')')
4301 ++*arg;
4302 else if (ret == OK)
4303 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004304 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004305 clear_tv(rettv);
4306 ret = FAIL;
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 break;
4310
Bram Moolenaar8c711452005-01-14 21:53:12 +00004311 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 break;
4313 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004314
4315 if (ret == NOTDONE)
4316 {
4317 /*
4318 * Must be a variable or function name.
4319 * Can also be a curly-braces kind of name: {expr}.
4320 */
4321 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004322 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004323 if (alias != NULL)
4324 s = alias;
4325
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004326 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004327 ret = FAIL;
4328 else
4329 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004330 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4331
Bram Moolenaar4525a572022-02-13 11:57:33 +00004332 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004333 {
4334 emsg(_(e_cannot_use_underscore_here));
4335 ret = FAIL;
4336 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004337 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004338 && s[0] == 's' && s[1] == ':')
4339 {
4340 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4341 ret = FAIL;
4342 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004343 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004344 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004345 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004346 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004347 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004348 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004350 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004351 // get the value of "true", "false", etc. or a variable
4352 ret = FAIL;
4353 if (vim9script)
4354 ret = handle_predefined(s, len, rettv);
4355 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004356 {
4357 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004358 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004359 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004360 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004361 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004362 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004363 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004364 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004365 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004366 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004369 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 }
4371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4373 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004374 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004375 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 /*
4378 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4379 */
4380 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004381 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004382
4383 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004384 return ret;
4385}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004387/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004388 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004389 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004390 * Adjusts "end_leaderp" until it is at "start_leader".
4391 */
4392 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004393eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004394 typval_T *rettv,
4395 int numeric_only,
4396 char_u *start_leader,
4397 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004398{
4399 char_u *end_leader = *end_leaderp;
4400 int ret = OK;
4401 int error = FALSE;
4402 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004403 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004404 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004405 float_T f = 0.0;
4406
4407 if (rettv->v_type == VAR_FLOAT)
4408 f = rettv->vval.v_float;
4409 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004410 {
4411 while (VIM_ISWHITE(end_leader[-1]))
4412 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004413 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004414 val = tv2bool(rettv);
4415 else
4416 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004417 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004418 if (error)
4419 {
4420 clear_tv(rettv);
4421 ret = FAIL;
4422 }
4423 else
4424 {
4425 while (end_leader > start_leader)
4426 {
4427 --end_leader;
4428 if (*end_leader == '!')
4429 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004430 if (numeric_only)
4431 {
4432 ++end_leader;
4433 break;
4434 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004435 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004436 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004437 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004438 {
4439 rettv->v_type = VAR_BOOL;
4440 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4441 }
4442 else
4443 f = !f;
4444 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004445 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004446 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004447 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004448 type = VAR_BOOL;
4449 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004450 }
4451 else if (*end_leader == '-')
4452 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004453 if (rettv->v_type == VAR_FLOAT)
4454 f = -f;
4455 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004456 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004457 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004458 type = VAR_NUMBER;
4459 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004460 }
4461 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004462 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004464 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004465 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467 else
4468 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004469 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004470 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004471 rettv->v_type = type;
4472 else
4473 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004474 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004477 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return ret;
4479}
4480
4481/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004482 * Call the function referred to in "rettv".
4483 */
4484 static int
4485call_func_rettv(
4486 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004487 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004488 typval_T *rettv,
4489 int evaluate,
4490 dict_T *selfdict,
4491 typval_T *basetv)
4492{
4493 partial_T *pt = NULL;
4494 funcexe_T funcexe;
4495 typval_T functv;
4496 char_u *s;
4497 int ret;
4498
4499 // need to copy the funcref so that we can clear rettv
4500 if (evaluate)
4501 {
4502 functv = *rettv;
4503 rettv->v_type = VAR_UNKNOWN;
4504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004505 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004506 if (functv.v_type == VAR_PARTIAL)
4507 {
4508 pt = functv.vval.v_partial;
4509 s = partial_name(pt);
4510 }
4511 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004512 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004513 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004514 if (s == NULL || *s == NUL)
4515 {
4516 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004517 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004518 goto theend;
4519 }
4520 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004521 }
4522 else
4523 s = (char_u *)"";
4524
Bram Moolenaara80faa82020-04-12 19:37:17 +02004525 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004526 funcexe.fe_firstline = curwin->w_cursor.lnum;
4527 funcexe.fe_lastline = curwin->w_cursor.lnum;
4528 funcexe.fe_evaluate = evaluate;
4529 funcexe.fe_partial = pt;
4530 funcexe.fe_selfdict = selfdict;
4531 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004532 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004533
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004534theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // Clear the funcref afterwards, so that deleting it while
4536 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004537 if (evaluate)
4538 clear_tv(&functv);
4539
4540 return ret;
4541}
4542
4543/*
4544 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004545 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004546 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4547 */
4548 static int
4549eval_lambda(
4550 char_u **arg,
4551 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004552 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004554{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004555 int evaluate = evalarg != NULL
4556 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004557 typval_T base = *rettv;
4558 int ret;
4559
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004560 rettv->v_type = VAR_UNKNOWN;
4561
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004562 if (**arg == '{')
4563 {
4564 // ->{lambda}()
4565 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4566 }
4567 else
4568 {
4569 // ->(lambda)()
4570 ++*arg;
4571 ret = eval1(arg, rettv, evalarg);
4572 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004573 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004574 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004575 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004576 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004577 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004578 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4579 && rettv->v_type != VAR_PARTIAL)
4580 {
4581 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4582 return FAIL;
4583 }
4584 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004585 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004586 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004587 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004588
4589 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004590 {
4591 if (verbose)
4592 {
4593 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004594 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004595 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004596 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004597 }
4598 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004599 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004600 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004601 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004602 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004603
4604 // Clear the funcref afterwards, so that deleting it while
4605 // evaluating the arguments is possible (see test55).
4606 if (evaluate)
4607 clear_tv(&base);
4608
4609 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004610}
4611
4612/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004613 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004614 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004615 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4616 */
4617 static int
4618eval_method(
4619 char_u **arg,
4620 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004621 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004623{
4624 char_u *name;
4625 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004626 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004627 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004628 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004629 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004630 int evaluate = evalarg != NULL
4631 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004632
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004633 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004634
Bram Moolenaarac92e252019-08-03 21:58:38 +02004635 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004636 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004637 if (alias != NULL)
4638 name = alias;
4639
4640 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004641 {
4642 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004643 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004644 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004645 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004646 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004647 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004648 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004649
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004650 // If there is no "(" immediately following, but there is further on,
4651 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4652 // Does not handle anything where "(" is part of the expression.
4653 *arg = skipwhite(*arg);
4654
4655 if (**arg != '(' && alias == NULL
4656 && (paren = vim_strchr(*arg, '(')) != NULL)
4657 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004658 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004659
4660 // Truncate the name a the "(". Avoid trying to get another line
4661 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004662 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004663 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4664 if (evalarg != NULL)
4665 {
4666 getline = evalarg->eval_getline;
4667 evalarg->eval_getline = NULL;
4668 }
4669
4670 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004671 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004672 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004673 *arg = name + len;
4674 ret = FAIL;
4675 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004676 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004677 {
4678 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004679 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004680 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004681
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004682 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004683 if (getline != NULL)
4684 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004685 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004686
4687 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004688 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004689 *arg = skipwhite(*arg);
4690
4691 if (**arg != '(')
4692 {
4693 if (verbose)
4694 semsg(_(e_missing_parenthesis_str), name);
4695 ret = FAIL;
4696 }
4697 else if (VIM_ISWHITE((*arg)[-1]))
4698 {
4699 if (verbose)
4700 emsg(_(e_no_white_space_allowed_before_parenthesis));
4701 ret = FAIL;
4702 }
4703 else
4704 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004705 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004706 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004707 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004708
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004709 // Clear the funcref afterwards, so that deleting it while
4710 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004711 if (evaluate)
4712 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004713 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004714
Bram Moolenaarac92e252019-08-03 21:58:38 +02004715 return ret;
4716}
4717
4718/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004719 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4720 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4722 */
4723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004724eval_index(
4725 char_u **arg,
4726 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004727 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004730 int evaluate = evalarg != NULL
4731 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004733 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004736 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004737 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004739 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4740 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004742 init_tv(&var1);
4743 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 /*
4747 * dict.name
4748 */
4749 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004750 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004752 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004754 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 }
4756 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 /*
4759 * something[idx]
4760 *
4761 * Get the (first) variable from inside the [].
4762 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004763 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 if (**arg == ':')
4765 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004766 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004767 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004768 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004769 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004770 semsg(_(e_white_space_required_before_and_after_str_at_str),
4771 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004772 clear_tv(&var1);
4773 return FAIL;
4774 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004775 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004777 int error = FALSE;
4778
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004779 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004780 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004781 && var1.v_type == VAR_FLOAT)
4782 {
4783 var1.vval.v_string = typval_tostring(&var1, TRUE);
4784 var1.v_type = VAR_STRING;
4785 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004786
Bram Moolenaar4525a572022-02-13 11:57:33 +00004787 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004788 tv_get_number_chk(&var1, &error);
4789 else
4790 error = tv_get_string_chk(&var1) == NULL;
4791 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004792 {
4793 // not a number or string
4794 clear_tv(&var1);
4795 return FAIL;
4796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798
4799 /*
4800 * Get the second variable from inside the [:].
4801 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004802 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 if (**arg == ':')
4804 {
4805 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004806 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004807 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004808 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004809 semsg(_(e_white_space_required_before_and_after_str_at_str),
4810 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004811 if (!empty1)
4812 clear_tv(&var1);
4813 return FAIL;
4814 }
4815 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (**arg == ']')
4817 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004818 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 if (!empty1)
4821 clear_tv(&var1);
4822 return FAIL;
4823 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004824 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004826 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 if (!empty1)
4828 clear_tv(&var1);
4829 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830 return FAIL;
4831 }
4832 }
4833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004835 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004836 if (**arg != ']')
4837 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004838 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004839 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 clear_tv(&var1);
4841 if (range)
4842 clear_tv(&var2);
4843 return FAIL;
4844 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004845 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 }
4847
4848 if (evaluate)
4849 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004850 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004851 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004852 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004853
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004854 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004857 clear_tv(&var2);
4858 return res;
4859 }
4860 return OK;
4861}
4862
4863/*
4864 * Check if "rettv" can have an [index] or [sli:ce]
4865 */
4866 int
4867check_can_index(typval_T *rettv, int evaluate, int verbose)
4868{
4869 switch (rettv->v_type)
4870 {
4871 case VAR_FUNC:
4872 case VAR_PARTIAL:
4873 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004874 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004875 return FAIL;
4876 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004877 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004878 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004879 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004880 case VAR_BOOL:
4881 case VAR_SPECIAL:
4882 case VAR_JOB:
4883 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004884 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004885 case VAR_CLASS:
4886 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004887 if (verbose)
4888 emsg(_(e_cannot_index_special_variable));
4889 return FAIL;
4890 case VAR_UNKNOWN:
4891 case VAR_ANY:
4892 case VAR_VOID:
4893 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004895 emsg(_(e_cannot_index_special_variable));
4896 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004898 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004900 case VAR_STRING:
4901 case VAR_LIST:
4902 case VAR_DICT:
4903 case VAR_BLOB:
4904 break;
4905 case VAR_NUMBER:
4906 if (in_vim9script())
4907 emsg(_(e_cannot_index_number));
4908 break;
4909 }
4910 return OK;
4911}
4912
4913/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004914 * slice() function
4915 */
4916 void
4917f_slice(typval_T *argvars, typval_T *rettv)
4918{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004919 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004920 && ((argvars[0].v_type != VAR_STRING
4921 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004922 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004923 && check_for_list_arg(argvars, 0) == FAIL)
4924 || check_for_number_arg(argvars, 1) == FAIL
4925 || check_for_opt_number_arg(argvars, 2) == FAIL))
4926 return;
4927
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004928 if (check_can_index(argvars, TRUE, FALSE) != OK)
4929 return;
4930
4931 copy_tv(argvars, rettv);
4932 eval_index_inner(rettv, TRUE, argvars + 1,
4933 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4934 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004935}
4936
4937/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004938 * Apply index or range to "rettv".
4939 * "var1" is the first index, NULL for [:expr].
4940 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004941 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4942 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004943 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4944 */
4945 int
4946eval_index_inner(
4947 typval_T *rettv,
4948 int is_range,
4949 typval_T *var1,
4950 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004951 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004952 char_u *key,
4953 int keylen,
4954 int verbose)
4955{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004956 varnumber_T n1, n2 = 0;
4957 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004958
4959 n1 = 0;
4960 if (var1 != NULL && rettv->v_type != VAR_DICT)
4961 n1 = tv_get_number(var1);
4962
4963 if (is_range)
4964 {
4965 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004967 if (verbose)
4968 emsg(_(e_cannot_slice_dictionary));
4969 return FAIL;
4970 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004971 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004972 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004973 else
4974 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004975 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004976
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004977 switch (rettv->v_type)
4978 {
4979 case VAR_UNKNOWN:
4980 case VAR_ANY:
4981 case VAR_VOID:
4982 case VAR_FUNC:
4983 case VAR_PARTIAL:
4984 case VAR_FLOAT:
4985 case VAR_BOOL:
4986 case VAR_SPECIAL:
4987 case VAR_JOB:
4988 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004989 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004990 case VAR_CLASS:
4991 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004992 break; // not evaluating, skipping over subscript
4993
4994 case VAR_NUMBER:
4995 case VAR_STRING:
4996 {
4997 char_u *s = tv_get_string(rettv);
4998
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004999 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01005000 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005001 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005002 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005003 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005004 else
5005 s = char_from_string(s, n1);
5006 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 // The resulting variable is a substring. If the indexes
5010 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 if (n1 < 0)
5012 {
5013 n1 = len + n1;
5014 if (n1 < 0)
5015 n1 = 0;
5016 }
5017 if (n2 < 0)
5018 n2 = len + n2;
5019 else if (n2 >= len)
5020 n2 = len;
5021 if (n1 >= len || n2 < 0 || n1 > n2)
5022 s = NULL;
5023 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02005024 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005025 }
5026 else
5027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // The resulting variable is a string of a single
5029 // character. If the index is too big or negative the
5030 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005031 if (n1 >= len || n1 < 0)
5032 s = NULL;
5033 else
5034 s = vim_strnsave(s + n1, 1);
5035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 clear_tv(rettv);
5037 rettv->v_type = VAR_STRING;
5038 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005039 }
5040 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005041
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005042 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005043 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
5044 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005045 break;
5046
5047 case VAR_LIST:
5048 if (var1 == NULL)
5049 n1 = 0;
5050 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01005051 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005052 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01005053 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005054 return FAIL;
5055 break;
5056
5057 case VAR_DICT:
5058 {
5059 dictitem_T *item;
5060 typval_T tmp;
5061
5062 if (key == NULL)
5063 {
5064 key = tv_get_string_chk(var1);
5065 if (key == NULL)
5066 return FAIL;
5067 }
5068
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005069 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005070
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005071 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005072 {
5073 if (verbose)
5074 {
5075 if (keylen > 0)
5076 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005077 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005078 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005079 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005080 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081
5082 copy_tv(&item->di_tv, &tmp);
5083 clear_tv(rettv);
5084 *rettv = tmp;
5085 }
5086 break;
5087 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 return OK;
5089}
5090
5091/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005092 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005093 */
5094 char_u *
5095partial_name(partial_T *pt)
5096{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005097 if (pt != NULL)
5098 {
5099 if (pt->pt_name != NULL)
5100 return pt->pt_name;
5101 if (pt->pt_func != NULL)
5102 return pt->pt_func->uf_name;
5103 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005104 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005105}
5106
Bram Moolenaarddecc252016-04-06 22:59:37 +02005107 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005108partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005109{
5110 int i;
5111
5112 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005113 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005114 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005115 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005116 if (pt->pt_name != NULL)
5117 {
5118 func_unref(pt->pt_name);
5119 vim_free(pt->pt_name);
5120 }
5121 else
5122 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005123
Bram Moolenaar54656012021-06-09 20:50:46 +02005124 // "out_up" is no longer used, decrement refcount on partial that owns it.
5125 partial_unref(pt->pt_outer.out_up_partial);
5126
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005127 // Using pt_outer from another partial.
5128 partial_unref(pt->pt_outer_partial);
5129
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005130 // Decrease the reference count for the context of a closure. If down
5131 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005132 if (pt->pt_funcstack != NULL)
5133 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005134 --pt->pt_funcstack->fs_refcount;
5135 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005136 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005137 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005138 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5139 if (pt->pt_loopvars[i] != NULL)
5140 {
5141 --pt->pt_loopvars[i]->lvs_refcount;
5142 loopvars_check_refcount(pt->pt_loopvars[i]);
5143 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005144
Bram Moolenaarddecc252016-04-06 22:59:37 +02005145 vim_free(pt);
5146}
5147
5148/*
5149 * Unreference a closure: decrement the reference count and free it when it
5150 * becomes zero.
5151 */
5152 void
5153partial_unref(partial_T *pt)
5154{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005155 if (pt == NULL)
5156 return;
5157
5158 int done = FALSE;
5159
5160 if (--pt->pt_refcount <= 0)
5161 partial_free(pt);
5162
5163 // If the reference count goes down to one, the funcstack may be the
5164 // only reference and can be freed if no other partials reference it.
5165 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005166 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005167 // careful: if the funcstack is freed it may contain this partial
5168 // and it gets freed as well
5169 if (pt->pt_funcstack != NULL)
5170 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005171
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005172 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005173 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005174 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005175
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005176 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5177 if (pt->pt_loopvars[depth] != NULL
5178 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005179 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005180 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005181 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005182}
5183
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005184/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005185 * Return the next (unique) copy ID.
5186 * Used for serializing nested structures.
5187 */
5188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005189get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005190{
5191 current_copyID += COPYID_INC;
5192 return current_copyID;
5193}
5194
5195/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196 * Garbage collection for lists and dictionaries.
5197 *
5198 * We use reference counts to be able to free most items right away when they
5199 * are no longer used. But for composite items it's possible that it becomes
5200 * unused while the reference count is > 0: When there is a recursive
5201 * reference. Example:
5202 * :let l = [1, 2, 3]
5203 * :let d = {9: l}
5204 * :let l[1] = d
5205 *
5206 * Since this is quite unusual we handle this with garbage collection: every
5207 * once in a while find out which lists and dicts are not referenced from any
5208 * variable.
5209 *
5210 * Here is a good reference text about garbage collection (refers to Python
5211 * but it applies to all reference-counting mechanisms):
5212 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005213 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005214
5215/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005216 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005217 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005218 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005219 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005220 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005221garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005222{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005223 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005224 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005225 buf_T *buf;
5226 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005227 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005228 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005230 if (!testing)
5231 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005233 want_garbage_collect = FALSE;
5234 may_garbage_collect = FALSE;
5235 garbage_collect_at_exit = FALSE;
5236 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005237
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005238 // The execution stack can grow big, limit the size.
5239 if (exestack.ga_maxlen - exestack.ga_len > 500)
5240 {
5241 size_t new_len;
5242 char_u *pp;
5243 int n;
5244
5245 // Keep 150% of the current size, with a minimum of the growth size.
5246 n = exestack.ga_len / 2;
5247 if (n < exestack.ga_growsize)
5248 n = exestack.ga_growsize;
5249
5250 // Don't make it bigger though.
5251 if (exestack.ga_len + n < exestack.ga_maxlen)
5252 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005253 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005254 pp = vim_realloc(exestack.ga_data, new_len);
5255 if (pp == NULL)
5256 return FAIL;
5257 exestack.ga_maxlen = exestack.ga_len + n;
5258 exestack.ga_data = pp;
5259 }
5260 }
5261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 // We advance by two because we add one for items referenced through
5263 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005264 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005265
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005266 /*
5267 * 1. Go through all accessible variables and mark all lists and dicts
5268 * with copyID.
5269 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 // Don't free variables in the previous_funccal list unless they are only
5272 // referenced through previous_funccal. This must be first, because if
5273 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005277 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005280 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005281 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5282 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005285 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5287 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005288 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005289 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005290 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005291 abort = abort || set_ref_in_item(
5292 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005293#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005294 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005295 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5296 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005297 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005298 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005299 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5300 NULL, NULL);
5301#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005304 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5306 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005308 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005314 abort = abort || set_ref_in_functions(copyID);
5315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005318
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005319 // funcstacks keep variables for closures
5320 abort = abort || set_ref_in_funcstacks(copyID);
5321
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005322 // loopvars keep variables for loop blocks
5323 abort = abort || set_ref_in_loopvars(copyID);
5324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005326 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005327
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005328 // callbacks in buffers
5329 abort = abort || set_ref_in_buffers(copyID);
5330
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005331 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5332 abort = abort || set_ref_in_insexpand_funcs(copyID);
5333
5334 // 'operatorfunc' callback
5335 abort = abort || set_ref_in_opfunc(copyID);
5336
5337 // 'tagfunc' callback
5338 abort = abort || set_ref_in_tagfunc(copyID);
5339
5340 // 'imactivatefunc' and 'imstatusfunc' callbacks
5341 abort = abort || set_ref_in_im_funcs(copyID);
5342
Bram Moolenaar1dced572012-04-05 16:54:08 +02005343#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005344 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005345#endif
5346
Bram Moolenaardb913952012-06-29 12:54:53 +02005347#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005349#endif
5350
5351#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005352 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005353#endif
5354
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005355#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005356 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005357 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005358#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005359#ifdef FEAT_NETBEANS_INTG
5360 abort = abort || set_ref_in_nb_channel(copyID);
5361#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005362
Bram Moolenaare3188e22016-05-31 21:13:04 +02005363#ifdef FEAT_TIMERS
5364 abort = abort || set_ref_in_timer(copyID);
5365#endif
5366
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005367#ifdef FEAT_QUICKFIX
5368 abort = abort || set_ref_in_quickfix(copyID);
5369#endif
5370
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005371#ifdef FEAT_TERMINAL
5372 abort = abort || set_ref_in_term(copyID);
5373#endif
5374
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005375#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005376 abort = abort || set_ref_in_popups(copyID);
5377#endif
5378
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005379 abort = abort || set_ref_in_classes(copyID);
5380
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005381 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005382 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005383 /*
5384 * 2. Free lists and dictionaries that are not referenced.
5385 */
5386 did_free = free_unref_items(copyID);
5387
5388 /*
5389 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005390 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005391 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005393 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 else if (p_verbose > 0)
5395 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005396 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005397 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005398
5399 return did_free;
5400}
5401
5402/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005403 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005404 */
5405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005406free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005407{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005408 int did_free = FALSE;
5409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 // Let all "free" functions know that we are here. This means no
5411 // dictionaries, lists, channels or jobs are to be freed, because we will
5412 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005413 in_free_unref_items = TRUE;
5414
5415 /*
5416 * PASS 1: free the contents of the items. We don't free the items
5417 * themselves yet, so that it is possible to decrement refcount counters
5418 */
5419
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005420 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005421 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005422
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005423 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005424 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005426 // Go through the list of objects and free items without this copyID.
5427 did_free |= object_free_nonref(copyID);
5428
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005429 // Go through the list of classes and free items without this copyID.
5430 did_free |= class_free_nonref(copyID);
5431
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 // Go through the list of jobs and free items without the copyID. This
5434 // must happen before doing channels, because jobs refer to channels, but
5435 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005436 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005438 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005439 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5440#endif
5441
5442 /*
5443 * PASS 2: free the items themselves.
5444 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005445 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005446 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005447
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005448#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // Go through the list of jobs and free items without the copyID. This
5450 // must happen before doing channels, because jobs refer to channels, but
5451 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 free_unused_jobs(copyID, COPYID_MASK);
5453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005455 free_unused_channels(copyID, COPYID_MASK);
5456#endif
5457
5458 in_free_unref_items = FALSE;
5459
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005460 return did_free;
5461}
5462
5463/*
5464 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005465 * "list_stack" is used to add lists to be marked. Can be NULL.
5466 *
5467 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005470set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005471{
5472 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005473 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005474 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005475 hashtab_T *cur_ht;
5476 ht_stack_T *ht_stack = NULL;
5477 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 cur_ht = ht;
5480 for (;;)
5481 {
5482 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005483 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // Mark each item in the hashtab. If the item contains a hashtab
5485 // it is added to ht_stack, if it contains a list it is added to
5486 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005488 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 if (!HASHITEM_EMPTY(hi))
5490 {
5491 --todo;
5492 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5493 &ht_stack, list_stack);
5494 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005495 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496
5497 if (ht_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_ht = ht_stack->ht;
5502 tempitem = ht_stack;
5503 ht_stack = ht_stack->prev;
5504 free(tempitem);
5505 }
5506
5507 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005508}
5509
Dominique Pelle748b3082022-01-08 12:41:16 +00005510#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5511 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005512/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005513 * Mark a dict and its items with "copyID".
5514 * Returns TRUE if setting references failed somehow.
5515 */
5516 int
5517set_ref_in_dict(dict_T *d, int copyID)
5518{
5519 if (d != NULL && d->dv_copyID != copyID)
5520 {
5521 d->dv_copyID = copyID;
5522 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5523 }
5524 return FALSE;
5525}
Dominique Pelle748b3082022-01-08 12:41:16 +00005526#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005527
5528/*
5529 * Mark a list and its items with "copyID".
5530 * Returns TRUE if setting references failed somehow.
5531 */
5532 int
5533set_ref_in_list(list_T *ll, int copyID)
5534{
5535 if (ll != NULL && ll->lv_copyID != copyID)
5536 {
5537 ll->lv_copyID = copyID;
5538 return set_ref_in_list_items(ll, copyID, NULL);
5539 }
5540 return FALSE;
5541}
5542
5543/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005544 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005545 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5546 *
5547 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005548 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005549 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005550set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005551{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 listitem_T *li;
5553 int abort = FALSE;
5554 list_T *cur_l;
5555 list_stack_T *list_stack = NULL;
5556 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005557
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005558 cur_l = l;
5559 for (;;)
5560 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005561 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 // Mark each item in the list. If the item contains a hashtab
5563 // it is added to ht_stack, if it contains a list it is added to
5564 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005565 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5566 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5567 ht_stack, &list_stack);
5568 if (list_stack == NULL)
5569 break;
5570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 cur_l = list_stack->list;
5573 tempitem = list_stack;
5574 list_stack = list_stack->prev;
5575 free(tempitem);
5576 }
5577
5578 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005579}
5580
5581/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005582 * Mark the partial in callback 'cb' with "copyID".
5583 */
5584 int
5585set_ref_in_callback(callback_T *cb, int copyID)
5586{
5587 typval_T tv;
5588
5589 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5590 return FALSE;
5591
5592 tv.v_type = VAR_PARTIAL;
5593 tv.vval.v_partial = cb->cb_partial;
5594 return set_ref_in_item(&tv, copyID, NULL, NULL);
5595}
5596
5597/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005598 * Mark the dict "dd" with "copyID".
5599 * Also see set_ref_in_item().
5600 */
5601 static int
5602set_ref_in_item_dict(
5603 dict_T *dd,
5604 int copyID,
5605 ht_stack_T **ht_stack,
5606 list_stack_T **list_stack)
5607{
5608 if (dd == NULL || dd->dv_copyID == copyID)
5609 return FALSE;
5610
5611 // Didn't see this dict yet.
5612 dd->dv_copyID = copyID;
5613 if (ht_stack == NULL)
5614 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5615
5616 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5617 if (newitem == NULL)
5618 return TRUE;
5619
5620 newitem->ht = &dd->dv_hashtab;
5621 newitem->prev = *ht_stack;
5622 *ht_stack = newitem;
5623
5624 return FALSE;
5625}
5626
5627/*
5628 * Mark the list "ll" with "copyID".
5629 * Also see set_ref_in_item().
5630 */
5631 static int
5632set_ref_in_item_list(
5633 list_T *ll,
5634 int copyID,
5635 ht_stack_T **ht_stack,
5636 list_stack_T **list_stack)
5637{
5638 if (ll == NULL || ll->lv_copyID == copyID)
5639 return FALSE;
5640
5641 // Didn't see this list yet.
5642 ll->lv_copyID = copyID;
5643 if (list_stack == NULL)
5644 return set_ref_in_list_items(ll, copyID, ht_stack);
5645
5646 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5647 if (newitem == NULL)
5648 return TRUE;
5649
5650 newitem->list = ll;
5651 newitem->prev = *list_stack;
5652 *list_stack = newitem;
5653
5654 return FALSE;
5655}
5656
5657/*
5658 * Mark the partial "pt" with "copyID".
5659 * Also see set_ref_in_item().
5660 */
5661 static int
5662set_ref_in_item_partial(
5663 partial_T *pt,
5664 int copyID,
5665 ht_stack_T **ht_stack,
5666 list_stack_T **list_stack)
5667{
5668 if (pt == NULL || pt->pt_copyID == copyID)
5669 return FALSE;
5670
5671 // Didn't see this partial yet.
5672 pt->pt_copyID = copyID;
5673
5674 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5675
5676 if (pt->pt_dict != NULL)
5677 {
5678 typval_T dtv;
5679
5680 dtv.v_type = VAR_DICT;
5681 dtv.vval.v_dict = pt->pt_dict;
5682 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5683 }
5684
5685 for (int i = 0; i < pt->pt_argc; ++i)
5686 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5687 ht_stack, list_stack);
5688 // pt_funcstack is handled in set_ref_in_funcstacks()
5689 // pt_loopvars is handled in set_ref_in_loopvars()
5690
5691 return abort;
5692}
5693
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005694#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005695/*
5696 * Mark the job "pt" with "copyID".
5697 * Also see set_ref_in_item().
5698 */
5699 static int
5700set_ref_in_item_job(
5701 job_T *job,
5702 int copyID,
5703 ht_stack_T **ht_stack,
5704 list_stack_T **list_stack)
5705{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005706 typval_T dtv;
5707
5708 if (job == NULL || job->jv_copyID == copyID)
5709 return FALSE;
5710
5711 job->jv_copyID = copyID;
5712 if (job->jv_channel != NULL)
5713 {
5714 dtv.v_type = VAR_CHANNEL;
5715 dtv.vval.v_channel = job->jv_channel;
5716 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5717 }
5718 if (job->jv_exit_cb.cb_partial != NULL)
5719 {
5720 dtv.v_type = VAR_PARTIAL;
5721 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5722 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5723 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005724
5725 return FALSE;
5726}
5727
5728/*
5729 * Mark the channel "ch" with "copyID".
5730 * Also see set_ref_in_item().
5731 */
5732 static int
5733set_ref_in_item_channel(
5734 channel_T *ch,
5735 int copyID,
5736 ht_stack_T **ht_stack,
5737 list_stack_T **list_stack)
5738{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005739 typval_T dtv;
5740
5741 if (ch == NULL || ch->ch_copyID == copyID)
5742 return FALSE;
5743
5744 ch->ch_copyID = copyID;
5745 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5746 {
5747 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5748 jq != NULL; jq = jq->jq_next)
5749 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5750 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5751 cq = cq->cq_next)
5752 if (cq->cq_callback.cb_partial != NULL)
5753 {
5754 dtv.v_type = VAR_PARTIAL;
5755 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5756 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5757 }
5758 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5759 {
5760 dtv.v_type = VAR_PARTIAL;
5761 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5762 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5763 }
5764 }
5765 if (ch->ch_callback.cb_partial != NULL)
5766 {
5767 dtv.v_type = VAR_PARTIAL;
5768 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5769 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5770 }
5771 if (ch->ch_close_cb.cb_partial != NULL)
5772 {
5773 dtv.v_type = VAR_PARTIAL;
5774 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5775 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5776 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005777
5778 return FALSE;
5779}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005780#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005781
5782/*
5783 * Mark the class "cl" with "copyID".
5784 * Also see set_ref_in_item().
5785 */
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005786 int
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005787set_ref_in_item_class(
5788 class_T *cl,
5789 int copyID,
5790 ht_stack_T **ht_stack,
5791 list_stack_T **list_stack)
5792{
5793 int abort = FALSE;
5794
Yegappan Lakshmanane651e112023-09-04 07:51:01 +02005795 if (cl == NULL || cl->class_copyID == copyID)
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005796 return FALSE;
5797
5798 cl->class_copyID = copyID;
Yegappan Lakshmanan544be0d2023-09-04 22:14:28 +02005799 if (cl->class_members_tv != NULL)
5800 {
5801 // The "class_members_tv" table is allocated only for regular classes
5802 // and not for interfaces.
5803 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5804 abort = abort || set_ref_in_item(
5805 &cl->class_members_tv[i],
5806 copyID, ht_stack, list_stack);
5807 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005808
5809 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5810 abort = abort || set_ref_in_func(NULL,
5811 cl->class_class_functions[i], copyID);
5812
5813 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5814 abort = abort || set_ref_in_func(NULL,
5815 cl->class_obj_methods[i], copyID);
5816
5817 return abort;
5818}
5819
5820/*
5821 * Mark the object "cl" with "copyID".
5822 * Also see set_ref_in_item().
5823 */
5824 static int
5825set_ref_in_item_object(
5826 object_T *obj,
5827 int copyID,
5828 ht_stack_T **ht_stack,
5829 list_stack_T **list_stack)
5830{
5831 int abort = FALSE;
5832
5833 if (obj == NULL || obj->obj_copyID == copyID)
5834 return FALSE;
5835
5836 obj->obj_copyID = copyID;
5837
5838 // The typval_T array is right after the object_T.
5839 typval_T *mtv = (typval_T *)(obj + 1);
5840 for (int i = 0; !abort
5841 && i < obj->obj_class->class_obj_member_count; ++i)
5842 abort = abort || set_ref_in_item(mtv + i, copyID,
5843 ht_stack, list_stack);
5844
5845 return abort;
5846}
5847
5848/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005849 * Mark all lists, dicts and other container types referenced through typval
5850 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005851 * "list_stack" is used to add lists to be marked. Can be NULL.
5852 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5853 *
5854 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005855 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857set_ref_in_item(
5858 typval_T *tv,
5859 int copyID,
5860 ht_stack_T **ht_stack,
5861 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005862{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005863 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005864
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005865 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005866 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005867 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005868 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5869 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005870
5871 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005872 return set_ref_in_item_list(tv->vval.v_list, copyID,
5873 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005874
5875 case VAR_FUNC:
5876 {
5877 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5878 break;
5879 }
5880
5881 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005882 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5883 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005884
5885 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005886#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005887 return set_ref_in_item_job(tv->vval.v_job, copyID,
5888 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005889#else
5890 break;
5891#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005892
5893 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005894#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005895 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005896 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005897#else
5898 break;
5899#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005900
5901 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005902 return set_ref_in_item_class(tv->vval.v_class, copyID,
5903 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005904
5905 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005906 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005907 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005908
5909 case VAR_UNKNOWN:
5910 case VAR_ANY:
5911 case VAR_VOID:
5912 case VAR_BOOL:
5913 case VAR_SPECIAL:
5914 case VAR_NUMBER:
5915 case VAR_FLOAT:
5916 case VAR_STRING:
5917 case VAR_BLOB:
5918 case VAR_INSTR:
5919 // Types that do not contain any other item
5920 break;
5921 }
5922
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005923 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005924}
5925
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 * Return a string with the string representation of a variable.
5928 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005931 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005932 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005933 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005934 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5935 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005936 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005938 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005939echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005940 typval_T *tv,
5941 char_u **tofree,
5942 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005943 int copyID,
5944 int echo_style,
5945 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005946 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 static int recurse = 0;
5949 char_u *r = NULL;
5950
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005953 if (!did_echo_string_emsg)
5954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005955 // Only give this message once for a recursive call to avoid
5956 // flooding the user with errors. And stop iterating over lists
5957 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005958 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005959 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005962 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005963 }
5964 ++recurse;
5965
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 switch (tv->v_type)
5967 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005968 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005969 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005970 {
5971 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005972 r = tv->vval.v_string;
5973 if (r == NULL)
5974 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005975 }
5976 else
5977 {
5978 *tofree = string_quote(tv->vval.v_string, FALSE);
5979 r = *tofree;
5980 }
5981 break;
5982
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005984 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005985 char_u buf[MAX_FUNC_NAME_LEN];
5986
5987 if (echo_style)
5988 {
LemonBoya5d35902022-04-29 21:15:02 +01005989 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5990 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005991 buf, MAX_FUNC_NAME_LEN);
5992 if (r == buf)
5993 {
5994 r = vim_strsave(buf);
5995 *tofree = r;
5996 }
5997 else
5998 *tofree = NULL;
5999 }
6000 else
6001 {
6002 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
6003 : make_ufunc_name_readable(
6004 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
6005 TRUE);
6006 r = *tofree;
6007 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006010
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006011 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006012 {
6013 partial_T *pt = tv->vval.v_partial;
6014 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006015 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006016 garray_T ga;
6017 int i;
6018 char_u *tf;
6019
6020 ga_init2(&ga, 1, 100);
6021 ga_concat(&ga, (char_u *)"function(");
6022 if (fname != NULL)
6023 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006024 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00006025 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00006026 && vim_isupper(fname[1]))
6027 {
6028 ga_concat(&ga, (char_u *)"'g:");
6029 ga_concat(&ga, fname + 1);
6030 }
6031 else
6032 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006033 vim_free(fname);
6034 }
6035 if (pt != NULL && pt->pt_argc > 0)
6036 {
6037 ga_concat(&ga, (char_u *)", [");
6038 for (i = 0; i < pt->pt_argc; ++i)
6039 {
6040 if (i > 0)
6041 ga_concat(&ga, (char_u *)", ");
6042 ga_concat(&ga,
6043 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6044 vim_free(tf);
6045 }
6046 ga_concat(&ga, (char_u *)"]");
6047 }
6048 if (pt != NULL && pt->pt_dict != NULL)
6049 {
6050 typval_T dtv;
6051
6052 ga_concat(&ga, (char_u *)", ");
6053 dtv.v_type = VAR_DICT;
6054 dtv.vval.v_dict = pt->pt_dict;
6055 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6056 vim_free(tf);
6057 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00006058 // terminate with ')' and a NUL
6059 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006060
6061 *tofree = ga.ga_data;
6062 r = *tofree;
6063 break;
6064 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006065
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006066 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006067 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006068 break;
6069
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006071 if (tv->vval.v_list == NULL)
6072 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02006073 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006074 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02006075 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006076 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006077 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6078 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 {
6080 *tofree = NULL;
6081 r = (char_u *)"[...]";
6082 }
6083 else
6084 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006085 int old_copyID = tv->vval.v_list->lv_copyID;
6086
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006087 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006088 *tofree = list2string(tv, copyID, restore_copyID);
6089 if (restore_copyID)
6090 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006091 r = *tofree;
6092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006094
Bram Moolenaar8c711452005-01-14 21:53:12 +00006095 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006096 if (tv->vval.v_dict == NULL)
6097 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006098 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006099 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006100 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006101 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006102 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6103 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006104 {
6105 *tofree = NULL;
6106 r = (char_u *)"{...}";
6107 }
6108 else
6109 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006110 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006111
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006112 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006113 *tofree = dict2string(tv, copyID, restore_copyID);
6114 if (restore_copyID)
6115 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006116 r = *tofree;
6117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006120 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006121 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006122 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006124 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006125 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006126 break;
6127
Bram Moolenaar835dc632016-02-07 14:27:38 +01006128 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006129 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006130#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006131 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006132 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6133 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006134 if (composite_val)
6135 {
6136 *tofree = string_quote(r, FALSE);
6137 r = *tofree;
6138 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006139#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006141
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006142 case VAR_INSTR:
6143 *tofree = NULL;
6144 r = (char_u *)"instructions";
6145 break;
6146
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006147 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006148 {
6149 class_T *cl = tv->vval.v_class;
6150 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6151 r = *tofree = alloc(len);
6152 vim_snprintf((char *)r, len, "class %s",
6153 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6154 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006155 break;
6156
6157 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006158 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006159 garray_T ga;
6160 ga_init2(&ga, 1, 50);
6161 ga_concat(&ga, (char_u *)"object of ");
6162 object_T *obj = tv->vval.v_object;
6163 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6164 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6165 : cl->class_name);
6166 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006167 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006168 ga_concat(&ga, (char_u *)" {");
6169 for (int i = 0; i < cl->class_obj_member_count; ++i)
6170 {
6171 if (i > 0)
6172 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006173 ocmember_T *m = &cl->class_obj_members[i];
6174 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006175 ga_concat(&ga, (char_u *)": ");
6176 char_u *tf = NULL;
6177 ga_concat(&ga, echo_string_core(
6178 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006179 &tf, numbuf, copyID, echo_style,
6180 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006181 vim_free(tf);
6182 }
6183 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006184 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006185
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006186 *tofree = r = ga.ga_data;
6187 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006188 break;
6189
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006190 case VAR_FLOAT:
6191 *tofree = NULL;
6192 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6193 r = numbuf;
6194 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006195
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006196 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006197 case VAR_SPECIAL:
6198 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006199 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006200 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006202
Bram Moolenaar8502c702014-06-17 12:51:16 +02006203 if (--recurse == 0)
6204 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006206}
6207
6208/*
6209 * Return a string with the string representation of a variable.
6210 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6211 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006212 * Does not put quotes around strings, as ":echo" displays values.
6213 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6214 * May return NULL.
6215 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006216 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006217echo_string(
6218 typval_T *tv,
6219 char_u **tofree,
6220 char_u *numbuf,
6221 int copyID)
6222{
6223 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6224}
6225
6226/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006227 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6228 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006229 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006230 */
6231 int
6232buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6233{
6234 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006235 char_u *t;
6236 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006237
6238 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6239 return -1;
6240
6241 if (lnum > buf->b_ml.ml_line_count)
6242 lnum = buf->b_ml.ml_line_count;
6243
6244 str = ml_get_buf(buf, lnum, FALSE);
6245 if (str == NULL)
6246 return -1;
6247
6248 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006249 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006250
Bram Moolenaar91458462021-01-13 20:08:38 +01006251 // count the number of characters
6252 t = str;
6253 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6254 t += mb_ptr2len(t);
6255
6256 // In insert mode, when the cursor is at the end of a non-empty line,
6257 // byteidx points to the NUL character immediately past the end of the
6258 // string. In this case, add one to the character count.
6259 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6260 count++;
6261
6262 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006263}
6264
6265/*
6266 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006267 * byte index. Works only for loaded buffers. Returns -1 on failure.
6268 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006269 */
6270 int
6271buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6272{
6273 char_u *str;
6274 char_u *t;
6275
6276 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6277 return -1;
6278
6279 if (lnum > buf->b_ml.ml_line_count)
6280 lnum = buf->b_ml.ml_line_count;
6281
6282 str = ml_get_buf(buf, lnum, FALSE);
6283 if (str == NULL)
6284 return -1;
6285
6286 // Convert the character offset to a byte offset
6287 t = str;
6288 while (*t != NUL && --charidx > 0)
6289 t += mb_ptr2len(t);
6290
Bram Moolenaar91458462021-01-13 20:08:38 +01006291 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006292}
6293
6294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006296 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299var2fpos(
6300 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006301 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006302 int *fnum, // set to fnum for '0, 'A, etc.
6303 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006305 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006307 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006309 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006310 if (varp->v_type == VAR_LIST)
6311 {
6312 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006313 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006314 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006315 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006316
6317 l = varp->vval.v_list;
6318 if (l == NULL)
6319 return NULL;
6320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006321 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006322 pos.lnum = list_find_nr(l, 0L, &error);
6323 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006324 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006325 if (charcol)
6326 len = (long)mb_charlen(ml_get(pos.lnum));
6327 else
6328 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006329
Bram Moolenaarec65d772020-08-20 22:29:12 +02006330 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006331 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006332 li = list_find(l, 1L);
6333 if (li != NULL && li->li_tv.v_type == VAR_STRING
6334 && li->li_tv.vval.v_string != NULL
6335 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006336 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006337 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006338 }
6339 else
6340 {
6341 pos.col = list_find_nr(l, 1L, &error);
6342 if (error)
6343 return NULL;
6344 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006347 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006348 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006349 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006351 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006352 pos.coladd = list_find_nr(l, 2L, &error);
6353 if (error)
6354 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006355
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006356 return &pos;
6357 }
6358
Bram Moolenaarc5809432021-03-27 21:23:30 +01006359 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6360 return NULL;
6361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006362 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006363 if (name == NULL)
6364 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006365
6366 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006367 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006368 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006369 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006370 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006371 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006372 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006373 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006374 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006375 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006376 pos = VIsual;
6377 else
6378 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006379 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006380 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006381 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006383 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006384 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6386 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006387 pos = *pp;
6388 }
6389 if (pos.lnum != 0)
6390 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006391 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006392 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6393 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006395
Bram Moolenaara5525202006-03-02 22:52:09 +00006396 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006397
Bram Moolenaar477933c2007-07-17 14:32:23 +00006398 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006399 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006400 // the "w_valid" flags are not reset when moving the cursor, but they
6401 // do matter for update_topline() and validate_botline().
6402 check_cursor_moved(curwin);
6403
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006404 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006405 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006406 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006407 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // In silent Ex mode topline is zero, but that's not a valid line
6409 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006410 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006411 return &pos;
6412 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006413 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006414 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006415 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006416 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006417 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006418 return &pos;
6419 }
6420 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006421 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006423 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
6425 pos.lnum = curbuf->b_ml.ml_line_count;
6426 pos.col = 0;
6427 }
6428 else
6429 {
6430 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006431 if (charcol)
6432 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6433 else
6434 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 }
6436 return &pos;
6437 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006438 if (in_vim9script())
6439 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 return NULL;
6441}
6442
6443/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006444 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006445 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006446 * Note that the column is passed on as-is, the caller may want to decrement
6447 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006448 * If "charcol" is TRUE use the column as the character index instead of the
6449 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006450 * Return FAIL when conversion is not possible, doesn't check the position for
6451 * validity.
6452 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454list2fpos(
6455 typval_T *arg,
6456 pos_T *posp,
6457 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006458 colnr_T *curswantp,
6459 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006460{
6461 list_T *l = arg->vval.v_list;
6462 long i = 0;
6463 long n;
6464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6466 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006467 if (arg->v_type != VAR_LIST
6468 || l == NULL
6469 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006470 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006471 return FAIL;
6472
6473 if (fnump != NULL)
6474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006476 if (n < 0)
6477 return FAIL;
6478 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006480 *fnump = n;
6481 }
6482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006484 if (n < 0)
6485 return FAIL;
6486 posp->lnum = n;
6487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006489 if (n < 0)
6490 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006491 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006492 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006493 if (charcol)
6494 {
6495 buf_T *buf;
6496
6497 // Get the text for the specified line in a loaded buffer
6498 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6499 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6500 return FAIL;
6501
Bram Moolenaar79f23442022-10-10 12:42:57 +01006502 n = buf_charidx_to_byteidx(buf,
6503 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006504 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006505 posp->col = n;
6506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006507 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006508 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006509 posp->coladd = 0;
6510 else
6511 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006512
Bram Moolenaar493c1782014-05-28 14:34:46 +02006513 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006514 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006515
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006516 return OK;
6517}
6518
6519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 * Get the length of an environment variable name.
6521 * Advance "arg" to the first character after the name.
6522 * Return 0 for error.
6523 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006525get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 char_u *p;
6528 int len;
6529
6530 for (p = *arg; vim_isIDc(*p); ++p)
6531 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 return 0;
6534
6535 len = (int)(p - *arg);
6536 *arg = p;
6537 return len;
6538}
6539
6540/*
6541 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006542 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 * Return 0 if something is wrong.
6544 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 char_u *p;
6549 int len;
6550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006553 {
6554 if (*p == ':')
6555 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006556 // "s:" is start of "s:var", but "n:" is not and can be used in
6557 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006558 len = (int)(p - *arg);
6559 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6560 || len > 1)
6561 break;
6562 }
6563 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return 0;
6566
6567 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006568 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 return len;
6571}
6572
6573/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006574 * Get the length of the name of a variable or function.
6575 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006577 * Return -1 if curly braces expansion failed.
6578 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * If the name contains 'magic' {}'s, expand them and return the
6580 * expanded name in an allocated string via 'alias' - caller must free.
6581 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583get_name_len(
6584 char_u **arg,
6585 char_u **alias,
6586 int evaluate,
6587 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
6589 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 char_u *p;
6591 char_u *expr_start;
6592 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6597 && (*arg)[2] == (int)KE_SNR)
6598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006599 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 *arg += 3;
6601 return get_id_len(arg) + 3;
6602 }
6603 len = eval_fname_script(*arg);
6604 if (len > 0)
6605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 *arg += len;
6608 }
6609
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006613 p = find_name_end(*arg, &expr_start, &expr_end,
6614 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (expr_start != NULL)
6616 {
6617 char_u *temp_string;
6618
6619 if (!evaluate)
6620 {
6621 len += (int)(p - *arg);
6622 *arg = skipwhite(p);
6623 return len;
6624 }
6625
6626 /*
6627 * Include any <SID> etc in the expanded string:
6628 * Thus the -len here.
6629 */
6630 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6631 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006632 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 *alias = temp_string;
6634 *arg = skipwhite(p);
6635 return (int)STRLEN(temp_string);
6636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006639 // Only give an error when there is something, otherwise it will be
6640 // reported at a higher level.
6641 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006642 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 return len;
6645}
6646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006647/*
6648 * Find the end of a variable or function name, taking care of magic braces.
6649 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6650 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006651 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006652 * Return a pointer to just after the name. Equal to "arg" if there is no
6653 * valid name.
6654 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656find_name_end(
6657 char_u *arg,
6658 char_u **expr_start,
6659 char_u **expr_end,
6660 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006662 int mb_nest = 0;
6663 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006665 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006666 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006668 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670 *expr_start = NULL;
6671 *expr_end = NULL;
6672 }
6673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006675 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006676 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006677 return arg;
6678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006679 for (p = arg; *p != NUL
6680 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006681 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006682 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006683 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006685 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006686 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006687 if (*p == '\'')
6688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006690 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006691 ;
6692 if (*p == NUL)
6693 break;
6694 }
6695 else if (*p == '"')
6696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006697 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006698 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006699 if (*p == '\\' && p[1] != NUL)
6700 ++p;
6701 if (*p == NUL)
6702 break;
6703 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006704 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006706 // "s:" is start of "s:var", but "n:" is not and can be used in
6707 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006708 len = (int)(p - arg);
6709 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006710 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006711 break;
6712 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006713
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006714 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006716 if (*p == '[')
6717 ++br_nest;
6718 else if (*p == ']')
6719 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006721
zeertzjqa93d9cd2023-05-02 16:25:47 +01006722 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724 if (*p == '{')
6725 {
6726 mb_nest++;
6727 if (expr_start != NULL && *expr_start == NULL)
6728 *expr_start = p;
6729 }
6730 else if (*p == '}')
6731 {
6732 mb_nest--;
6733 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6734 *expr_end = p;
6735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738
6739 return p;
6740}
6741
6742/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006743 * Expands out the 'magic' {}'s in a variable/function name.
6744 * Note that this can call itself recursively, to deal with
6745 * constructs like foo{bar}{baz}{bam}
6746 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6747 * "in_start" ^
6748 * "expr_start" ^
6749 * "expr_end" ^
6750 * "in_end" ^
6751 *
6752 * Returns a new allocated string, which the caller must free.
6753 * Returns NULL for failure.
6754 */
6755 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756make_expanded_name(
6757 char_u *in_start,
6758 char_u *expr_start,
6759 char_u *expr_end,
6760 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006761{
6762 char_u c1;
6763 char_u *retval = NULL;
6764 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006765
6766 if (expr_end == NULL || in_end == NULL)
6767 return NULL;
6768 *expr_start = NUL;
6769 *expr_end = NUL;
6770 c1 = *in_end;
6771 *in_end = NUL;
6772
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006773 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006774 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006775 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006776 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6777 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006778 if (retval != NULL)
6779 {
6780 STRCPY(retval, in_start);
6781 STRCAT(retval, temp_result);
6782 STRCAT(retval, expr_end + 1);
6783 }
6784 }
6785 vim_free(temp_result);
6786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006787 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006788 *expr_start = '{';
6789 *expr_end = '}';
6790
6791 if (retval != NULL)
6792 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006793 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794 if (expr_start != NULL)
6795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006796 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006797 temp_result = make_expanded_name(retval, expr_start,
6798 expr_end, temp_result);
6799 vim_free(retval);
6800 retval = temp_result;
6801 }
6802 }
6803
6804 return retval;
6805}
6806
6807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006814 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006815}
6816
6817/*
6818 * Return TRUE if character "c" can be used as the first character in a
6819 * variable or function name (excluding '{' and '}').
6820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006822eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006823{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006824 return ASCII_ISALPHA(c) || c == '_';
6825}
6826
6827/*
6828 * Return TRUE if character "c" can be used as the first character of a
6829 * dictionary key.
6830 */
6831 int
6832eval_isdictc(int c)
6833{
6834 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835}
6836
6837/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006838 * Handle:
6839 * - expr[expr], expr[expr:expr] subscript
6840 * - ".name" lookup
6841 * - function call with Funcref variable: func(expr)
6842 * - method call: var->method()
6843 *
6844 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006845 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006846 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848handle_subscript(
6849 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006850 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006852 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006853 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006854{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006855 int evaluate = evalarg != NULL
6856 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006857 int ret = OK;
6858 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006859 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006860 int getnext;
6861 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006862
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006863 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006864 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006865 // When at the end of the line and ".name" or "->{" or "->X" follows in
6866 // the next line then consume the line break.
6867 p = eval_next_non_blank(*arg, evalarg, &getnext);
6868 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006869 && ((*p == '.'
6870 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6871 || rettv->v_type == VAR_CLASS
6872 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006873 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6874 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6875 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006876 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006877 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006878 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006879 check_white = FALSE;
6880 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006881
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006882 if (rettv->v_type == VAR_ANY)
6883 {
6884 char_u *exp_name;
6885 int cc;
6886 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006887 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006888 type_T *type;
6889
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006890 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006891 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006892 if (**arg != '.')
6893 {
6894 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006895 semsg(_(e_expected_dot_after_name_str),
6896 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006897 ret = FAIL;
6898 break;
6899 }
6900 ++*arg;
6901 if (IS_WHITE_OR_NUL(**arg))
6902 {
6903 if (verbose)
6904 emsg(_(e_no_white_space_allowed_after_dot));
6905 ret = FAIL;
6906 break;
6907 }
6908
6909 // isolate the name
6910 exp_name = *arg;
6911 while (eval_isnamec(**arg))
6912 ++*arg;
6913 cc = **arg;
6914 **arg = NUL;
6915
6916 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006917 evalarg == NULL ? NULL : evalarg->eval_cctx,
6918 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006919 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006920
6921 if (idx < 0 && ufunc == NULL)
6922 {
6923 ret = FAIL;
6924 break;
6925 }
6926 if (idx >= 0)
6927 {
6928 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6929 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6930
6931 copy_tv(sv->sv_tv, rettv);
6932 }
6933 else
6934 {
6935 rettv->v_type = VAR_FUNC;
6936 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6937 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006938 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006939 }
6940
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006941 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6942 || rettv->v_type == VAR_PARTIAL))
6943 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006944 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006945 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6946 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006947
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006948 // Stop the expression evaluation when immediately aborting on
6949 // error, or when an interrupt occurred or an exception was thrown
6950 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006951 if (aborting())
6952 {
6953 if (ret == OK)
6954 clear_tv(rettv);
6955 ret = FAIL;
6956 }
6957 dict_unref(selfdict);
6958 selfdict = NULL;
6959 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006960 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006961 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006962 if (in_vim9script())
6963 *arg = skipwhite(p + 2);
6964 else
6965 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006966 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006967 {
zeertzjqc481ad32023-03-11 16:18:51 +00006968 emsg(_(e_no_white_space_allowed_before_parenthesis));
6969 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006970 }
zeertzjqc481ad32023-03-11 16:18:51 +00006971 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6972 // expr->{lambda}() or expr->(lambda)()
6973 ret = eval_lambda(arg, rettv, evalarg, verbose);
6974 else
6975 // expr->name()
6976 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006977 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006978 // "." is ".name" lookup when we found a dict or when evaluating and
6979 // scriptversion is at least 2, where string concatenation is "..".
6980 else if (**arg == '['
6981 || (**arg == '.' && (rettv->v_type == VAR_DICT
6982 || (!evaluate
6983 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006984 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006985 {
6986 dict_unref(selfdict);
6987 if (rettv->v_type == VAR_DICT)
6988 {
6989 selfdict = rettv->vval.v_dict;
6990 if (selfdict != NULL)
6991 ++selfdict->dv_refcount;
6992 }
6993 else
6994 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006995 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006996 {
6997 clear_tv(rettv);
6998 ret = FAIL;
6999 }
7000 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007001 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
7002 || rettv->v_type == VAR_OBJECT))
7003 {
7004 // class member: SomeClass.varname
7005 // class method: SomeClass.SomeMethod()
7006 // class constructor: SomeClass.new()
7007 // object member: someObject.varname
7008 // object method: someObject.SomeMethod()
7009 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
7010 {
7011 clear_tv(rettv);
7012 ret = FAIL;
7013 }
7014 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02007015 else
7016 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007017 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019 // Turn "dict.Func" into a partial for "Func" bound to "dict".
7020 // Don't do this when "Func" is already a partial that was bound
7021 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02007022 if (selfdict != NULL
7023 && (rettv->v_type == VAR_FUNC
7024 || (rettv->v_type == VAR_PARTIAL
7025 && (rettv->vval.v_partial->pt_auto
7026 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007027 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007028
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007029 dict_unref(selfdict);
7030 return ret;
7031}
7032
7033/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034 * Make a copy of an item.
7035 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00007036 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007037 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
7038 * reference to an already copied list/dict can be used.
7039 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02007041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042item_copy(
7043 typval_T *from,
7044 typval_T *to,
7045 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00007046 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007047 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007048{
7049 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007050 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007053 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00007054 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007055 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007056 }
7057 ++recurse;
7058
7059 switch (from->v_type)
7060 {
7061 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007062 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007063 case VAR_STRING:
7064 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007065 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01007066 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01007067 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007068 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007069 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007070 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007071 case VAR_CLASS:
7072 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 copy_tv(from, to);
7074 break;
7075 case VAR_LIST:
7076 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007077 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007078 if (from->vval.v_list == NULL)
7079 to->vval.v_list = NULL;
7080 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
7081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007082 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007083 to->vval.v_list = from->vval.v_list->lv_copylist;
7084 ++to->vval.v_list->lv_refcount;
7085 }
7086 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007087 to->vval.v_list = list_copy(from->vval.v_list,
7088 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007089 if (to->vval.v_list == NULL)
7090 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007092 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007094 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095 case VAR_DICT:
7096 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007097 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007098 if (from->vval.v_dict == NULL)
7099 to->vval.v_dict = NULL;
7100 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007103 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7104 ++to->vval.v_dict->dv_refcount;
7105 }
7106 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007107 to->vval.v_dict = dict_copy(from->vval.v_dict,
7108 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007109 if (to->vval.v_dict == NULL)
7110 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007112 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007113 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007115 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007116 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 }
7118 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007119 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007120}
7121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 void
7123echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7124{
7125 char_u *tofree;
7126 char_u numbuf[NUMBUFLEN];
7127 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7128
7129 if (*atstart)
7130 {
7131 *atstart = FALSE;
7132 // Call msg_start() after eval1(), evaluating the expression
7133 // may cause a message to appear.
7134 if (with_space)
7135 {
7136 // Mark the saved text as finishing the line, so that what
7137 // follows is displayed on a new line when scrolling back
7138 // at the more prompt.
7139 msg_sb_eol();
7140 msg_start();
7141 }
7142 }
7143 else if (with_space)
7144 msg_puts_attr(" ", echo_attr);
7145
7146 if (p != NULL)
7147 for ( ; *p != NUL && !got_int; ++p)
7148 {
7149 if (*p == '\n' || *p == '\r' || *p == TAB)
7150 {
7151 if (*p != TAB && *needclr)
7152 {
7153 // remove any text still there from the command
7154 msg_clr_eos();
7155 *needclr = FALSE;
7156 }
7157 msg_putchar_attr(*p, echo_attr);
7158 }
7159 else
7160 {
7161 if (has_mbyte)
7162 {
7163 int i = (*mb_ptr2len)(p);
7164
7165 (void)msg_outtrans_len_attr(p, i, echo_attr);
7166 p += i - 1;
7167 }
7168 else
7169 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7170 }
7171 }
7172 vim_free(tofree);
7173}
7174
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 * ":echo expr1 ..." print each argument separated with a space, add a
7177 * newline at the end.
7178 * ":echon expr1 ..." print each argument plain.
7179 */
7180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007181ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007185 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int needclr = TRUE;
7187 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007188 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007189 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007190 evalarg_T evalarg;
7191
Bram Moolenaare707c882020-07-01 13:07:37 +02007192 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 if (eap->skip)
7195 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007196 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007198 // If eval1() causes an error message the text from the command may
7199 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007200 need_clr_eos = needclr;
7201
Bram Moolenaar68db9962021-05-09 23:19:22 +02007202 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007203 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {
7205 /*
7206 * Report the invalid expression unless the expression evaluation
7207 * has been cancelled due to an aborting error, an interrupt, or an
7208 * exception.
7209 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007210 if (!aborting() && did_emsg == did_emsg_before
7211 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007212 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007213 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 break;
7215 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007216 need_clr_eos = FALSE;
7217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007219 {
7220 if (rettv.v_type == VAR_VOID)
7221 {
7222 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7223 break;
7224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007226 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 arg = skipwhite(arg);
7230 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007231 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007232 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
7234 if (eap->skip)
7235 --emsg_skip;
7236 else
7237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007238 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (needclr)
7240 msg_clr_eos();
7241 if (eap->cmdidx == CMD_echo)
7242 msg_end();
7243 }
7244}
7245
7246/*
7247 * ":echohl {name}".
7248 */
7249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007252 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007256 * Returns the :echo attribute
7257 */
7258 int
7259get_echo_attr(void)
7260{
7261 return echo_attr;
7262}
7263
7264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 * ":execute expr1 ..." execute the result of an expression.
7266 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007267 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007269 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 * Each gets spaces around each argument and a newline at the end for
7271 * echo commands
7272 */
7273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007274ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 int ret = OK;
7279 char_u *p;
7280 garray_T ga;
7281 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007282 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 ga_init2(&ga, 1, 80);
7285
7286 if (eap->skip)
7287 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007288 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007290 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007291 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 if (!eap->skip)
7295 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007296 char_u buf[NUMBUFLEN];
7297
7298 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007299 {
7300 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7301 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007302 semsg(_(e_using_invalid_value_as_string_str),
7303 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007304 p = NULL;
7305 }
7306 else
7307 p = tv_get_string_buf(&rettv, buf);
7308 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007309 else
7310 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007311 if (p == NULL)
7312 {
7313 clear_tv(&rettv);
7314 ret = FAIL;
7315 break;
7316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 len = (int)STRLEN(p);
7318 if (ga_grow(&ga, len + 2) == FAIL)
7319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007320 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 ret = FAIL;
7322 break;
7323 }
7324 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 ga.ga_len += len;
7328 }
7329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007330 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 arg = skipwhite(arg);
7332 }
7333
7334 if (ret != FAIL && ga.ga_data != NULL)
7335 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007336 // use the first line of continuation lines for messages
7337 SOURCING_LNUM = start_lnum;
7338
Bram Moolenaar37fef162022-08-29 18:16:32 +01007339 if (eap->cmdidx == CMD_echomsg
7340 || eap->cmdidx == CMD_echowindow
7341 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007343 // Mark the already saved text as finishing the line, so that what
7344 // follows is displayed on a new line when scrolling back at the
7345 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007346 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007347 }
7348
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007349 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007350 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007351 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007352 out_flush();
7353 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007354 else if (eap->cmdidx == CMD_echowindow)
7355 {
7356#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007357 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007358#endif
7359 msg_attr(ga.ga_data, echo_attr);
7360#ifdef HAS_MESSAGE_WINDOW
7361 end_echowindow();
7362#endif
7363 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007364 else if (eap->cmdidx == CMD_echoconsole)
7365 {
7366 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7367 ui_write((char_u *)"\r\n", 2, TRUE);
7368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else if (eap->cmdidx == CMD_echoerr)
7370 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007371 int save_did_emsg = did_emsg;
7372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007373 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007374 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (!force_abort)
7376 did_emsg = save_did_emsg;
7377 }
7378 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007379 {
7380 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7381
7382 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7383 sticky_cmdmod_flags = cmdmod.cmod_flags
7384 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 do_cmdline((char_u *)ga.ga_data,
7386 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007387 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390
7391 ga_clear(&ga);
7392
7393 if (eap->skip)
7394 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007395 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396}
7397
7398/*
7399 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7400 * "arg" points to the "&" or '+' when called, to "option" when returning.
7401 * Returns NULL when no option name found. Otherwise pointer to the char
7402 * after the option name.
7403 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007404 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007405find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406{
7407 char_u *p = *arg;
7408
7409 ++p;
7410 if (*p == 'g' && p[1] == ':')
7411 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007412 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 p += 2;
7414 }
7415 else if (*p == 'l' && p[1] == ':')
7416 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007417 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 p += 2;
7419 }
7420 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007421 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 if (!ASCII_ISALPHA(*p))
7424 return NULL;
7425 *arg = p;
7426
7427 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007428 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
7430 while (ASCII_ISALPHA(*p))
7431 ++p;
7432 return p;
7433}
7434
7435/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007436 * Display script name where an item was last set.
7437 * Should only be invoked when 'verbose' is non-zero.
7438 */
7439 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007440last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007441{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007442 char_u *p;
7443
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007444 if (script_ctx.sc_sid == 0)
7445 return;
7446
7447 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7448 if (p == NULL)
7449 return;
7450
7451 verbose_enter();
7452 msg_puts(_("\n\tLast set from "));
7453 msg_puts((char *)p);
7454 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007455 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007456 msg_puts(_(line_msg));
7457 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007458 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007459 verbose_leave();
7460 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007461}
7462
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007463#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465/*
7466 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007467 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 * "flags" can be "g" to do a global substitute.
7469 * Returns an allocated string, NULL for error.
7470 */
7471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472do_string_sub(
7473 char_u *str,
7474 char_u *pat,
7475 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007476 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 int sublen;
7480 regmatch_T regmatch;
7481 int i;
7482 int do_all;
7483 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007484 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 garray_T ga;
7486 char_u *ret;
7487 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007488 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007490 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007492 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 ga_init2(&ga, 1, 200);
7495
7496 do_all = (flags[0] == 'g');
7497
7498 regmatch.rm_ic = p_ic;
7499 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7500 if (regmatch.regprog != NULL)
7501 {
7502 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007503 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007506 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007507 if (regmatch.startp[0] == regmatch.endp[0])
7508 {
7509 if (zero_width == regmatch.startp[0])
7510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007511 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007512 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007513 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7514 (size_t)i);
7515 ga.ga_len += i;
7516 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007517 continue;
7518 }
7519 zero_width = regmatch.startp[0];
7520 }
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 /*
7523 * Get some space for a temporary buffer to do the substitution
7524 * into. It will contain:
7525 * - The text up to where the match is.
7526 * - The substituted text.
7527 * - The text after the match.
7528 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007529 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007530 if (sublen <= 0)
7531 {
7532 ga_clear(&ga);
7533 break;
7534 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007535 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7537 {
7538 ga_clear(&ga);
7539 break;
7540 }
7541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007542 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 i = (int)(regmatch.startp[0] - tail);
7544 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007545 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007546 (void)vim_regsub(&regmatch, sub, expr,
7547 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7548 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007550 tail = regmatch.endp[0];
7551 if (*tail == NUL)
7552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (!do_all)
7554 break;
7555 }
7556
7557 if (ga.ga_data != NULL)
7558 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7559
Bram Moolenaar473de612013-06-08 18:19:48 +02007560 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
7562
7563 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7564 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007565 if (p_cpo == empty_option)
7566 p_cpo = save_cpo;
7567 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007569 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007570 // If it's still empty it was changed and restored, need to restore in
7571 // the complicated way.
7572 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007573 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007574 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
7577 return ret;
7578}