blob: 8660a25d508aa109c2b4072223d6bd01e6891a70 [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]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100255 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 * Return the result in "rettv" and OK or FAIL.
257 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200258 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100259eval_expr_typval(
260 typval_T *expr,
261 typval_T *argv,
262 int argc,
263 funccall_T *fc_arg,
264 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100265{
266 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200268 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100269
270 if (expr->v_type == VAR_FUNC)
271 {
272 s = expr->vval.v_string;
273 if (s == NULL || *s == NUL)
274 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200275 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000276 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200277 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100278 return FAIL;
279 }
280 else if (expr->v_type == VAR_PARTIAL)
281 {
282 partial_T *partial = expr->vval.v_partial;
283
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200284 if (partial == NULL)
285 return FAIL;
286
Bram Moolenaar822ba242020-05-24 23:00:18 +0200287 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200288 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100290 funccall_T *fc = fc_arg != NULL ? fc_arg
291 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100292 int r;
293
294 if (fc == NULL)
295 return FAIL;
296
Bram Moolenaar69082912022-09-22 21:35:19 +0100297 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100298 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000299 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100300 if (fc_arg == NULL)
301 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100302 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 return FAIL;
304 }
305 else
306 {
307 s = partial_name(partial);
308 if (s == NULL || *s == NUL)
309 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200310 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000311 funcexe.fe_evaluate = TRUE;
312 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
314 return FAIL;
315 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200317 else if (expr->v_type == VAR_INSTR)
318 {
319 return exe_typval_instr(expr, rettv);
320 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 else
322 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000323 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 if (s == NULL)
325 return FAIL;
326 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200327 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200329 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200331 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200332 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100333 return FAIL;
334 }
335 }
336 return OK;
337}
338
339/*
340 * Like eval_to_bool() but using a typval_T instead of a string.
341 * Works for string, funcref and partial.
342 */
343 int
344eval_expr_to_bool(typval_T *expr, int *error)
345{
346 typval_T rettv;
347 int res;
348
Bram Moolenaar82418262022-09-28 16:16:15 +0100349 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100350 {
351 *error = TRUE;
352 return FALSE;
353 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200354 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100355 clear_tv(&rettv);
356 return res;
357}
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359/*
360 * Top level evaluation function, returning a string. If "skip" is TRUE,
361 * only parsing to "nextcmd" is done, without reporting errors. Return
362 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
363 */
364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100365eval_to_string_skip(
366 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200367 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100368 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
Bram Moolenaar33570922005-01-25 22:26:29 +0000370 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200372 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar37c83712020-06-30 21:18:36 +0200374 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (skip)
376 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200377 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 retval = NULL;
379 else
380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100381 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384 if (skip)
385 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200386 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
388 return retval;
389}
390
391/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100392 * Initialize "evalarg" for use.
393 */
394 void
395init_evalarg(evalarg_T *evalarg)
396{
397 CLEAR_POINTER(evalarg);
398 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
399}
400
401/*
402 * If "evalarg->eval_tofree" is not NULL free it later.
403 * Caller is expected to overwrite "evalarg->eval_tofree" next.
404 */
405 static void
406free_eval_tofree_later(evalarg_T *evalarg)
407{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000408 if (evalarg->eval_tofree == NULL)
409 return;
410
411 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
412 ((char_u **)evalarg->eval_tofree_ga.ga_data)
413 [evalarg->eval_tofree_ga.ga_len++]
414 = evalarg->eval_tofree;
415 else
416 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100417}
418
419/*
420 * After using "evalarg" filled from "eap": free the memory.
421 */
422 void
423clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
424{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000425 if (evalarg == NULL)
426 return;
427
428 garray_T *etga = &evalarg->eval_tofree_ga;
429
430 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100431 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000432 if (eap != NULL)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100433 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000434 // We may need to keep the original command line, e.g. for
435 // ":let" it has the variable names. But we may also need
436 // the new one, "nextcmd" points into it. Keep both.
437 vim_free(eap->cmdline_tofree);
438 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100439
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000440 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
441 {
442 // "nextcmd" points into the last line in eval_tofree_ga,
443 // need to keep it around.
444 --etga->ga_len;
445 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
446 vim_free(evalarg->eval_tofree);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100447 }
448 else
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000449 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100450 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000451 else
452 vim_free(evalarg->eval_tofree);
453 evalarg->eval_tofree = NULL;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100454 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +0000455
456 ga_clear_strings(etga);
457 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100458}
459
460/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000461 * Skip over an expression at "*pp".
462 * Return FAIL for an error, OK otherwise.
463 */
464 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200465skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000466{
Bram Moolenaar33570922005-01-25 22:26:29 +0000467 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000468
469 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200470 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000471}
472
473/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200474 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200475 * If in Vim9 script and line breaks are encountered, the lines are
476 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200477 * "arg" is advanced to just after the expression.
478 * "start" is set to the start of the expression, "end" to just after the end.
479 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200480 * Return FAIL for an error, OK otherwise.
481 */
482 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200483skip_expr_concatenate(
484 char_u **arg,
485 char_u **start,
486 char_u **end,
487 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200488{
489 typval_T rettv;
490 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200491 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200492 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200493 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200494 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200495 int evaluate = evalarg == NULL
496 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200497
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200498 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200499 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200500 {
501 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200502 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200503 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200504 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200505 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200506 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200507 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508
509 // Don't evaluate the expression.
510 if (evalarg != NULL)
511 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200512 *arg = skipwhite(*arg);
513 res = eval1(arg, &rettv, evalarg);
514 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200515 if (evalarg != NULL)
516 evalarg->eval_flags = save_flags;
517
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200518 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200519 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200520 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200521 if (evalarg->eval_ga.ga_len == 1)
522 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200523 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200524 ga_clear(gap);
525 gap->ga_itemsize = 0;
526 }
527 else
528 {
529 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200530 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200531
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200532 // Line breaks encountered, concatenate all the lines.
533 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200534 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200535
536 // free the lines only when using getsourceline()
537 if (evalarg->eval_cookie != NULL)
538 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200539 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200540 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200541 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100542 // later. Also free "eval_tofree" later if needed.
543 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200544 evalarg->eval_tofree =
545 ((char_u **)gap->ga_data)[gap->ga_len - 1];
546 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200547 ga_clear_strings(gap);
548 }
549 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200550 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200551 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200552
553 // free lines that were explicitly marked for freeing
554 ga_clear_strings(freegap);
555 }
556
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200557 gap->ga_itemsize = 0;
558 if (p == NULL)
559 return FAIL;
560 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200561 vim_free(evalarg->eval_tofree_lambda);
562 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200563 // Compute "end" relative to the end.
564 *end = *start + STRLEN(*start) - endoff;
565 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200566 }
567
568 return res;
569}
570
571/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100572 * Convert "tv" to a string.
zeertzjq19dfa272023-06-15 10:56:41 +0100573 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar883cf972021-01-15 18:04:43 +0100574 * Returns an allocated string (NULL when out of memory).
575 */
576 char_u *
577typval2string(typval_T *tv, int convert)
578{
579 garray_T ga;
580 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100581
582 if (convert && tv->v_type == VAR_LIST)
583 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000584 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100585 if (tv->vval.v_list != NULL)
586 {
587 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
588 if (tv->vval.v_list->lv_len > 0)
589 ga_append(&ga, NL);
590 }
591 ga_append(&ga, NUL);
592 retval = (char_u *)ga.ga_data;
593 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100594 else
595 retval = vim_strsave(tv_get_string(tv));
596 return retval;
597}
598
599/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200600 * Top level evaluation function, returning a string. Does not handle line
601 * breaks.
zeertzjq19dfa272023-06-15 10:56:41 +0100602 * When "convert" is TRUE convert a List into a sequence of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * Return pointer to allocated memory, or NULL for failure.
604 */
605 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100606eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100608 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100609 exarg_T *eap,
610 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
Bram Moolenaar33570922005-01-25 22:26:29 +0000612 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100614 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100615 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100617 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100618 if (use_simple_function)
619 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
620 else
621 r = eval0(arg, &tv, NULL, &evalarg);
622 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 retval = NULL;
624 else
625 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100626 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000627 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100629 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 return retval;
632}
633
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100634 char_u *
635eval_to_string(
636 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100637 int convert,
638 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100639{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100640 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100641}
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000644 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100645 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200646 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 */
648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_to_string_safe(
650 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000651 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100652 int keep_script_version,
653 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200656 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200657 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200658 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar9530b582022-01-22 13:39:08 +0000660 if (!keep_script_version)
661 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200662 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000663 if (use_sandbox)
664 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100665 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200666 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100667 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000668 if (use_sandbox)
669 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100670 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200671 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200672 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200673 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return retval;
675}
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677/*
678 * Top level evaluation function, returning a number.
679 * Evaluates "expr" silently.
680 * Returns -1 for an error.
681 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200682 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100683eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar33570922005-01-25 22:26:29 +0000685 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200686 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000687 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100688 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 ++emsg_off;
691
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100692 if (use_simple_function)
693 r = may_call_simple_func(expr, &rettv);
694 if (r == NOTDONE)
695 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
696 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 retval = -1;
698 else
699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100700 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000701 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703 --emsg_off;
704
705 return retval;
706}
707
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000708/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000709 * Top level evaluation function.
710 * Returns an allocated typval_T with the result.
711 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000712 */
713 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200714eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000715{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100716 return eval_expr_ext(arg, eap, FALSE);
717}
718
719 typval_T *
720eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
721{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000722 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200723 evalarg_T evalarg;
724
725 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000726
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200727 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100728 if (tv != NULL)
729 {
730 int r = NOTDONE;
731
732 if (use_simple_function)
733 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
734 if (r == NOTDONE)
735 r = eval0(arg, tv, eap, &evalarg);
736
737 if (r == FAIL)
738 VIM_CLEAR(tv);
739 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000740
Bram Moolenaar37c83712020-06-30 21:18:36 +0200741 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000742 return tv;
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000746 * "*arg" points to what can be a function name in the form of "import.Name" or
747 * "Funcref". Return the name of the function. Set "tofree" to something that
748 * was allocated.
749 * If "verbose" is FALSE no errors are given.
750 * Return NULL for any failure.
751 */
752 static char_u *
753deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000754 char_u **arg,
755 char_u **tofree,
756 evalarg_T *evalarg,
757 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000758{
759 typval_T ref;
760 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100761 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000762
763 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100764 if (evalarg != NULL)
765 {
766 // need to evaluate this to get an import, like in "a.Func"
767 save_flags = evalarg->eval_flags;
768 evalarg->eval_flags |= EVAL_EVALUATE;
769 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100770 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100771 {
772 dictitem_T *v;
773
774 // If <SID>VarName was used it would not be found, try another way.
775 v = find_var_also_in_script(name, NULL, FALSE);
776 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100777 {
778 name = NULL;
779 goto theend;
780 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100781 copy_tv(&v->di_tv, &ref);
782 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000783 if (*skipwhite(*arg) != NUL)
784 {
785 if (verbose)
786 semsg(_(e_trailing_characters_str), *arg);
787 name = NULL;
788 }
789 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
790 {
791 name = ref.vval.v_string;
792 ref.vval.v_string = NULL;
793 *tofree = name;
794 }
795 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
796 {
797 if (ref.vval.v_partial->pt_argc > 0
798 || ref.vval.v_partial->pt_dict != NULL)
799 {
800 if (verbose)
801 emsg(_(e_cannot_use_partial_here));
802 name = NULL;
803 }
804 else
805 {
806 name = vim_strsave(partial_name(ref.vval.v_partial));
807 *tofree = name;
808 }
809 }
810 else
811 {
812 if (verbose)
813 semsg(_(e_not_callable_type_str), name);
814 name = NULL;
815 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100816
817theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000818 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100819 if (evalarg != NULL)
820 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000821 return name;
822}
823
824/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100825 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200826 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
827 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000828 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100831call_vim_function(
832 char_u *func,
833 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200834 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200835 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000837 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200838 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000839 char_u *arg;
840 char_u *name;
841 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000842 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200845 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000846 funcexe.fe_firstline = curwin->w_cursor.lnum;
847 funcexe.fe_lastline = curwin->w_cursor.lnum;
848 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000849
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000850 // The name might be "import.Func" or "Funcref". We don't know, we need to
851 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000852 // autoload script has errors. Guess that when there is a dot in the name
853 // showing errors is the right choice.
854 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000855 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000856 if (ignore_errors)
857 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000858 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000859 if (ignore_errors)
860 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000861 if (name == NULL)
862 name = func;
863
864 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
865
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000866 if (ret == FAIL)
867 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000868 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000869
870 return ret;
871}
872
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100873/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100874 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000875 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
876 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000877 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000878 */
879 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100880call_func_retstr(
881 char_u *func,
882 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200883 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000884{
885 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000886 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000887
Bram Moolenaarded27a12018-08-01 19:06:03 +0200888 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000889 return NULL;
890
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100891 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 return retval;
894}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000895
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000896/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100897 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000898 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000899 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100900 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000901 */
902 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100903call_func_retlist(
904 char_u *func,
905 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200906 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000907{
908 typval_T rettv;
909
Bram Moolenaarded27a12018-08-01 19:06:03 +0200910 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000911 return NULL;
912
913 if (rettv.v_type != VAR_LIST)
914 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100915 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
916 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000917 clear_tv(&rettv);
918 return NULL;
919 }
920
921 return rettv.vval.v_list;
922}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaare70dd112022-01-21 16:31:11 +0000924#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100926 * Evaluate "arg", which is 'foldexpr'.
927 * Note: caller must set "curwin" to match "arg".
928 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
929 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 */
931 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000932eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000934 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200936 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000938 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000939 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100940 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100942 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000943 current_sctx = wp->w_p_script_ctx[WV_FDE];
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000946 if (use_sandbox)
947 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100948 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100950
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100951 // Evaluate the expression. If the expression is "FuncName()" call the
952 // function directly.
953 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 retval = 0;
955 else
956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100957 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958 if (tv.v_type == VAR_NUMBER)
959 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000960 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 retval = 0;
962 else
963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // If the result is a string, check if there is a non-digit before
965 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (!VIM_ISDIGIT(*s) && *s != '-')
968 *cp = *s++;
969 retval = atol((char *)s);
970 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000971 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
973 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000974 if (use_sandbox)
975 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100976 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200977 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000978 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200980 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
982#endif
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 * Get an lval: variable, Dict item or List item that can be assigned a value
986 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
987 * "name.key", "name.key[expr]" etc.
988 * Indexing only works if "name" is an existing List or Dictionary.
989 * "name" points to the start of the name.
990 * If "rettv" is not NULL it points to the value to be assigned.
991 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
992 * wrong; must end in space or cmd separator.
993 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100994 * flags:
995 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100996 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100997 * GLV_NO_AUTOLOAD: do not use script autoloading
998 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001000 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004get_lval(
1005 char_u *name,
1006 typval_T *rettv,
1007 lval_T *lp,
1008 int unlet,
1009 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 int flags, // GLV_ values
1011 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 char_u *expr_start, *expr_end;
1015 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001016 dictitem_T *v;
1017 typval_T var1;
1018 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001022 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001023 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001024 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001025 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001027 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001028 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001030 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001032 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001034 lp->ll_name_end = find_name_end(name, NULL, NULL,
1035 FNE_INCL_BR | fne_flags);
1036 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 }
1038
Bram Moolenaara749a422022-02-12 19:52:25 +00001039 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001040 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001041 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1042 {
1043 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1044 return NULL;
1045 }
1046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001047 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (expr_start != NULL)
1051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001052 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001053 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 && *p != '[' && *p != '.')
1055 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001056 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 return NULL;
1058 }
1059
1060 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1061 if (lp->ll_exp_name == NULL)
1062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001063 // Report an invalid expression in braces, unless the
1064 // expression evaluation has been cancelled due to an
1065 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (!aborting() && !quiet)
1067 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001068 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001069 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
1071 }
1072 }
1073 lp->ll_name = lp->ll_exp_name;
1074 }
1075 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 lp->ll_name = name;
1078
Bram Moolenaar4525a572022-02-13 11:57:33 +00001079 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001081 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001082 // However, "g:[key]" is indexing a dictionary.
1083 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001084 {
1085 --p;
1086 lp->ll_name_end = p;
1087 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001088 if (*skipwhite(p) == ':')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001089 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001090 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001091
Bram Moolenaar9510d222022-09-11 15:14:05 +01001092 if (is_scoped_variable(name))
1093 {
1094 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1095 return NULL;
1096 }
Bram Moolenaarce93d162023-01-30 21:12:34 +00001097 if (VIM_ISWHITE(*p))
1098 {
1099 semsg(_(e_no_white_space_allowed_before_colon_str), p);
1100 return NULL;
1101 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001102 if (tp == p + 1 && !quiet)
1103 {
1104 semsg(_(e_white_space_required_after_str_str), ":", p);
1105 return NULL;
1106 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001107 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001108 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001109 semsg(_(e_using_type_not_in_script_context_str), p);
1110 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001111 }
1112
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001113 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001114 lp->ll_type = parse_type(&tp,
1115 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1116 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001117 if (lp->ll_type == NULL && !quiet)
1118 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001119 lp->ll_name_end = tp;
1120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 }
1122 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001123 if (lp->ll_name == NULL)
1124 return p;
1125
Bram Moolenaar62aec932022-01-29 21:45:34 +00001126 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001127 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001128 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001129
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001130 if (import != NULL)
1131 {
1132 ufunc_T *ufunc;
1133 type_T *type;
1134
Bram Moolenaar753885b2022-08-24 16:30:36 +01001135 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001136 lp->ll_sid = import->imp_sid;
1137 lp->ll_name = skipwhite(p + 1);
1138 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1139 lp->ll_name_end = p;
1140
1141 // check the item is exported
1142 cc = *p;
1143 *p = NUL;
1144 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001145 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001146 {
1147 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001148 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001149 }
1150 *p = cc;
1151 }
1152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001155 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 return p;
1157
Bram Moolenaar4525a572022-02-13 11:57:33 +00001158 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001159 {
1160 // using local variable
1161 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001162 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001163 }
1164 else
1165 {
1166 cc = *p;
1167 *p = NUL;
1168 // When we would write to the variable pass &ht and prevent autoload.
1169 writing = !(flags & GLV_READ_ONLY);
1170 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001171 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001172 if (v == NULL && !quiet)
1173 semsg(_(e_undefined_variable_str), lp->ll_name);
1174 *p = cc;
1175 if (v == NULL)
1176 return NULL;
1177 lp->ll_tv = &v->di_tv;
1178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179
Bram Moolenaar4525a572022-02-13 11:57:33 +00001180 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001181 {
1182 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001183 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001184 return NULL;
1185 }
1186
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 /*
1188 * Loop until no more [idx] or .key is following.
1189 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001190 var1.v_type = VAR_UNKNOWN;
1191 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001192 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001193 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001194 vartype_T v_type = lp->ll_tv->v_type;
1195
1196 if (*p == '.' && v_type != VAR_DICT
1197 && v_type != VAR_OBJECT
1198 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001199 {
1200 if (!quiet)
1201 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1202 return NULL;
1203 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001204 if (v_type != VAR_LIST
1205 && v_type != VAR_DICT
1206 && v_type != VAR_BLOB
1207 && v_type != VAR_OBJECT
1208 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001214
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001215 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001216 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001217 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001218 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001219 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001220 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001221 r = rettv_blob_alloc(lp->ll_tv);
1222 if (r == FAIL)
1223 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001224
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001228 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001231
Bram Moolenaar4525a572022-02-13 11:57:33 +00001232 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001233 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001234 && lp->ll_tv == &v->di_tv
1235 && ht != NULL && ht == get_script_local_ht())
1236 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001237 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001238
1239 // Vim9 script local variable: get the type
1240 if (sv != NULL)
1241 lp->ll_valtype = sv->sv_type;
1242 }
1243
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 len = -1;
1245 if (*p == '.')
1246 {
1247 key = p + 1;
1248 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1249 ;
1250 if (len == 0)
1251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001253 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256 p = key + len;
1257 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 else
1259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 if (*p == ':')
1263 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 else
1265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001267 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001269 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 clear_tv(&var1);
1273 return NULL;
1274 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001275 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 }
1277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 if (*p == ':')
1280 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001281 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001284 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001285 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 if (rettv != NULL
1289 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001290 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001292 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001293 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001295 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001296 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001298 }
1299 p = skipwhite(p + 1);
1300 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 else
1303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001305 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001306 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001307 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001308 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001313 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001314 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001315 clear_tv(&var2);
1316 return NULL;
1317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001323
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001327 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001328 clear_tv(&var1);
1329 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 }
1332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001333 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 ++p;
1335 }
1336
Bram Moolenaard505d172022-12-18 21:42:55 +00001337 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001338 {
1339 if (len == -1)
1340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001341 // "[key]": get key from "var1"
1342 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001343 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 }
1348 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001350
1351 // a NULL dict is equivalent with an empty dict
1352 if (lp->ll_tv->vval.v_dict == NULL)
1353 {
1354 lp->ll_tv->vval.v_dict = dict_alloc();
1355 if (lp->ll_tv->vval.v_dict == NULL)
1356 {
1357 clear_tv(&var1);
1358 return NULL;
1359 }
1360 ++lp->ll_tv->vval.v_dict->dv_refcount;
1361 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001363
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001364 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001366 // When assigning to a scope dictionary check that a function and
1367 // variable name is valid (only variable name unless it is l: or
1368 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001369 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001370 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001371 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001372
1373 if (len != -1)
1374 {
1375 prevval = key[len];
1376 key[len] = NUL;
1377 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001378 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001379 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001380 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001381 && (rettv->v_type == VAR_FUNC
1382 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001383 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001384 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001385 if (len != -1)
1386 key[len] = prevval;
1387 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001388 {
1389 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001390 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001391 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001392 }
1393
Bram Moolenaar10c65862020-10-08 21:16:42 +02001394 if (lp->ll_valtype != NULL)
1395 // use the type of the member
1396 lp->ll_valtype = lp->ll_valtype->tt_member;
1397
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001400 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001401 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001402 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001403 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001404 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001405 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001406 return NULL;
1407 }
1408
Bram Moolenaar31b81602019-02-10 22:14:27 +01001409 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001413 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 }
1417 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001421 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 p = NULL;
1424 break;
1425 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001426 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001427 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001428 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1429 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001430 {
1431 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001432 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001433 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001434
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001435 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001438 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001439 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001440 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1441
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 /*
1443 * Get the number and item for the only or first index of the List.
1444 */
1445 if (empty1)
1446 lp->ll_n1 = 0;
1447 else
1448 // is number or string
1449 lp->ll_n1 = (long)tv_get_number(&var1);
1450 clear_tv(&var1);
1451
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001452 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001454 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 return NULL;
1456 }
1457 if (lp->ll_range && !lp->ll_empty2)
1458 {
1459 lp->ll_n2 = (long)tv_get_number(&var2);
1460 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001461 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1462 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001463 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001464 }
1465 lp->ll_blob = lp->ll_tv->vval.v_blob;
1466 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001467 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001469 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 {
1471 /*
1472 * Get the number and item for the only or first index of the List.
1473 */
1474 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001476 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001477 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001478 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001479 clear_tv(&var1);
1480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001483 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1484 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001485 if (lp->ll_li == NULL)
1486 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001487 clear_tv(&var2);
1488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001489 }
1490
Bram Moolenaar10c65862020-10-08 21:16:42 +02001491 if (lp->ll_valtype != NULL)
1492 // use the type of the member
1493 lp->ll_valtype = lp->ll_valtype->tt_member;
1494
Bram Moolenaar8c711452005-01-14 21:53:12 +00001495 /*
1496 * May need to find the item or absolute index for the second
1497 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 * When no index given: "lp->ll_empty2" is TRUE.
1499 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001500 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001502 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001503 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001506 if (check_range_index_two(lp->ll_list,
1507 &lp->ll_n1, lp->ll_li,
1508 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001510 }
1511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001513 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001514 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001515 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001516 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001517 && lp->ll_tv->vval.v_object != NULL)
1518 ? lp->ll_tv->vval.v_object->obj_class
1519 : lp->ll_tv->vval.v_class;
1520 // TODO: what if class is NULL?
1521 if (cl != NULL)
1522 {
1523 lp->ll_valtype = NULL;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001524
1525 if (flags & GLV_PREFER_FUNC)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001526 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001527 // First look for a function with this name.
1528 // round 1: class functions (skipped for an object)
1529 // round 2: object methods
1530 for (int round = v_type == VAR_OBJECT ? 2 : 1;
1531 round <= 2; ++round)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001532 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001533 int count = round == 1
1534 ? cl->class_class_function_count
1535 : cl->class_obj_method_count;
1536 ufunc_T **funcs = round == 1
1537 ? cl->class_class_functions
1538 : cl->class_obj_methods;
1539 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001540 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001541 ufunc_T *fp = funcs[i];
1542 char_u *ufname = (char_u *)fp->uf_name;
1543 if (STRNCMP(ufname, key, p - key) == 0
1544 && ufname[p - key] == NUL)
1545 {
1546 lp->ll_ufunc = fp;
1547 lp->ll_valtype = fp->uf_func_type;
1548 round = 3;
1549 break;
1550 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001551 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001552 }
1553 }
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001554
1555 if (lp->ll_valtype == NULL)
1556 {
1557 int count = v_type == VAR_OBJECT
1558 ? cl->class_obj_member_count
1559 : cl->class_class_member_count;
1560 ocmember_T *members = v_type == VAR_OBJECT
1561 ? cl->class_obj_members
1562 : cl->class_class_members;
1563 for (int i = 0; i < count; ++i)
1564 {
1565 ocmember_T *om = members + i;
1566 if (STRNCMP(om->ocm_name, key, p - key) == 0
1567 && om->ocm_name[p - key] == NUL)
1568 {
1569 switch (om->ocm_access)
1570 {
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001571 case VIM_ACCESS_PRIVATE:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001572 semsg(_(e_cannot_access_private_member_str),
1573 om->ocm_name);
1574 return NULL;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001575 case VIM_ACCESS_READ:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001576 if ((flags & GLV_READ_ONLY) == 0)
1577 {
1578 semsg(_(e_member_is_not_writable_str),
1579 om->ocm_name);
1580 return NULL;
1581 }
1582 break;
=?UTF-8?q?Ola=20S=C3=B6der?=d8742472023-03-05 13:12:32 +00001583 case VIM_ACCESS_ALL:
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001584 break;
1585 }
1586
1587 lp->ll_valtype = om->ocm_type;
1588
1589 if (v_type == VAR_OBJECT)
1590 lp->ll_tv = ((typval_T *)(
1591 lp->ll_tv->vval.v_object + 1)) + i;
1592 else
1593 lp->ll_tv = &cl->class_members_tv[i];
1594 break;
1595 }
1596 }
1597 }
1598
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001599 if (lp->ll_valtype == NULL)
1600 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001601 if (v_type == VAR_OBJECT)
1602 semsg(_(e_object_member_not_found_str), key);
1603 else
1604 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001605 return NULL;
1606 }
1607 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 }
1610
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001611 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001613 return p;
1614}
1615
1616/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001618 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001621{
1622 vim_free(lp->ll_exp_name);
1623 vim_free(lp->ll_newkey);
1624}
1625
1626/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001627 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001628 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001629 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1630 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001631 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001633set_var_lval(
1634 lval_T *lp,
1635 char_u *endp,
1636 typval_T *rettv,
1637 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001638 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1639 char_u *op,
1640 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001641{
1642 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001643 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001644
1645 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001646 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001647 cc = *endp;
1648 *endp = NUL;
h-east2bd6a092023-05-19 19:01:17 +01001649 if (in_vim9script() && check_reserved_name(lp->ll_name, FALSE) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001650 return;
1651
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001652 if (lp->ll_blob != NULL)
1653 {
1654 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 if (op != NULL && *op != '=')
1657 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001658 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001659 return;
1660 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001661 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001662 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663
1664 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1665 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001666 if (lp->ll_empty2)
1667 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1668
Bram Moolenaar68452172021-04-12 21:21:02 +02001669 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1670 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001672 }
1673 else
1674 {
1675 val = (int)tv_get_number_chk(rettv, &error);
1676 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001677 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001678 }
1679 }
1680 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001682 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001684 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1685 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001686 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001687 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001688 *endp = cc;
1689 return;
1690 }
1691
Bram Moolenaarff697e62019-02-12 22:28:33 +01001692 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001693 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001694 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001695 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001696 {
1697 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001698 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1699 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001700 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001701 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001702 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001703 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001705 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001706 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001707 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001708 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1709 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001710 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001711 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001712 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001713 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001714 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001715 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001716 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001717 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001718 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001719 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001720 else if (lp->ll_range)
1721 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001722 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1723 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001724 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001725 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001726 return;
1727 }
1728
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001729 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1730 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 }
1732 else
1733 {
1734 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001735 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001736 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001737 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1738 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001739 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001740 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001741 return;
1742 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001743
1744 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001745 && check_typval_arg_type(lp->ll_valtype, rettv,
1746 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001747 return;
1748
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001749 if (lp->ll_newkey != NULL)
1750 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 if (op != NULL && *op != '=')
1752 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001753 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754 return;
1755 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001756 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1757 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001758 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001761 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001762 if (di == NULL)
1763 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001764 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1765 {
1766 vim_free(di);
1767 return;
1768 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001769 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001770 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 else if (op != NULL && *op != '=')
1772 {
1773 tv_op(lp->ll_tv, rettv, op);
1774 return;
1775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001777 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001778
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001779 /*
1780 * Assign the value to the variable or list item.
1781 */
1782 if (copy)
1783 copy_tv(rettv, lp->ll_tv);
1784 else
1785 {
1786 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001787 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001788 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 }
1790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791}
1792
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001794 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1795 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001796 * Returns OK or FAIL.
1797 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001801 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 char_u numbuf[NUMBUFLEN];
1803 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001804 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001806 // Can't do anything with a Funcref or Dict on the right.
1807 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001808 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001809 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1810 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001811 {
1812 switch (tv1->v_type)
1813 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001814 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001815 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 case VAR_DICT:
1818 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001819 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001820 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001821 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001822 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001823 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001824 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001825 case VAR_CLASS:
1826 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 break;
1828
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001829 case VAR_BLOB:
1830 if (*op != '+' || tv2->v_type != VAR_BLOB)
1831 break;
1832 // BLOB += BLOB
1833 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1834 {
1835 blob_T *b1 = tv1->vval.v_blob;
1836 blob_T *b2 = tv2->vval.v_blob;
1837 int i, len = blob_len(b2);
1838 for (i = 0; i < len; i++)
1839 ga_append(&b1->bv_ga, blob_get(b2, i));
1840 }
1841 return OK;
1842
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001843 case VAR_LIST:
1844 if (*op != '+' || tv2->v_type != VAR_LIST)
1845 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001846 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001847 if (tv2->vval.v_list != NULL)
1848 {
1849 if (tv1->vval.v_list == NULL)
1850 {
1851 tv1->vval.v_list = tv2->vval.v_list;
1852 ++tv1->vval.v_list->lv_refcount;
1853 }
1854 else
1855 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1856 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001857 return OK;
1858
1859 case VAR_NUMBER:
1860 case VAR_STRING:
1861 if (tv2->v_type == VAR_LIST)
1862 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001863 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001864 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001865 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001866 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001867 if (tv2->v_type == VAR_FLOAT)
1868 {
1869 float_T f = n;
1870
Bram Moolenaarff697e62019-02-12 22:28:33 +01001871 if (*op == '%')
1872 break;
1873 switch (*op)
1874 {
1875 case '+': f += tv2->vval.v_float; break;
1876 case '-': f -= tv2->vval.v_float; break;
1877 case '*': f *= tv2->vval.v_float; break;
1878 case '/': f /= tv2->vval.v_float; break;
1879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001880 clear_tv(tv1);
1881 tv1->v_type = VAR_FLOAT;
1882 tv1->vval.v_float = f;
1883 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001885 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001886 switch (*op)
1887 {
1888 case '+': n += tv_get_number(tv2); break;
1889 case '-': n -= tv_get_number(tv2); break;
1890 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001891 case '/': n = num_divide(n, tv_get_number(tv2),
1892 &failed); break;
1893 case '%': n = num_modulus(n, tv_get_number(tv2),
1894 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001895 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001896 clear_tv(tv1);
1897 tv1->v_type = VAR_NUMBER;
1898 tv1->vval.v_number = n;
1899 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
1901 else
1902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001903 if (tv2->v_type == VAR_FLOAT)
1904 break;
1905
Bram Moolenaarff697e62019-02-12 22:28:33 +01001906 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001907 s = tv_get_string(tv1);
1908 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 clear_tv(tv1);
1910 tv1->v_type = VAR_STRING;
1911 tv1->vval.v_string = s;
1912 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001913 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001914
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001915 case VAR_FLOAT:
1916 {
1917 float_T f;
1918
Bram Moolenaarff697e62019-02-12 22:28:33 +01001919 if (*op == '%' || *op == '.'
1920 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001921 && tv2->v_type != VAR_NUMBER
1922 && tv2->v_type != VAR_STRING))
1923 break;
1924 if (tv2->v_type == VAR_FLOAT)
1925 f = tv2->vval.v_float;
1926 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001927 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001928 switch (*op)
1929 {
1930 case '+': tv1->vval.v_float += f; break;
1931 case '-': tv1->vval.v_float -= f; break;
1932 case '*': tv1->vval.v_float *= f; break;
1933 case '/': tv1->vval.v_float /= f; break;
1934 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001935 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001936 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 }
1938 }
1939
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001940 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 return FAIL;
1942}
1943
1944/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 * Evaluate the expression used in a ":for var in expr" command.
1946 * "arg" points to "var".
1947 * Set "*errp" to TRUE for an error, FALSE otherwise;
1948 * Return a pointer that holds the info. Null when there is an error.
1949 */
1950 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001951eval_for_line(
1952 char_u *arg,
1953 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001954 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001955 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956{
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001958 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 typval_T tv;
1961 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001962 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001966 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 if (fi == NULL)
1968 return NULL;
1969
Bram Moolenaar404557e2021-07-05 21:41:48 +02001970 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1971 &fi->fi_semicolon, FALSE);
1972 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return fi;
1974
Bram Moolenaar404557e2021-07-05 21:41:48 +02001975 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001976 if (expr[0] != 'i' || expr[1] != 'n'
1977 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001979 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1980 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1981 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001982 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return fi;
1984 }
1985
1986 if (skip)
1987 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001988 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1989 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 {
1991 *errp = FALSE;
1992 if (!skip)
1993 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001994 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001995 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001996 l = tv.vval.v_list;
1997 if (l == NULL)
1998 {
1999 // a null list is like an empty list: do nothing
2000 clear_tv(&tv);
2001 }
2002 else
2003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002005 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002007 // No need to increment the refcount, it's already set for
2008 // the list being used in "tv".
2009 fi->fi_list = l;
2010 list_add_watch(l, &fi->fi_lw);
2011 fi->fi_lw.lw_item = l->lv_first;
2012 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002014 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002015 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002016 fi->fi_bi = 0;
2017 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002018 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002019 typval_T btv;
2020
2021 // Make a copy, so that the iteration still works when the
2022 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002024 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002025 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002026 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002027 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002028 else if (tv.v_type == VAR_STRING)
2029 {
2030 fi->fi_byte_idx = 0;
2031 fi->fi_string = tv.vval.v_string;
2032 tv.vval.v_string = NULL;
2033 if (fi->fi_string == NULL)
2034 fi->fi_string = vim_strsave((char_u *)"");
2035 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 else
2037 {
Sean Dewar80d73952021-08-04 19:25:54 +02002038 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002039 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 }
2041 }
2042 }
2043 if (skip)
2044 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002045 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046
2047 return fi;
2048}
2049
2050/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002051 * Used when looping over a :for line, skip the "in expr" part.
2052 */
2053 void
2054skip_for_lines(void *fi_void, evalarg_T *evalarg)
2055{
2056 forinfo_T *fi = (forinfo_T *)fi_void;
2057 int i;
2058
2059 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002060 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002061}
2062
2063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 * Use the first item in a ":for" list. Advance to the next.
2065 * Assign the values to the variable (list). "arg" points to the first one.
2066 * Return TRUE when a valid item was found, FALSE when at end of list or
2067 * something wrong.
2068 */
2069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002070next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002072 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002074 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002075 ? (ASSIGN_FINAL
2076 // first round: error if variable exists
2077 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002078 | ASSIGN_NO_MEMBER_TYPE
2079 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002080 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002082 int skip_assign = in_vim9script() && arg[0] == '_'
2083 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002085 if (fi->fi_blob != NULL)
2086 {
2087 typval_T tv;
2088
2089 if (fi->fi_bi >= blob_len(fi->fi_blob))
2090 return FALSE;
2091 tv.v_type = VAR_NUMBER;
2092 tv.v_lock = VAR_FIXED;
2093 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2094 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002095 if (skip_assign)
2096 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002097 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002098 fi->fi_varcount, flag, NULL) == OK;
2099 }
2100
2101 if (fi->fi_string != NULL)
2102 {
2103 typval_T tv;
2104 int len;
2105
2106 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2107 if (len == 0)
2108 return FALSE;
2109 tv.v_type = VAR_STRING;
2110 tv.v_lock = VAR_FIXED;
2111 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2112 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002113 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002114 if (skip_assign)
2115 result = TRUE;
2116 else
2117 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002118 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002119 vim_free(tv.vval.v_string);
2120 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002121 }
2122
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 item = fi->fi_lw.lw_item;
2124 if (item == NULL)
2125 result = FALSE;
2126 else
2127 {
2128 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002129 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002130 if (skip_assign)
2131 result = TRUE;
2132 else
2133 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002134 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135 }
2136 return result;
2137}
2138
2139/*
2140 * Free the structure used to store info used by ":for".
2141 */
2142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002143free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002144{
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002147 if (fi == NULL)
2148 return;
2149 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002150 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002151 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002152 list_unref(fi->fi_list);
2153 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002154 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002155 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002156 else
2157 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002158 vim_free(fi);
2159}
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162set_context_for_expression(
2163 expand_T *xp,
2164 char_u *arg,
2165 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002167 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002169 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002171 if (cmdidx == CMD_let || cmdidx == CMD_var
2172 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002173 {
2174 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002177 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002179 {
2180 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002181 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002182 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183 break;
2184 }
2185 return;
2186 }
2187 }
2188 else
2189 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2190 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 while ((xp->xp_pattern = vim_strpbrk(arg,
2192 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2193 {
2194 c = *xp->xp_pattern;
2195 if (c == '&')
2196 {
2197 c = xp->xp_pattern[1];
2198 if (c == '&')
2199 {
2200 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002201 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002206 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2207 xp->xp_pattern += 2;
2208
2209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 else if (c == '$')
2212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 xp->xp_context = EXPAND_ENV_VARS;
2215 }
2216 else if (c == '=')
2217 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002218 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 xp->xp_context = EXPAND_EXPRESSION;
2220 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002221 else if (c == '#'
2222 && xp->xp_context == EXPAND_EXPRESSION)
2223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002224 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002225 break;
2226 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002227 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 && xp->xp_context == EXPAND_FUNCTIONS
2229 && vim_strchr(xp->xp_pattern, '(') == NULL)
2230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002231 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 break;
2233 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002234 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002236 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2239 if (c == '\\' && xp->xp_pattern[1] != NUL)
2240 ++xp->xp_pattern;
2241 xp->xp_context = EXPAND_NOTHING;
2242 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002243 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2247 /* skip */ ;
2248 xp->xp_context = EXPAND_NOTHING;
2249 }
2250 else if (c == '|')
2251 {
2252 if (xp->xp_pattern[1] == '|')
2253 {
2254 ++xp->xp_pattern;
2255 xp->xp_context = EXPAND_EXPRESSION;
2256 }
2257 else
2258 xp->xp_context = EXPAND_COMMANDS;
2259 }
2260 else
2261 xp->xp_context = EXPAND_EXPRESSION;
2262 }
2263 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002264 // Doesn't look like something valid, expand as an expression
2265 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002266 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 arg = xp->xp_pattern;
2268 if (*arg != NUL)
2269 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2270 /* skip */ ;
2271 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002272
2273 // ":exe one two" completes "two"
2274 if ((cmdidx == CMD_execute
2275 || cmdidx == CMD_echo
2276 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002277 || cmdidx == CMD_echomsg
2278 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002279 && xp->xp_context == EXPAND_EXPRESSION)
2280 {
2281 for (;;)
2282 {
2283 char_u *n = skiptowhite(arg);
2284
2285 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2286 break;
2287 arg = skipwhite(n);
2288 }
2289 }
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 xp->xp_pattern = arg;
2292}
2293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002295 * Return TRUE if "pat" matches "text".
2296 * Does not use 'cpo' and always uses 'magic'.
2297 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002298 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002299pattern_match(char_u *pat, char_u *text, int ic)
2300{
2301 int matches = FALSE;
2302 char_u *save_cpo;
2303 regmatch_T regmatch;
2304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002305 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002306 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002307 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002308 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2309 if (regmatch.regprog != NULL)
2310 {
2311 regmatch.rm_ic = ic;
2312 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2313 vim_regfree(regmatch.regprog);
2314 }
2315 p_cpo = save_cpo;
2316 return matches;
2317}
2318
2319/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002320 * Handle a name followed by "(". Both for just "name(arg)" and for
2321 * "expr->name(arg)".
2322 * Returns OK or FAIL.
2323 */
2324 static int
2325eval_func(
2326 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002327 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002328 char_u *name,
2329 int name_len,
2330 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002331 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002332 typval_T *basetv) // "expr" for "expr->name(arg)"
2333{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002334 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002335 char_u *s = name;
2336 int len = name_len;
2337 partial_T *partial;
2338 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002339 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002340 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002341
2342 if (!evaluate)
2343 check_vars(s, len);
2344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002345 // If "s" is the name of a variable of type VAR_FUNC
2346 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002347 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002348 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002350 // Need to make a copy, in case evaluating the arguments makes
2351 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002352 s = vim_strsave(s);
zeertzjqb7f6f932023-04-13 22:12:50 +01002353 if (s == NULL || (evaluate && *s == NUL))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002354 ret = FAIL;
2355 else
2356 {
2357 funcexe_T funcexe;
2358
2359 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002360 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002361 funcexe.fe_firstline = curwin->w_cursor.lnum;
2362 funcexe.fe_lastline = curwin->w_cursor.lnum;
2363 funcexe.fe_evaluate = evaluate;
2364 funcexe.fe_partial = partial;
2365 funcexe.fe_basetv = basetv;
2366 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002367 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002368 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002369 }
2370 vim_free(s);
2371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002372 // If evaluate is FALSE rettv->v_type was not set in
2373 // get_func_tv, but it's needed in handle_subscript() to parse
2374 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002375 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2376 {
2377 rettv->vval.v_string = NULL;
2378 rettv->v_type = VAR_FUNC;
2379 }
2380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002381 // Stop the expression evaluation when immediately
2382 // aborting on error, or when an interrupt occurred or
2383 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002384 if (evaluate && aborting())
2385 {
2386 if (ret == OK)
2387 clear_tv(rettv);
2388 ret = FAIL;
2389 }
2390 return ret;
2391}
2392
2393/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002394 * After a NL, skip over empty lines and comment-only lines.
2395 */
2396 static char_u *
2397newline_skip_comments(char_u *arg)
2398{
2399 char_u *p = arg + 1;
2400
2401 for (;;)
2402 {
2403 p = skipwhite(p);
2404
2405 if (*p == NUL)
2406 break;
2407 if (vim9_comment_start(p))
2408 {
2409 char_u *nl = vim_strchr(p, NL);
2410
2411 if (nl == NULL)
2412 break;
2413 p = nl;
2414 }
2415 if (*p != NL)
2416 break;
2417 ++p; // skip another NL
2418 }
2419 return p;
2420}
2421
2422/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002423 * Get the next line source line without advancing. But do skip over comment
2424 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002425 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002426 */
2427 static char_u *
2428getline_peek_skip_comments(evalarg_T *evalarg)
2429{
2430 for (;;)
2431 {
2432 char_u *next = getline_peek(evalarg->eval_getline,
2433 evalarg->eval_cookie);
2434 char_u *p;
2435
2436 if (next == NULL)
2437 break;
2438 p = skipwhite(next);
2439 if (*p != NUL && !vim9_comment_start(p))
2440 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002441 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002442 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002443 }
2444 return NULL;
2445}
2446
2447/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002448 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2449 * comment) and there is a next line, return the next line (skipping blanks)
2450 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002451 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2452 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 * "arg" must point somewhere inside a line, not at the start.
2454 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002455 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2457{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002458 char_u *p = skipwhite(arg);
2459
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002461 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002463 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2464 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002465 && (*p == NUL || *p == NL
2466 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002467 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002469
Bram Moolenaare442d592022-05-05 12:20:28 +01002470 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002471 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002472 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002473 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002474 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002475 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476
Bram Moolenaar9d489562020-07-30 20:08:50 +02002477 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002478 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002479 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002480 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 }
2482 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002483 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484}
2485
2486/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002487 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002488 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002489 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002490 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002491eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002492{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002493 garray_T *gap = &evalarg->eval_ga;
2494 char_u *line;
2495
Bram Moolenaar39be4982022-05-06 21:51:50 +01002496 if (arg != NULL)
2497 {
2498 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002499 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002500 // Truncate before a trailing comment, so that concatenating the lines
2501 // won't turn the rest into a comment.
2502 if (*skipwhite(arg) == '#')
2503 *arg = NUL;
2504 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002505
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002506 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002507 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2508 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002509 else
2510 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002511 if (line == NULL)
2512 return NULL;
2513
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002514 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002515 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2516 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002517 char_u *p = skipwhite(line);
2518
2519 // Going to concatenate the lines after parsing. For an empty or
2520 // comment line use an empty string.
2521 if (*p == NUL || vim9_comment_start(p))
2522 {
2523 vim_free(line);
2524 line = vim_strsave((char_u *)"");
2525 }
2526
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002527 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2528 ++gap->ga_len;
2529 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002530 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002531 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002532 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002533 evalarg->eval_tofree = line;
2534 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002535
2536 // Advanced to the next line, "arg" no longer points into the previous
2537 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002538 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002539 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002540}
2541
Bram Moolenaare6b53242020-07-01 17:28:33 +02002542/*
2543 * Call eval_next_non_blank() and get the next line if needed.
2544 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002545 char_u *
2546skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2547{
2548 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002549 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002550
Bram Moolenaar962d7212020-07-04 14:15:00 +02002551 if (evalarg == NULL)
2552 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002553 eval_next_non_blank(p, evalarg, &getnext);
2554 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002555 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002556 return p;
2557}
2558
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002559/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002560 * The "eval" functions have an "evalarg" argument: When NULL or
2561 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2562 * parsed but not executed. The functions may return OK, but the rettv will be
2563 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
2565
2566/*
2567 * Handle zero level expression.
2568 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002570 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 * Return OK or FAIL.
2573 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575eval0(
2576 char_u *arg,
2577 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002578 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002581 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2582}
2583
2584/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002585 * If "arg" is a simple function call without arguments then call it and return
2586 * the result. Otherwise return NOTDONE.
2587 */
2588 int
2589may_call_simple_func(
2590 char_u *arg,
2591 typval_T *rettv)
2592{
2593 char_u *parens = (char_u *)strstr((char *)arg, "()");
2594 int r = NOTDONE;
2595
2596 // If the expression is "FuncName()" then we can skip a lot of overhead.
2597 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2598 {
2599 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2600
2601 if (to_name_end(p, TRUE) == parens)
2602 r = call_simple_func(arg, (int)(parens - arg), rettv);
2603 }
2604 return r;
2605}
2606
2607/*
2608 * Handle zero level expression with optimization for a simple function call.
2609 * Same arguments and return value as eval0().
2610 */
2611 int
2612eval0_simple_funccal(
2613 char_u *arg,
2614 typval_T *rettv,
2615 exarg_T *eap,
2616 evalarg_T *evalarg)
2617{
2618 int r = may_call_simple_func(arg, rettv);
2619
2620 if (r == NOTDONE)
2621 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2622 return r;
2623}
2624
2625/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002626 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2627 * expression and don't check what comes after the expression.
2628 */
2629 int
2630eval0_retarg(
2631 char_u *arg,
2632 typval_T *rettv,
2633 exarg_T *eap,
2634 evalarg_T *evalarg,
2635 char_u **retarg)
2636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int ret;
2638 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002639 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002640 int did_emsg_before = did_emsg;
2641 int called_emsg_before = called_emsg;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002642 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002643 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002647
Bram Moolenaar79481362022-06-27 20:15:10 +01002648 if (ret != FAIL)
2649 {
2650 expr_end = p;
2651 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002652
Bram Moolenaar79481362022-06-27 20:15:10 +01002653 // In Vim9 script a command block is not split at NL characters for
2654 // commands using an expression argument. Skip over a '#' comment to
2655 // check for a following NL. Require white space before the '#'.
2656 if (in_vim9script() && p > expr_end && retarg == NULL)
2657 while (*p == '#')
2658 {
2659 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002660
Bram Moolenaar79481362022-06-27 20:15:10 +01002661 if (nl == NULL)
2662 break;
2663 p = skipwhite(nl + 1);
2664 if (eap != NULL && *p != NUL)
2665 eap->nextcmd = p;
2666 check_for_end = FALSE;
2667 }
2668
2669 if (check_for_end)
2670 end_error = !ends_excmd2(arg, p);
2671 }
2672
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002673 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
2675 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 /*
2678 * Report the invalid expression unless the expression evaluation has
2679 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002680 * exception, or we already gave a more specific error.
2681 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002683 if (!aborting()
2684 && did_emsg == did_emsg_before
2685 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002686 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002687 {
2688 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002689 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002690 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002691 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002692 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002693
zeertzjqf2588b62023-05-05 17:22:35 +01002694 if (eap != NULL && p != NULL)
2695 {
2696 // Some of the expression may not have been consumed.
2697 // Only execute a next command if it cannot be a "||" operator.
2698 // The next command may be "catch".
2699 char_u *nextcmd = check_nextcmd(p);
2700 if (nextcmd != NULL && *nextcmd != '|')
2701 eap->nextcmd = nextcmd;
2702 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002703 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002705
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002706 if (retarg != NULL)
2707 *retarg = p;
2708 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002709 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return ret;
2712}
2713
2714/*
2715 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002716 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002717 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 *
2719 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002720 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002722 * Note: "rettv.v_lock" is not set.
2723 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * Return OK or FAIL.
2725 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002726 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002727eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002729 char_u *p;
2730 int getnext;
2731
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002732 CLEAR_POINTER(rettv);
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /*
2735 * Get the first variable.
2736 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return FAIL;
2739
Bram Moolenaar793648f2020-06-26 21:28:25 +02002740 p = eval_next_non_blank(*arg, evalarg, &getnext);
2741 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002743 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744 int result;
2745 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002746 evalarg_T *evalarg_used = evalarg;
2747 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002748 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002749 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002750 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751
2752 if (evalarg == NULL)
2753 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002754 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002755 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002757 orig_flags = evalarg_used->eval_flags;
2758 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002759
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002760 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002761 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002762 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002763 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002764 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002765 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002766 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002767 clear_tv(rettv);
2768 return FAIL;
2769 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002770 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002771 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002774 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 int error = FALSE;
2777
Bram Moolenaar13106602020-10-04 16:06:05 +02002778 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002779 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002780 else if (vim9script)
2781 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002782 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002784 if (error || !op_falsy || !result)
2785 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002786 if (error)
2787 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789
2790 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002791 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002793 if (op_falsy)
2794 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002795 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002796 {
mityu4ac198c2021-05-28 17:52:40 +02002797 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002798 clear_tv(rettv);
2799 return FAIL;
2800 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002802 evalarg_used->eval_flags = (op_falsy ? !result : result)
zeertzjqb7f6f932023-04-13 22:12:50 +01002803 ? orig_flags : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002804 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002805 {
2806 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002808 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002809 if (!op_falsy || !result)
2810 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002812 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002814 /*
2815 * Check for the ":".
2816 */
2817 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2818 if (*p != ':')
2819 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002820 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002821 if (evaluate && result)
2822 clear_tv(rettv);
2823 evalarg_used->eval_flags = orig_flags;
2824 return FAIL;
2825 }
2826 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002827 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002828 else
2829 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002830 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002831 {
2832 error_white_both(p, 1);
2833 clear_tv(rettv);
2834 evalarg_used->eval_flags = orig_flags;
2835 return FAIL;
2836 }
2837 *arg = p;
2838 }
2839
2840 /*
2841 * Get the third variable. Recursive!
2842 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002843 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002844 {
mityu4ac198c2021-05-28 17:52:40 +02002845 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002846 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002847 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002848 return FAIL;
2849 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002850 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2851 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002852 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002853 if (eval1(arg, &var2, evalarg_used) == FAIL)
2854 {
2855 if (evaluate && result)
2856 clear_tv(rettv);
2857 evalarg_used->eval_flags = orig_flags;
2858 return FAIL;
2859 }
2860 if (evaluate && !result)
2861 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002863
2864 if (evalarg == NULL)
2865 clear_evalarg(&local_evalarg, NULL);
2866 else
2867 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869
2870 return OK;
2871}
2872
2873/*
2874 * Handle first level expression:
2875 * expr2 || expr2 || expr2 logical OR
2876 *
2877 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002878 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 *
2880 * Return OK or FAIL.
2881 */
2882 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002885 char_u *p;
2886 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
2888 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002889 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 return FAIL;
2893
2894 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002895 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002897 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002898 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002900 evalarg_T *evalarg_used = evalarg;
2901 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 int evaluate;
2903 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002904 long result = FALSE;
2905 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002906 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002907 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002908
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909 if (evalarg == NULL)
2910 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002911 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002914 orig_flags = evalarg_used->eval_flags;
2915 evaluate = orig_flags & EVAL_EVALUATE;
2916 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002918 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002919 result = tv_get_bool_chk(rettv, &error);
2920 else if (tv_get_number_chk(rettv, &error) != 0)
2921 result = TRUE;
2922 clear_tv(rettv);
2923 if (error)
2924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
2927 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002928 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002930 while (p[0] == '|' && p[1] == '|')
2931 {
2932 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002933 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002934 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002935 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002936 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002937 {
2938 error_white_both(p, 2);
2939 clear_tv(rettv);
2940 return FAIL;
2941 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002942 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002943 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002944
2945 /*
2946 * Get the second variable.
2947 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002948 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002949 {
mityu4ac198c2021-05-28 17:52:40 +02002950 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002951 clear_tv(rettv);
2952 return FAIL;
2953 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002954 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2955 evalarg_used->eval_flags = !result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01002956 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002957 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002958 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002959
2960 /*
2961 * Compute the result.
2962 */
2963 if (evaluate && !result)
2964 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002965 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002966 result = tv_get_bool_chk(&var2, &error);
2967 else if (tv_get_number_chk(&var2, &error) != 0)
2968 result = TRUE;
2969 clear_tv(&var2);
2970 if (error)
2971 return FAIL;
2972 }
2973 if (evaluate)
2974 {
2975 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002976 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002977 rettv->v_type = VAR_BOOL;
2978 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002979 }
2980 else
2981 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002982 rettv->v_type = VAR_NUMBER;
2983 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002984 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002985 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002986
2987 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002989
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002990 if (evalarg == NULL)
2991 clear_evalarg(&local_evalarg, NULL);
2992 else
2993 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995
2996 return OK;
2997}
2998
2999/*
3000 * Handle second level expression:
3001 * expr3 && expr3 && expr3 logical AND
3002 *
3003 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02003004 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 *
3006 * Return OK or FAIL.
3007 */
3008 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003009eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003011 char_u *p;
3012 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003015 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 return FAIL;
3019
3020 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003021 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003023 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003024 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003026 evalarg_T *evalarg_used = evalarg;
3027 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003028 int orig_flags;
3029 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003030 long result = TRUE;
3031 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003032 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003033 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003034
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003035 if (evalarg == NULL)
3036 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003037 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003038 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003040 orig_flags = evalarg_used->eval_flags;
3041 evaluate = orig_flags & EVAL_EVALUATE;
3042 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003044 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003045 result = tv_get_bool_chk(rettv, &error);
3046 else if (tv_get_number_chk(rettv, &error) == 0)
3047 result = FALSE;
3048 clear_tv(rettv);
3049 if (error)
3050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052
3053 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003054 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003056 while (p[0] == '&' && p[1] == '&')
3057 {
3058 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003059 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003060 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003061 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003062 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003063 {
3064 error_white_both(p, 2);
3065 clear_tv(rettv);
3066 return FAIL;
3067 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003069 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003070
3071 /*
3072 * Get the second variable.
3073 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003074 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003075 {
mityu4ac198c2021-05-28 17:52:40 +02003076 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003077 clear_tv(rettv);
3078 return FAIL;
3079 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003080 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3081 evalarg_used->eval_flags = result ? orig_flags
zeertzjqb7f6f932023-04-13 22:12:50 +01003082 : (orig_flags & ~EVAL_EVALUATE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003083 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003084 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003085 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003086
3087 /*
3088 * Compute the result.
3089 */
3090 if (evaluate && result)
3091 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003092 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003093 result = tv_get_bool_chk(&var2, &error);
3094 else if (tv_get_number_chk(&var2, &error) == 0)
3095 result = FALSE;
3096 clear_tv(&var2);
3097 if (error)
3098 return FAIL;
3099 }
3100 if (evaluate)
3101 {
3102 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003103 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003104 rettv->v_type = VAR_BOOL;
3105 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003106 }
3107 else
3108 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003109 rettv->v_type = VAR_NUMBER;
3110 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003111 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003112 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003113
3114 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003116
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003117 if (evalarg == NULL)
3118 clear_evalarg(&local_evalarg, NULL);
3119 else
3120 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122
3123 return OK;
3124}
3125
3126/*
3127 * Handle third level expression:
3128 * var1 == var2
3129 * var1 =~ var2
3130 * var1 != var2
3131 * var1 !~ var2
3132 * var1 > var2
3133 * var1 >= var2
3134 * var1 < var2
3135 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003136 * var1 is var2
3137 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 *
3139 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003140 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 *
3142 * Return OK or FAIL.
3143 */
3144 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003148 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003149 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003151 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003154 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003156 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return FAIL;
3158
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003159 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003160
Bram Moolenaar696ba232020-07-29 21:20:41 +02003161 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003166 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003168 typval_T var2;
3169 int ic;
3170 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003171 int evaluate = evalarg == NULL
3172 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003173 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003174
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003175 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003176 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003177 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003178 p = *arg;
3179 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003180 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3181 {
mityu4ac198c2021-05-28 17:52:40 +02003182 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003183 clear_tv(rettv);
3184 return FAIL;
3185 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003186
Bram Moolenaar696ba232020-07-29 21:20:41 +02003187 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3188 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003189 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003190 clear_tv(rettv);
3191 return FAIL;
3192 }
3193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003194 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (p[len] == '?')
3196 {
3197 ic = TRUE;
3198 ++len;
3199 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003200 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else if (p[len] == '#')
3202 {
3203 ic = FALSE;
3204 ++len;
3205 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003206 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003208 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210 /*
3211 * Get the second variable.
3212 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003213 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3214 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003215 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003216 clear_tv(rettv);
3217 return FAIL;
3218 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003219 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003220 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 return FAIL;
3224 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003225 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003226 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003227 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003228
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003229 // use the line of the comparison for messages
3230 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003231 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003232 {
3233 ret = FAIL;
3234 clear_tv(rettv);
3235 }
3236 else
3237 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003238 clear_tv(&var2);
3239 return ret;
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242
3243 return OK;
3244}
3245
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003246/*
3247 * Make a copy of blob "tv1" and append blob "tv2".
3248 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 void
3250eval_addblob(typval_T *tv1, typval_T *tv2)
3251{
3252 blob_T *b1 = tv1->vval.v_blob;
3253 blob_T *b2 = tv2->vval.v_blob;
3254 blob_T *b = blob_alloc();
3255 int i;
3256
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003257 if (b == NULL)
3258 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003260 for (i = 0; i < blob_len(b1); i++)
3261 ga_append(&b->bv_ga, blob_get(b1, i));
3262 for (i = 0; i < blob_len(b2); i++)
3263 ga_append(&b->bv_ga, blob_get(b2, i));
3264
3265 clear_tv(tv1);
3266 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267}
3268
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003269/*
3270 * Make a copy of list "tv1" and append list "tv2".
3271 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 int
3273eval_addlist(typval_T *tv1, typval_T *tv2)
3274{
3275 typval_T var3;
3276
3277 // concatenate Lists
3278 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3279 {
3280 clear_tv(tv1);
3281 clear_tv(tv2);
3282 return FAIL;
3283 }
3284 clear_tv(tv1);
3285 *tv1 = var3;
3286 return OK;
3287}
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003290 * Handle the bitwise left/right shift operator expression:
3291 * var1 << var2
3292 * var1 >> var2
3293 *
3294 * "arg" must point to the first non-white of the expression.
3295 * "arg" is advanced to just after the recognized expression.
3296 *
3297 * Return OK or FAIL.
3298 */
3299 static int
3300eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3301{
3302 /*
3303 * Get the first expression.
3304 */
3305 if (eval6(arg, rettv, evalarg) == FAIL)
3306 return FAIL;
3307
3308 /*
3309 * Repeat computing, until no '<<' or '>>' is following.
3310 */
3311 for (;;)
3312 {
3313 char_u *p;
3314 int getnext;
3315 exprtype_T type;
3316 int evaluate;
3317 typval_T var2;
3318 int vim9script;
3319
3320 p = eval_next_non_blank(*arg, evalarg, &getnext);
3321 if (p[0] == '<' && p[1] == '<')
3322 type = EXPR_LSHIFT;
3323 else if (p[0] == '>' && p[1] == '>')
3324 type = EXPR_RSHIFT;
3325 else
3326 return OK;
3327
3328 // Handle a bitwise left or right shift operator
3329 if (rettv->v_type != VAR_NUMBER)
3330 {
3331 // left operand should be a number
3332 emsg(_(e_bitshift_ops_must_be_number));
3333 clear_tv(rettv);
3334 return FAIL;
3335 }
3336
3337 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3338 vim9script = in_vim9script();
3339 if (getnext)
3340 {
3341 *arg = eval_next_line(*arg, evalarg);
3342 p = *arg;
3343 }
3344 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3345 {
3346 error_white_both(*arg, 2);
3347 clear_tv(rettv);
3348 return FAIL;
3349 }
3350
3351 /*
3352 * Get the second variable.
3353 */
3354 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3355 {
3356 error_white_both(p, 2);
3357 clear_tv(rettv);
3358 return FAIL;
3359 }
3360 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3361 if (eval6(arg, &var2, evalarg) == FAIL)
3362 {
3363 clear_tv(rettv);
3364 return FAIL;
3365 }
3366
3367 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3368 {
3369 // right operand should be a positive number
3370 if (var2.v_type != VAR_NUMBER)
3371 emsg(_(e_bitshift_ops_must_be_number));
3372 else
dundargocc57b5bc2022-11-02 13:30:51 +00003373 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003374 clear_tv(rettv);
3375 clear_tv(&var2);
3376 return FAIL;
3377 }
3378
3379 if (evaluate)
3380 {
3381 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3382 // shifting more bits than we have always results in zero
3383 rettv->vval.v_number = 0;
3384 else if (type == EXPR_LSHIFT)
3385 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003386 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003387 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003388 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003389 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003390 }
3391
3392 clear_tv(&var2);
3393 }
3394
3395 return OK;
3396}
3397
3398/*
3399 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003400 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003402 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003403 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 *
3405 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003406 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 *
3408 * Return OK or FAIL.
3409 */
3410 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003411eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003414 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003416 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return FAIL;
3418
3419 /*
3420 * Repeat computing, until no '+', '-' or '.' is following.
3421 */
3422 for (;;)
3423 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003424 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003425 int getnext;
3426 char_u *p;
3427 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003428 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 int concat;
3430 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003431 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003432
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003433 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003434 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003435 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436 p = eval_next_non_blank(*arg, evalarg, &getnext);
3437 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003438 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003439 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3440 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003442 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3443 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003444
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003445 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003446 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003448 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003449 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003450 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003451 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003452 {
mityu4ac198c2021-05-28 17:52:40 +02003453 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003454 clear_tv(rettv);
3455 return FAIL;
3456 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003457 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003458 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003459 if ((op != '+' || (rettv->v_type != VAR_LIST
3460 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003461 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003462 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003463 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003464 int error = FALSE;
3465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003466 // For "list + ...", an illegal use of the first operand as
3467 // a number cannot be determined before evaluating the 2nd
3468 // operand: if this is also a list, all is ok.
3469 // For "something . ...", "something - ..." or "non-list + ...",
3470 // we know that the first operand needs to be a string or number
3471 // without evaluating the 2nd operand. So check before to avoid
3472 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003473 if (op != '.')
3474 tv_get_number_chk(rettv, &error);
3475 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476 {
3477 clear_tv(rettv);
3478 return FAIL;
3479 }
3480 }
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /*
3483 * Get the second variable.
3484 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003485 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003486 {
mityu89dcb4d2021-05-28 13:50:17 +02003487 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003488 clear_tv(rettv);
3489 return FAIL;
3490 }
3491 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003492 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return FAIL;
3496 }
3497
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003498 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
3500 /*
3501 * Compute the result.
3502 */
3503 if (op == '.')
3504 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003505 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3506 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003507 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003508
Bram Moolenaar2e086612020-08-25 22:37:48 +02003509 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003510 || var2.v_type == VAR_CHANNEL
3511 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003512 semsg(_(e_using_invalid_value_as_string_str),
3513 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003514 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003515 {
3516 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3517 var2.vval.v_float);
3518 s2 = buf2;
3519 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003520 else
3521 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003522 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003523 {
3524 clear_tv(rettv);
3525 clear_tv(&var2);
3526 return FAIL;
3527 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003528 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003529 clear_tv(rettv);
3530 rettv->v_type = VAR_STRING;
3531 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003533 else if (op == '+' && rettv->v_type == VAR_BLOB
3534 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003536 else if (op == '+' && rettv->v_type == VAR_LIST
3537 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003538 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003540 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
3543 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003544 int error = FALSE;
3545 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003546 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003547
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003549 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550 f1 = rettv->vval.v_float;
3551 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003552 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003555 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003556 if (error)
3557 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003558 // This can only happen for "list + non-list" or
3559 // "blob + non-blob". For "non-list + ..." or
3560 // "something - ...", we returned before evaluating the
3561 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003562 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003563 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 return FAIL;
3565 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 if (var2.v_type == VAR_FLOAT)
3567 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003569 if (var2.v_type == VAR_FLOAT)
3570 {
3571 f2 = var2.vval.v_float;
3572 n2 = 0;
3573 }
3574 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003576 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003577 if (error)
3578 {
3579 clear_tv(rettv);
3580 clear_tv(&var2);
3581 return FAIL;
3582 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 if (rettv->v_type == VAR_FLOAT)
3584 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003586 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3590 {
3591 if (op == '+')
3592 f1 = f1 + f2;
3593 else
3594 f1 = f1 - f2;
3595 rettv->v_type = VAR_FLOAT;
3596 rettv->vval.v_float = f1;
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003599 {
3600 if (op == '+')
3601 n1 = n1 + n2;
3602 else
3603 n1 = n1 - n2;
3604 rettv->v_type = VAR_NUMBER;
3605 rettv->vval.v_number = n1;
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610 }
3611 return OK;
3612}
3613
3614/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003615 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * * number multiplication
3617 * / number division
3618 * % number modulo
3619 *
3620 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003621 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 *
3623 * Return OK or FAIL.
3624 */
3625 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003626eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627 char_u **arg,
3628 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003629 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003632 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003635 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003637 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639
3640 /*
3641 * Repeat computing, until no '*', '/' or '%' is following.
3642 */
3643 for (;;)
3644 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003645 int evaluate;
3646 int getnext;
3647 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003648 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003649 int op;
3650 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003651 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003652 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003653
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003654 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003655 p = eval_next_non_blank(*arg, evalarg, &getnext);
3656 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003657 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003659
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003660 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003661 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003662 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003663 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003664 {
3665 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3666 {
mityu4ac198c2021-05-28 17:52:40 +02003667 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003668 clear_tv(rettv);
3669 return FAIL;
3670 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003671 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003674 f1 = 0;
3675 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003676 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003677 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003679 if (rettv->v_type == VAR_FLOAT)
3680 {
3681 f1 = rettv->vval.v_float;
3682 use_float = TRUE;
3683 n1 = 0;
3684 }
3685 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003686 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003687 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 if (error)
3689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 else
3692 n1 = 0;
3693
3694 /*
3695 * Get the second variable.
3696 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003697 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3698 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003699 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003700 clear_tv(rettv);
3701 return FAIL;
3702 }
3703 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003704 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 return FAIL;
3706
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003707 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003709 if (var2.v_type == VAR_FLOAT)
3710 {
3711 if (!use_float)
3712 {
3713 f1 = n1;
3714 use_float = TRUE;
3715 }
3716 f2 = var2.vval.v_float;
3717 n2 = 0;
3718 }
3719 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003721 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 clear_tv(&var2);
3723 if (error)
3724 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003725 if (use_float)
3726 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /*
3730 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003731 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 if (op == '*')
3736 f1 = f1 * f2;
3737 else if (op == '/')
3738 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003739#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003740 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003741 if (f2 == 0.0)
3742 {
3743 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003745 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003746 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003747 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003748 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003749 }
3750 else
3751 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003752#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 // We rely on the floating point library to handle divide
3754 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003755 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003756#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003759 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003760 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003761 return FAIL;
3762 }
3763 rettv->v_type = VAR_FLOAT;
3764 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 else
3767 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003768 int failed = FALSE;
3769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003770 if (op == '*')
3771 n1 = n1 * n2;
3772 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003773 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003775 n1 = num_modulus(n1, n2, &failed);
3776 if (failed)
3777 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003779 rettv->v_type = VAR_NUMBER;
3780 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
3784
3785 return OK;
3786}
3787
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003788/*
3789 * Handle a type cast before a base level expression.
3790 * "arg" must point to the first non-white of the expression.
3791 * "arg" is advanced to just after the recognized expression.
3792 * Return OK or FAIL.
3793 */
3794 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003795eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003796 char_u **arg,
3797 typval_T *rettv,
3798 evalarg_T *evalarg,
3799 int want_string) // after "." operator
3800{
3801 type_T *want_type = NULL;
3802 garray_T type_list; // list of pointers to allocated types
3803 int res;
3804 int evaluate = evalarg == NULL ? 0
3805 : (evalarg->eval_flags & EVAL_EVALUATE);
3806
3807 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003808 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3809 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003810 {
3811 ++*arg;
3812 ga_init2(&type_list, sizeof(type_T *), 10);
3813 want_type = parse_type(arg, &type_list, TRUE);
3814 if (want_type == NULL && (evaluate || **arg != '>'))
3815 {
3816 clear_type_list(&type_list);
3817 return FAIL;
3818 }
3819
3820 if (**arg != '>')
3821 {
3822 if (*skipwhite(*arg) == '>')
3823 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3824 else
3825 emsg(_(e_missing_gt));
3826 clear_type_list(&type_list);
3827 return FAIL;
3828 }
3829 ++*arg;
3830 *arg = skipwhite_and_linebreak(*arg, evalarg);
3831 }
3832
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003833 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003834
3835 if (want_type != NULL && evaluate)
3836 {
3837 if (res == OK)
3838 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003839 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3840 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003841
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003842 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003843 {
3844 if (want_type == &t_bool && actual != &t_bool
3845 && (actual->tt_flags & TTFLAG_BOOL_OK))
3846 {
3847 int n = tv2bool(rettv);
3848
3849 // can use "0" and "1" for boolean in some places
3850 clear_tv(rettv);
3851 rettv->v_type = VAR_BOOL;
3852 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3853 }
3854 else
3855 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003856 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003857
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003858 where.wt_variable = TRUE;
3859 res = check_type(want_type, actual, TRUE, where);
3860 }
3861 }
3862 }
3863 clear_type_list(&type_list);
3864 }
3865
3866 return res;
3867}
3868
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003869 int
3870eval_leader(char_u **arg, int vim9)
3871{
3872 char_u *s = *arg;
3873 char_u *p = *arg;
3874
3875 while (*p == '!' || *p == '-' || *p == '+')
3876 {
3877 char_u *n = skipwhite(p + 1);
3878
3879 // ++, --, -+ and +- are not accepted in Vim9 script
3880 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3881 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003882 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003883 return FAIL;
3884 }
3885 p = n;
3886 }
3887 *arg = p;
3888 return OK;
3889}
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003892 * Check for a predefined value "true", "false" and "null.*".
3893 * Return OK when recognized.
3894 */
3895 int
3896handle_predefined(char_u *s, int len, typval_T *rettv)
3897{
3898 switch (len)
3899 {
3900 case 4: if (STRNCMP(s, "true", 4) == 0)
3901 {
3902 rettv->v_type = VAR_BOOL;
3903 rettv->vval.v_number = VVAL_TRUE;
3904 return OK;
3905 }
3906 if (STRNCMP(s, "null", 4) == 0)
3907 {
3908 rettv->v_type = VAR_SPECIAL;
3909 rettv->vval.v_number = VVAL_NULL;
3910 return OK;
3911 }
3912 break;
3913 case 5: if (STRNCMP(s, "false", 5) == 0)
3914 {
3915 rettv->v_type = VAR_BOOL;
3916 rettv->vval.v_number = VVAL_FALSE;
3917 return OK;
3918 }
3919 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003920 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3921 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003922#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003923 rettv->v_type = VAR_JOB;
3924 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003925#else
3926 rettv->v_type = VAR_SPECIAL;
3927 rettv->vval.v_number = VVAL_NULL;
3928#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003929 return OK;
3930 }
3931 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003932 case 9:
3933 if (STRNCMP(s, "null_", 5) != 0)
3934 break;
3935 if (STRNCMP(s + 5, "list", 4) == 0)
3936 {
3937 rettv->v_type = VAR_LIST;
3938 rettv->vval.v_list = NULL;
3939 return OK;
3940 }
3941 if (STRNCMP(s + 5, "dict", 4) == 0)
3942 {
3943 rettv->v_type = VAR_DICT;
3944 rettv->vval.v_dict = NULL;
3945 return OK;
3946 }
3947 if (STRNCMP(s + 5, "blob", 4) == 0)
3948 {
3949 rettv->v_type = VAR_BLOB;
3950 rettv->vval.v_blob = NULL;
3951 return OK;
3952 }
3953 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003954 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3955 {
3956 rettv->v_type = VAR_CLASS;
3957 rettv->vval.v_class = NULL;
3958 return OK;
3959 }
3960 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003961 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3962 {
3963 rettv->v_type = VAR_STRING;
3964 rettv->vval.v_string = NULL;
3965 return OK;
3966 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003967 if (STRNCMP(s, "null_object", 11) == 0)
3968 {
3969 rettv->v_type = VAR_OBJECT;
3970 rettv->vval.v_object = NULL;
3971 return OK;
3972 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003973 break;
3974 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003975 if (STRNCMP(s, "null_channel", 12) == 0)
3976 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003977#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003978 rettv->v_type = VAR_CHANNEL;
3979 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003980#else
3981 rettv->v_type = VAR_SPECIAL;
3982 rettv->vval.v_number = VVAL_NULL;
3983#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003984 return OK;
3985 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003986 if (STRNCMP(s, "null_partial", 12) == 0)
3987 {
3988 rettv->v_type = VAR_PARTIAL;
3989 rettv->vval.v_partial = NULL;
3990 return OK;
3991 }
3992 break;
3993 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3994 {
3995 rettv->v_type = VAR_FUNC;
3996 rettv->vval.v_string = NULL;
3997 return OK;
3998 }
3999 break;
4000 }
4001 return FAIL;
4002}
4003
4004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * Handle sixth level expression:
4006 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004007 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004008 * "string" string constant
4009 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * &option-name option value
4011 * @r register contents
4012 * identifier variable value
4013 * function() function call
4014 * $VAR environment variable
4015 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004016 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004018 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004019 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 *
4021 * Also handle:
4022 * ! in front logical NOT
4023 * - in front unary minus
4024 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 * trailing [] subscript in String or List
4026 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004027 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 *
4029 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02004030 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * Return OK or FAIL.
4033 */
4034 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004035eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004036 char_u **arg,
4037 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004038 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004041 int evaluate = evalarg != NULL
4042 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 int len;
4044 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004045 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u *start_leader, *end_leader;
4047 int ret = OK;
4048 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004049 static int recurse = 0;
4050 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004054 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004059 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
4061 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004062 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 end_leader = *arg;
4065
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004066 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004067 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004068 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004069 ++*arg;
4070 return FAIL;
4071 }
4072
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004073 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004074 // and crash. With MSVC the stack is smaller.
4075 if (recurse ==
4076#ifdef _MSC_VER
4077 300
4078#else
4079 1000
4080#endif
4081 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004082 {
4083 semsg(_(e_expression_too_recursive_str), *arg);
4084 return FAIL;
4085 }
4086 ++recurse;
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 switch (**arg)
4089 {
4090 /*
4091 * Number constant.
4092 */
4093 case '0':
4094 case '1':
4095 case '2':
4096 case '3':
4097 case '4':
4098 case '5':
4099 case '6':
4100 case '7':
4101 case '8':
4102 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004103 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004104
4105 // Apply prefixed "-" and "+" now. Matters especially when
4106 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004107 if (ret == OK && evaluate && end_leader > start_leader
4108 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004109 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 break;
4111
4112 /*
4113 * String constant: "string".
4114 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004115 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117
4118 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004119 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004121 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004122 break;
4123
4124 /*
4125 * List: [expr, expr]
4126 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004127 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129
4130 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004131 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004132 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004133 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004134 {
4135 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4136 }
4137 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004138 {
4139 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004140 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004141 }
4142 else
4143 ret = NOTDONE;
4144 break;
4145
4146 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004147 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004148 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004149 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004150 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004151 ret = NOTDONE;
4152 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004153 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004154 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004155 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 break;
4157
4158 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004159 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004161 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 break;
4163
4164 /*
4165 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004166 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 */
LemonBoy2eaef102022-05-06 13:14:50 +01004168 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4169 ret = eval_interp_string(arg, rettv, evaluate);
4170 else
4171 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 break;
4173
4174 /*
4175 * Register contents: @r.
4176 */
4177 case '@': ++*arg;
4178 if (evaluate)
4179 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004180 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004181 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004182 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004183 emsg_invreg(**arg);
4184 else
4185 {
4186 rettv->v_type = VAR_STRING;
4187 rettv->vval.v_string = get_reg_contents(**arg,
4188 GREG_EXPR_SRC);
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 if (**arg != NUL)
4192 ++*arg;
4193 break;
4194
4195 /*
4196 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004197 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004199 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004200 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004201 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004202 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004203 if (ret == OK && evaluate)
4204 {
4205 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4206
Bram Moolenaara9931532021-06-12 15:58:16 +02004207 // Compile it here to get the return type. The return
4208 // type is optional, when it's missing use t_unknown.
4209 // This is recognized in compile_return().
4210 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4211 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004212 if (compile_def_function(ufunc, FALSE,
4213 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004214 {
4215 clear_tv(rettv);
4216 ret = FAIL;
4217 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004218 }
4219 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004220 if (ret == NOTDONE)
4221 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004222 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004223 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004224
Bram Moolenaar9215f012020-06-27 21:18:00 +02004225 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004226 if (**arg == ')')
4227 ++*arg;
4228 else if (ret == OK)
4229 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004230 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004231 clear_tv(rettv);
4232 ret = FAIL;
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 break;
4236
Bram Moolenaar8c711452005-01-14 21:53:12 +00004237 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 break;
4239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240
4241 if (ret == NOTDONE)
4242 {
4243 /*
4244 * Must be a variable or function name.
4245 * Can also be a curly-braces kind of name: {expr}.
4246 */
4247 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004248 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004249 if (alias != NULL)
4250 s = alias;
4251
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004252 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004253 ret = FAIL;
4254 else
4255 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004256 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4257
Bram Moolenaar4525a572022-02-13 11:57:33 +00004258 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004259 {
4260 emsg(_(e_cannot_use_underscore_here));
4261 ret = FAIL;
4262 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004263 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004264 && s[0] == 's' && s[1] == ':')
4265 {
4266 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4267 ret = FAIL;
4268 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004269 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004270 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004271 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004272 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004273 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004274 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004275 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004276 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004277 // get the value of "true", "false", etc. or a variable
4278 ret = FAIL;
4279 if (vim9script)
4280 ret = handle_predefined(s, len, rettv);
4281 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004282 {
4283 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004284 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004285 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004286 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004287 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004288 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004289 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004290 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004291 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004292 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004294 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004295 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004296 }
4297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004298 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4299 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004300 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004301 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /*
4304 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4305 */
4306 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004307 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004308
4309 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004310 return ret;
4311}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004313/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004314 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004315 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004316 * Adjusts "end_leaderp" until it is at "start_leader".
4317 */
4318 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004319eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004320 typval_T *rettv,
4321 int numeric_only,
4322 char_u *start_leader,
4323 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004324{
4325 char_u *end_leader = *end_leaderp;
4326 int ret = OK;
4327 int error = FALSE;
4328 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004329 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004330 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004331 float_T f = 0.0;
4332
4333 if (rettv->v_type == VAR_FLOAT)
4334 f = rettv->vval.v_float;
4335 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004336 {
4337 while (VIM_ISWHITE(end_leader[-1]))
4338 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004339 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004340 val = tv2bool(rettv);
4341 else
4342 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004344 if (error)
4345 {
4346 clear_tv(rettv);
4347 ret = FAIL;
4348 }
4349 else
4350 {
4351 while (end_leader > start_leader)
4352 {
4353 --end_leader;
4354 if (*end_leader == '!')
4355 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004356 if (numeric_only)
4357 {
4358 ++end_leader;
4359 break;
4360 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004361 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004362 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004364 {
4365 rettv->v_type = VAR_BOOL;
4366 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4367 }
4368 else
4369 f = !f;
4370 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004371 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004372 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004373 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004374 type = VAR_BOOL;
4375 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004376 }
4377 else if (*end_leader == '-')
4378 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004379 if (rettv->v_type == VAR_FLOAT)
4380 f = -f;
4381 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004382 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004383 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004384 type = VAR_NUMBER;
4385 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004386 }
4387 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004388 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004391 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 else
4394 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004395 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004396 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004397 rettv->v_type = type;
4398 else
4399 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004400 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004403 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return ret;
4405}
4406
4407/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004408 * Call the function referred to in "rettv".
4409 */
4410 static int
4411call_func_rettv(
4412 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004413 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004414 typval_T *rettv,
4415 int evaluate,
4416 dict_T *selfdict,
4417 typval_T *basetv)
4418{
4419 partial_T *pt = NULL;
4420 funcexe_T funcexe;
4421 typval_T functv;
4422 char_u *s;
4423 int ret;
4424
4425 // need to copy the funcref so that we can clear rettv
4426 if (evaluate)
4427 {
4428 functv = *rettv;
4429 rettv->v_type = VAR_UNKNOWN;
4430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004432 if (functv.v_type == VAR_PARTIAL)
4433 {
4434 pt = functv.vval.v_partial;
4435 s = partial_name(pt);
4436 }
4437 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004438 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004439 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004440 if (s == NULL || *s == NUL)
4441 {
4442 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004443 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004444 goto theend;
4445 }
4446 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004447 }
4448 else
4449 s = (char_u *)"";
4450
Bram Moolenaara80faa82020-04-12 19:37:17 +02004451 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004452 funcexe.fe_firstline = curwin->w_cursor.lnum;
4453 funcexe.fe_lastline = curwin->w_cursor.lnum;
4454 funcexe.fe_evaluate = evaluate;
4455 funcexe.fe_partial = pt;
4456 funcexe.fe_selfdict = selfdict;
4457 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004458 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004459
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004460theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004461 // Clear the funcref afterwards, so that deleting it while
4462 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004463 if (evaluate)
4464 clear_tv(&functv);
4465
4466 return ret;
4467}
4468
4469/*
4470 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004471 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004472 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4473 */
4474 static int
4475eval_lambda(
4476 char_u **arg,
4477 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004478 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004480{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004481 int evaluate = evalarg != NULL
4482 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004483 typval_T base = *rettv;
4484 int ret;
4485
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004486 rettv->v_type = VAR_UNKNOWN;
4487
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004488 if (**arg == '{')
4489 {
4490 // ->{lambda}()
4491 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4492 }
4493 else
4494 {
4495 // ->(lambda)()
4496 ++*arg;
4497 ret = eval1(arg, rettv, evalarg);
4498 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004499 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004500 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004501 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004502 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004503 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004504 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4505 && rettv->v_type != VAR_PARTIAL)
4506 {
4507 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4508 return FAIL;
4509 }
4510 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004511 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004512 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004513 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004514
4515 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004516 {
4517 if (verbose)
4518 {
4519 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004520 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004521 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004522 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004523 }
4524 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004525 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004526 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004527 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004528 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004529
4530 // Clear the funcref afterwards, so that deleting it while
4531 // evaluating the arguments is possible (see test55).
4532 if (evaluate)
4533 clear_tv(&base);
4534
4535 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004536}
4537
4538/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004539 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004540 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004541 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4542 */
4543 static int
4544eval_method(
4545 char_u **arg,
4546 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004547 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004549{
4550 char_u *name;
4551 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004552 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004553 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004554 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004555 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004556 int evaluate = evalarg != NULL
4557 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004558
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004559 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004560
Bram Moolenaarac92e252019-08-03 21:58:38 +02004561 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004562 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004563 if (alias != NULL)
4564 name = alias;
4565
4566 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004567 {
4568 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004569 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004570 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004571 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004572 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004573 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004574 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004575
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004576 // If there is no "(" immediately following, but there is further on,
4577 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4578 // Does not handle anything where "(" is part of the expression.
4579 *arg = skipwhite(*arg);
4580
4581 if (**arg != '(' && alias == NULL
4582 && (paren = vim_strchr(*arg, '(')) != NULL)
4583 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004584 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004585
4586 // Truncate the name a the "(". Avoid trying to get another line
4587 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004588 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004589 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4590 if (evalarg != NULL)
4591 {
4592 getline = evalarg->eval_getline;
4593 evalarg->eval_getline = NULL;
4594 }
4595
4596 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004597 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004598 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004599 *arg = name + len;
4600 ret = FAIL;
4601 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004602 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004603 {
4604 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004605 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004606 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004607
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004608 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004609 if (getline != NULL)
4610 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004611 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004612
4613 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004614 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004615 *arg = skipwhite(*arg);
4616
4617 if (**arg != '(')
4618 {
4619 if (verbose)
4620 semsg(_(e_missing_parenthesis_str), name);
4621 ret = FAIL;
4622 }
4623 else if (VIM_ISWHITE((*arg)[-1]))
4624 {
4625 if (verbose)
4626 emsg(_(e_no_white_space_allowed_before_parenthesis));
4627 ret = FAIL;
4628 }
4629 else
4630 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004631 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004632 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004633 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004634
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004635 // Clear the funcref afterwards, so that deleting it while
4636 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004637 if (evaluate)
4638 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004639 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004640
Bram Moolenaarac92e252019-08-03 21:58:38 +02004641 return ret;
4642}
4643
4644/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004645 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4646 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4648 */
4649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650eval_index(
4651 char_u **arg,
4652 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004653 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004656 int evaluate = evalarg != NULL
4657 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004659 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004662 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004663 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004665 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4666 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004668 init_tv(&var1);
4669 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 /*
4673 * dict.name
4674 */
4675 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004678 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004680 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 }
4682 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 /*
4685 * something[idx]
4686 *
4687 * Get the (first) variable from inside the [].
4688 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004689 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 if (**arg == ':')
4691 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004692 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004694 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004695 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004696 semsg(_(e_white_space_required_before_and_after_str_at_str),
4697 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004698 clear_tv(&var1);
4699 return FAIL;
4700 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004701 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004703 int error = FALSE;
4704
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004705 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004706 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004707 && var1.v_type == VAR_FLOAT)
4708 {
4709 var1.vval.v_string = typval_tostring(&var1, TRUE);
4710 var1.v_type = VAR_STRING;
4711 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004712
Bram Moolenaar4525a572022-02-13 11:57:33 +00004713 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004714 tv_get_number_chk(&var1, &error);
4715 else
4716 error = tv_get_string_chk(&var1) == NULL;
4717 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004718 {
4719 // not a number or string
4720 clear_tv(&var1);
4721 return FAIL;
4722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004724
4725 /*
4726 * Get the second variable from inside the [:].
4727 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004728 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 if (**arg == ':')
4730 {
4731 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004732 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004733 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004734 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004735 semsg(_(e_white_space_required_before_and_after_str_at_str),
4736 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004737 if (!empty1)
4738 clear_tv(&var1);
4739 return FAIL;
4740 }
4741 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 if (**arg == ']')
4743 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004744 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 if (!empty1)
4747 clear_tv(&var1);
4748 return FAIL;
4749 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004750 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004752 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 if (!empty1)
4754 clear_tv(&var1);
4755 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 return FAIL;
4757 }
4758 }
4759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004761 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004762 if (**arg != ']')
4763 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004764 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004765 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 clear_tv(&var1);
4767 if (range)
4768 clear_tv(&var2);
4769 return FAIL;
4770 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004771 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 }
4773
4774 if (evaluate)
4775 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004776 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004777 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004778 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004779
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004780 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783 clear_tv(&var2);
4784 return res;
4785 }
4786 return OK;
4787}
4788
4789/*
4790 * Check if "rettv" can have an [index] or [sli:ce]
4791 */
4792 int
4793check_can_index(typval_T *rettv, int evaluate, int verbose)
4794{
4795 switch (rettv->v_type)
4796 {
4797 case VAR_FUNC:
4798 case VAR_PARTIAL:
4799 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004800 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004801 return FAIL;
4802 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004803 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004804 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004805 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004806 case VAR_BOOL:
4807 case VAR_SPECIAL:
4808 case VAR_JOB:
4809 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004810 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004811 case VAR_CLASS:
4812 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004813 if (verbose)
4814 emsg(_(e_cannot_index_special_variable));
4815 return FAIL;
4816 case VAR_UNKNOWN:
4817 case VAR_ANY:
4818 case VAR_VOID:
4819 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004821 emsg(_(e_cannot_index_special_variable));
4822 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004824 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004826 case VAR_STRING:
4827 case VAR_LIST:
4828 case VAR_DICT:
4829 case VAR_BLOB:
4830 break;
4831 case VAR_NUMBER:
4832 if (in_vim9script())
4833 emsg(_(e_cannot_index_number));
4834 break;
4835 }
4836 return OK;
4837}
4838
4839/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004840 * slice() function
4841 */
4842 void
4843f_slice(typval_T *argvars, typval_T *rettv)
4844{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004845 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004846 && ((argvars[0].v_type != VAR_STRING
4847 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004848 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004849 && check_for_list_arg(argvars, 0) == FAIL)
4850 || check_for_number_arg(argvars, 1) == FAIL
4851 || check_for_opt_number_arg(argvars, 2) == FAIL))
4852 return;
4853
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004854 if (check_can_index(argvars, TRUE, FALSE) != OK)
4855 return;
4856
4857 copy_tv(argvars, rettv);
4858 eval_index_inner(rettv, TRUE, argvars + 1,
4859 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4860 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004861}
4862
4863/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004864 * Apply index or range to "rettv".
4865 * "var1" is the first index, NULL for [:expr].
4866 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004867 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4868 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004869 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4870 */
4871 int
4872eval_index_inner(
4873 typval_T *rettv,
4874 int is_range,
4875 typval_T *var1,
4876 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004877 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878 char_u *key,
4879 int keylen,
4880 int verbose)
4881{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004882 varnumber_T n1, n2 = 0;
4883 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004884
4885 n1 = 0;
4886 if (var1 != NULL && rettv->v_type != VAR_DICT)
4887 n1 = tv_get_number(var1);
4888
4889 if (is_range)
4890 {
4891 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004893 if (verbose)
4894 emsg(_(e_cannot_slice_dictionary));
4895 return FAIL;
4896 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004897 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004898 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004899 else
4900 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004901 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004902
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004903 switch (rettv->v_type)
4904 {
4905 case VAR_UNKNOWN:
4906 case VAR_ANY:
4907 case VAR_VOID:
4908 case VAR_FUNC:
4909 case VAR_PARTIAL:
4910 case VAR_FLOAT:
4911 case VAR_BOOL:
4912 case VAR_SPECIAL:
4913 case VAR_JOB:
4914 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004915 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004916 case VAR_CLASS:
4917 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004918 break; // not evaluating, skipping over subscript
4919
4920 case VAR_NUMBER:
4921 case VAR_STRING:
4922 {
4923 char_u *s = tv_get_string(rettv);
4924
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004926 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004927 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004928 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004929 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004930 else
4931 s = char_from_string(s, n1);
4932 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004933 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 // The resulting variable is a substring. If the indexes
4936 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004937 if (n1 < 0)
4938 {
4939 n1 = len + n1;
4940 if (n1 < 0)
4941 n1 = 0;
4942 }
4943 if (n2 < 0)
4944 n2 = len + n2;
4945 else if (n2 >= len)
4946 n2 = len;
4947 if (n1 >= len || n2 < 0 || n1 > n2)
4948 s = NULL;
4949 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004950 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 }
4952 else
4953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // The resulting variable is a string of a single
4955 // character. If the index is too big or negative the
4956 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004957 if (n1 >= len || n1 < 0)
4958 s = NULL;
4959 else
4960 s = vim_strnsave(s + n1, 1);
4961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004962 clear_tv(rettv);
4963 rettv->v_type = VAR_STRING;
4964 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004965 }
4966 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004968 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004969 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4970 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004971 break;
4972
4973 case VAR_LIST:
4974 if (var1 == NULL)
4975 n1 = 0;
4976 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004977 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004978 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004979 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004980 return FAIL;
4981 break;
4982
4983 case VAR_DICT:
4984 {
4985 dictitem_T *item;
4986 typval_T tmp;
4987
4988 if (key == NULL)
4989 {
4990 key = tv_get_string_chk(var1);
4991 if (key == NULL)
4992 return FAIL;
4993 }
4994
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004995 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004996
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004997 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004998 {
4999 if (verbose)
5000 {
5001 if (keylen > 0)
5002 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005003 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005004 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005005 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00005006 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005007
5008 copy_tv(&item->di_tv, &tmp);
5009 clear_tv(rettv);
5010 *rettv = tmp;
5011 }
5012 break;
5013 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 return OK;
5015}
5016
5017/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02005018 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005019 */
5020 char_u *
5021partial_name(partial_T *pt)
5022{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02005023 if (pt != NULL)
5024 {
5025 if (pt->pt_name != NULL)
5026 return pt->pt_name;
5027 if (pt->pt_func != NULL)
5028 return pt->pt_func->uf_name;
5029 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005030 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005031}
5032
Bram Moolenaarddecc252016-04-06 22:59:37 +02005033 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005034partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005035{
5036 int i;
5037
5038 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005039 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005040 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005041 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005042 if (pt->pt_name != NULL)
5043 {
5044 func_unref(pt->pt_name);
5045 vim_free(pt->pt_name);
5046 }
5047 else
5048 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005049
Bram Moolenaar54656012021-06-09 20:50:46 +02005050 // "out_up" is no longer used, decrement refcount on partial that owns it.
5051 partial_unref(pt->pt_outer.out_up_partial);
5052
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005053 // Using pt_outer from another partial.
5054 partial_unref(pt->pt_outer_partial);
5055
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005056 // Decrease the reference count for the context of a closure. If down
5057 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005058 if (pt->pt_funcstack != NULL)
5059 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005060 --pt->pt_funcstack->fs_refcount;
5061 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005062 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005063 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005064 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5065 if (pt->pt_loopvars[i] != NULL)
5066 {
5067 --pt->pt_loopvars[i]->lvs_refcount;
5068 loopvars_check_refcount(pt->pt_loopvars[i]);
5069 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005070
Bram Moolenaarddecc252016-04-06 22:59:37 +02005071 vim_free(pt);
5072}
5073
5074/*
5075 * Unreference a closure: decrement the reference count and free it when it
5076 * becomes zero.
5077 */
5078 void
5079partial_unref(partial_T *pt)
5080{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005081 if (pt == NULL)
5082 return;
5083
5084 int done = FALSE;
5085
5086 if (--pt->pt_refcount <= 0)
5087 partial_free(pt);
5088
5089 // If the reference count goes down to one, the funcstack may be the
5090 // only reference and can be freed if no other partials reference it.
5091 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005092 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005093 // careful: if the funcstack is freed it may contain this partial
5094 // and it gets freed as well
5095 if (pt->pt_funcstack != NULL)
5096 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005097
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005098 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005099 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005100 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005101
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005102 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5103 if (pt->pt_loopvars[depth] != NULL
5104 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005105 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005106 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005107 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005108}
5109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005110/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005111 * Return the next (unique) copy ID.
5112 * Used for serializing nested structures.
5113 */
5114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005115get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005116{
5117 current_copyID += COPYID_INC;
5118 return current_copyID;
5119}
5120
5121/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005122 * Garbage collection for lists and dictionaries.
5123 *
5124 * We use reference counts to be able to free most items right away when they
5125 * are no longer used. But for composite items it's possible that it becomes
5126 * unused while the reference count is > 0: When there is a recursive
5127 * reference. Example:
5128 * :let l = [1, 2, 3]
5129 * :let d = {9: l}
5130 * :let l[1] = d
5131 *
5132 * Since this is quite unusual we handle this with garbage collection: every
5133 * once in a while find out which lists and dicts are not referenced from any
5134 * variable.
5135 *
5136 * Here is a good reference text about garbage collection (refers to Python
5137 * but it applies to all reference-counting mechanisms):
5138 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005139 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005140
5141/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005142 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005143 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005144 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005145 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005146 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005147garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005148{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005149 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005150 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005151 buf_T *buf;
5152 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005153 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005154 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005155
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005156 if (!testing)
5157 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005158 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005159 want_garbage_collect = FALSE;
5160 may_garbage_collect = FALSE;
5161 garbage_collect_at_exit = FALSE;
5162 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005163
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005164 // The execution stack can grow big, limit the size.
5165 if (exestack.ga_maxlen - exestack.ga_len > 500)
5166 {
5167 size_t new_len;
5168 char_u *pp;
5169 int n;
5170
5171 // Keep 150% of the current size, with a minimum of the growth size.
5172 n = exestack.ga_len / 2;
5173 if (n < exestack.ga_growsize)
5174 n = exestack.ga_growsize;
5175
5176 // Don't make it bigger though.
5177 if (exestack.ga_len + n < exestack.ga_maxlen)
5178 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005179 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005180 pp = vim_realloc(exestack.ga_data, new_len);
5181 if (pp == NULL)
5182 return FAIL;
5183 exestack.ga_maxlen = exestack.ga_len + n;
5184 exestack.ga_data = pp;
5185 }
5186 }
5187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // We advance by two because we add one for items referenced through
5189 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005190 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005191
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 /*
5193 * 1. Go through all accessible variables and mark all lists and dicts
5194 * with copyID.
5195 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // Don't free variables in the previous_funccal list unless they are only
5198 // referenced through previous_funccal. This must be first, because if
5199 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005203 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005206 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005207 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5208 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005211 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005212 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5213 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005214 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005215 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005216 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005217 abort = abort || set_ref_in_item(
5218 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005219#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005220 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005221 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5222 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005223 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005224 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005225 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5226 NULL, NULL);
5227#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005230 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005231 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5232 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005234 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005240 abort = abort || set_ref_in_functions(copyID);
5241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005242 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005244
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005245 // funcstacks keep variables for closures
5246 abort = abort || set_ref_in_funcstacks(copyID);
5247
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005248 // loopvars keep variables for loop blocks
5249 abort = abort || set_ref_in_loopvars(copyID);
5250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005251 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005252 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005253
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005254 // callbacks in buffers
5255 abort = abort || set_ref_in_buffers(copyID);
5256
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005257 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5258 abort = abort || set_ref_in_insexpand_funcs(copyID);
5259
5260 // 'operatorfunc' callback
5261 abort = abort || set_ref_in_opfunc(copyID);
5262
5263 // 'tagfunc' callback
5264 abort = abort || set_ref_in_tagfunc(copyID);
5265
5266 // 'imactivatefunc' and 'imstatusfunc' callbacks
5267 abort = abort || set_ref_in_im_funcs(copyID);
5268
Bram Moolenaar1dced572012-04-05 16:54:08 +02005269#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005271#endif
5272
Bram Moolenaardb913952012-06-29 12:54:53 +02005273#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005274 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005275#endif
5276
5277#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005278 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005279#endif
5280
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005281#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005282 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005283 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005284#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005285#ifdef FEAT_NETBEANS_INTG
5286 abort = abort || set_ref_in_nb_channel(copyID);
5287#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005288
Bram Moolenaare3188e22016-05-31 21:13:04 +02005289#ifdef FEAT_TIMERS
5290 abort = abort || set_ref_in_timer(copyID);
5291#endif
5292
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005293#ifdef FEAT_QUICKFIX
5294 abort = abort || set_ref_in_quickfix(copyID);
5295#endif
5296
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005297#ifdef FEAT_TERMINAL
5298 abort = abort || set_ref_in_term(copyID);
5299#endif
5300
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005301#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005302 abort = abort || set_ref_in_popups(copyID);
5303#endif
5304
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005305 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005306 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 /*
5308 * 2. Free lists and dictionaries that are not referenced.
5309 */
5310 did_free = free_unref_items(copyID);
5311
5312 /*
5313 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005314 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005315 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005316 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005317 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005318 else if (p_verbose > 0)
5319 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005320 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005321 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005322
5323 return did_free;
5324}
5325
5326/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005327 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005328 */
5329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005331{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005332 int did_free = FALSE;
5333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 // Let all "free" functions know that we are here. This means no
5335 // dictionaries, lists, channels or jobs are to be freed, because we will
5336 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005337 in_free_unref_items = TRUE;
5338
5339 /*
5340 * PASS 1: free the contents of the items. We don't free the items
5341 * themselves yet, so that it is possible to decrement refcount counters
5342 */
5343
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005344 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005345 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005346
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005347 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005348 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005349
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005350 // Go through the list of objects and free items without this copyID.
5351 did_free |= object_free_nonref(copyID);
5352
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 // Go through the list of jobs and free items without the copyID. This
5355 // must happen before doing channels, because jobs refer to channels, but
5356 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005357 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005360 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5361#endif
5362
5363 /*
5364 * PASS 2: free the items themselves.
5365 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005366 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005367 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005368
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 // Go through the list of jobs and free items without the copyID. This
5371 // must happen before doing channels, because jobs refer to channels, but
5372 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005373 free_unused_jobs(copyID, COPYID_MASK);
5374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 free_unused_channels(copyID, COPYID_MASK);
5377#endif
5378
5379 in_free_unref_items = FALSE;
5380
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 return did_free;
5382}
5383
5384/*
5385 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005386 * "list_stack" is used to add lists to be marked. Can be NULL.
5387 *
5388 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005389 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005391set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005392{
5393 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005395 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005396 hashtab_T *cur_ht;
5397 ht_stack_T *ht_stack = NULL;
5398 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005399
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005400 cur_ht = ht;
5401 for (;;)
5402 {
5403 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // Mark each item in the hashtab. If the item contains a hashtab
5406 // it is added to ht_stack, if it contains a list it is added to
5407 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005408 todo = (int)cur_ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00005409 FOR_ALL_HASHTAB_ITEMS(cur_ht, hi, todo)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005410 if (!HASHITEM_EMPTY(hi))
5411 {
5412 --todo;
5413 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5414 &ht_stack, list_stack);
5415 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005416 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005417
5418 if (ht_stack == NULL)
5419 break;
5420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005422 cur_ht = ht_stack->ht;
5423 tempitem = ht_stack;
5424 ht_stack = ht_stack->prev;
5425 free(tempitem);
5426 }
5427
5428 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429}
5430
Dominique Pelle748b3082022-01-08 12:41:16 +00005431#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5432 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005434 * Mark a dict and its items with "copyID".
5435 * Returns TRUE if setting references failed somehow.
5436 */
5437 int
5438set_ref_in_dict(dict_T *d, int copyID)
5439{
5440 if (d != NULL && d->dv_copyID != copyID)
5441 {
5442 d->dv_copyID = copyID;
5443 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5444 }
5445 return FALSE;
5446}
Dominique Pelle748b3082022-01-08 12:41:16 +00005447#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005448
5449/*
5450 * Mark a list and its items with "copyID".
5451 * Returns TRUE if setting references failed somehow.
5452 */
5453 int
5454set_ref_in_list(list_T *ll, int copyID)
5455{
5456 if (ll != NULL && ll->lv_copyID != copyID)
5457 {
5458 ll->lv_copyID = copyID;
5459 return set_ref_in_list_items(ll, copyID, NULL);
5460 }
5461 return FALSE;
5462}
5463
5464/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005465 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005466 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5467 *
5468 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005469 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005470 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005471set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005472{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005473 listitem_T *li;
5474 int abort = FALSE;
5475 list_T *cur_l;
5476 list_stack_T *list_stack = NULL;
5477 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005478
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005479 cur_l = l;
5480 for (;;)
5481 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005482 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 // Mark each item in the list. If the item contains a hashtab
5484 // it is added to ht_stack, if it contains a list it is added to
5485 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5487 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5488 ht_stack, &list_stack);
5489 if (list_stack == NULL)
5490 break;
5491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005492 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005493 cur_l = list_stack->list;
5494 tempitem = list_stack;
5495 list_stack = list_stack->prev;
5496 free(tempitem);
5497 }
5498
5499 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005500}
5501
5502/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005503 * Mark the partial in callback 'cb' with "copyID".
5504 */
5505 int
5506set_ref_in_callback(callback_T *cb, int copyID)
5507{
5508 typval_T tv;
5509
5510 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5511 return FALSE;
5512
5513 tv.v_type = VAR_PARTIAL;
5514 tv.vval.v_partial = cb->cb_partial;
5515 return set_ref_in_item(&tv, copyID, NULL, NULL);
5516}
5517
5518/*
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005519 * Mark the dict "dd" with "copyID".
5520 * Also see set_ref_in_item().
5521 */
5522 static int
5523set_ref_in_item_dict(
5524 dict_T *dd,
5525 int copyID,
5526 ht_stack_T **ht_stack,
5527 list_stack_T **list_stack)
5528{
5529 if (dd == NULL || dd->dv_copyID == copyID)
5530 return FALSE;
5531
5532 // Didn't see this dict yet.
5533 dd->dv_copyID = copyID;
5534 if (ht_stack == NULL)
5535 return set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5536
5537 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5538 if (newitem == NULL)
5539 return TRUE;
5540
5541 newitem->ht = &dd->dv_hashtab;
5542 newitem->prev = *ht_stack;
5543 *ht_stack = newitem;
5544
5545 return FALSE;
5546}
5547
5548/*
5549 * Mark the list "ll" with "copyID".
5550 * Also see set_ref_in_item().
5551 */
5552 static int
5553set_ref_in_item_list(
5554 list_T *ll,
5555 int copyID,
5556 ht_stack_T **ht_stack,
5557 list_stack_T **list_stack)
5558{
5559 if (ll == NULL || ll->lv_copyID == copyID)
5560 return FALSE;
5561
5562 // Didn't see this list yet.
5563 ll->lv_copyID = copyID;
5564 if (list_stack == NULL)
5565 return set_ref_in_list_items(ll, copyID, ht_stack);
5566
5567 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5568 if (newitem == NULL)
5569 return TRUE;
5570
5571 newitem->list = ll;
5572 newitem->prev = *list_stack;
5573 *list_stack = newitem;
5574
5575 return FALSE;
5576}
5577
5578/*
5579 * Mark the partial "pt" with "copyID".
5580 * Also see set_ref_in_item().
5581 */
5582 static int
5583set_ref_in_item_partial(
5584 partial_T *pt,
5585 int copyID,
5586 ht_stack_T **ht_stack,
5587 list_stack_T **list_stack)
5588{
5589 if (pt == NULL || pt->pt_copyID == copyID)
5590 return FALSE;
5591
5592 // Didn't see this partial yet.
5593 pt->pt_copyID = copyID;
5594
5595 int abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5596
5597 if (pt->pt_dict != NULL)
5598 {
5599 typval_T dtv;
5600
5601 dtv.v_type = VAR_DICT;
5602 dtv.vval.v_dict = pt->pt_dict;
5603 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5604 }
5605
5606 for (int i = 0; i < pt->pt_argc; ++i)
5607 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5608 ht_stack, list_stack);
5609 // pt_funcstack is handled in set_ref_in_funcstacks()
5610 // pt_loopvars is handled in set_ref_in_loopvars()
5611
5612 return abort;
5613}
5614
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005615#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005616/*
5617 * Mark the job "pt" with "copyID".
5618 * Also see set_ref_in_item().
5619 */
5620 static int
5621set_ref_in_item_job(
5622 job_T *job,
5623 int copyID,
5624 ht_stack_T **ht_stack,
5625 list_stack_T **list_stack)
5626{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005627 typval_T dtv;
5628
5629 if (job == NULL || job->jv_copyID == copyID)
5630 return FALSE;
5631
5632 job->jv_copyID = copyID;
5633 if (job->jv_channel != NULL)
5634 {
5635 dtv.v_type = VAR_CHANNEL;
5636 dtv.vval.v_channel = job->jv_channel;
5637 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5638 }
5639 if (job->jv_exit_cb.cb_partial != NULL)
5640 {
5641 dtv.v_type = VAR_PARTIAL;
5642 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
5643 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5644 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005645
5646 return FALSE;
5647}
5648
5649/*
5650 * Mark the channel "ch" with "copyID".
5651 * Also see set_ref_in_item().
5652 */
5653 static int
5654set_ref_in_item_channel(
5655 channel_T *ch,
5656 int copyID,
5657 ht_stack_T **ht_stack,
5658 list_stack_T **list_stack)
5659{
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005660 typval_T dtv;
5661
5662 if (ch == NULL || ch->ch_copyID == copyID)
5663 return FALSE;
5664
5665 ch->ch_copyID = copyID;
5666 for (ch_part_T part = PART_SOCK; part < PART_COUNT; ++part)
5667 {
5668 for (jsonq_T *jq = ch->ch_part[part].ch_json_head.jq_next;
5669 jq != NULL; jq = jq->jq_next)
5670 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5671 for (cbq_T *cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5672 cq = cq->cq_next)
5673 if (cq->cq_callback.cb_partial != NULL)
5674 {
5675 dtv.v_type = VAR_PARTIAL;
5676 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5677 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5678 }
5679 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5680 {
5681 dtv.v_type = VAR_PARTIAL;
5682 dtv.vval.v_partial = ch->ch_part[part].ch_callback.cb_partial;
5683 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5684 }
5685 }
5686 if (ch->ch_callback.cb_partial != NULL)
5687 {
5688 dtv.v_type = VAR_PARTIAL;
5689 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5690 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5691 }
5692 if (ch->ch_close_cb.cb_partial != NULL)
5693 {
5694 dtv.v_type = VAR_PARTIAL;
5695 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5696 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5697 }
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005698
5699 return FALSE;
5700}
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005701#endif
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005702
5703/*
5704 * Mark the class "cl" with "copyID".
5705 * Also see set_ref_in_item().
5706 */
5707 static int
5708set_ref_in_item_class(
5709 class_T *cl,
5710 int copyID,
5711 ht_stack_T **ht_stack,
5712 list_stack_T **list_stack)
5713{
5714 int abort = FALSE;
5715
5716 if (cl == NULL || cl->class_copyID == copyID
5717 || (cl->class_flags & CLASS_INTERFACE) != 0)
5718 return FALSE;
5719
5720 cl->class_copyID = copyID;
5721 for (int i = 0; !abort && i < cl->class_class_member_count; ++i)
5722 abort = abort || set_ref_in_item(
5723 &cl->class_members_tv[i],
5724 copyID, ht_stack, list_stack);
5725
5726 for (int i = 0; !abort && i < cl->class_class_function_count; ++i)
5727 abort = abort || set_ref_in_func(NULL,
5728 cl->class_class_functions[i], copyID);
5729
5730 for (int i = 0; !abort && i < cl->class_obj_method_count; ++i)
5731 abort = abort || set_ref_in_func(NULL,
5732 cl->class_obj_methods[i], copyID);
5733
5734 return abort;
5735}
5736
5737/*
5738 * Mark the object "cl" with "copyID".
5739 * Also see set_ref_in_item().
5740 */
5741 static int
5742set_ref_in_item_object(
5743 object_T *obj,
5744 int copyID,
5745 ht_stack_T **ht_stack,
5746 list_stack_T **list_stack)
5747{
5748 int abort = FALSE;
5749
5750 if (obj == NULL || obj->obj_copyID == copyID)
5751 return FALSE;
5752
5753 obj->obj_copyID = copyID;
5754
5755 // The typval_T array is right after the object_T.
5756 typval_T *mtv = (typval_T *)(obj + 1);
5757 for (int i = 0; !abort
5758 && i < obj->obj_class->class_obj_member_count; ++i)
5759 abort = abort || set_ref_in_item(mtv + i, copyID,
5760 ht_stack, list_stack);
5761
5762 return abort;
5763}
5764
5765/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005766 * Mark all lists, dicts and other container types referenced through typval
5767 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005768 * "list_stack" is used to add lists to be marked. Can be NULL.
5769 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5770 *
5771 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005772 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774set_ref_in_item(
5775 typval_T *tv,
5776 int copyID,
5777 ht_stack_T **ht_stack,
5778 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005780 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005781
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005782 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005783 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005784 case VAR_DICT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005785 return set_ref_in_item_dict(tv->vval.v_dict, copyID,
5786 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005787
5788 case VAR_LIST:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005789 return set_ref_in_item_list(tv->vval.v_list, copyID,
5790 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005791
5792 case VAR_FUNC:
5793 {
5794 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5795 break;
5796 }
5797
5798 case VAR_PARTIAL:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005799 return set_ref_in_item_partial(tv->vval.v_partial, copyID,
5800 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005801
5802 case VAR_JOB:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005803#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005804 return set_ref_in_item_job(tv->vval.v_job, copyID,
5805 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005806#else
5807 break;
5808#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005809
5810 case VAR_CHANNEL:
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005811#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005812 return set_ref_in_item_channel(tv->vval.v_channel, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005813 ht_stack, list_stack);
Bram Moolenaarbcbfaf32023-01-11 19:11:15 +00005814#else
5815 break;
5816#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005817
5818 case VAR_CLASS:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005819 return set_ref_in_item_class(tv->vval.v_class, copyID,
5820 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005821
5822 case VAR_OBJECT:
Yegappan Lakshmananea125392023-01-11 11:46:17 +00005823 return set_ref_in_item_object(tv->vval.v_object, copyID,
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005824 ht_stack, list_stack);
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005825
5826 case VAR_UNKNOWN:
5827 case VAR_ANY:
5828 case VAR_VOID:
5829 case VAR_BOOL:
5830 case VAR_SPECIAL:
5831 case VAR_NUMBER:
5832 case VAR_FLOAT:
5833 case VAR_STRING:
5834 case VAR_BLOB:
5835 case VAR_INSTR:
5836 // Types that do not contain any other item
5837 break;
5838 }
5839
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005840 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005841}
5842
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 * Return a string with the string representation of a variable.
5845 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005847 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005848 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005849 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005850 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005851 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5852 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005853 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005855 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005856echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857 typval_T *tv,
5858 char_u **tofree,
5859 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005860 int copyID,
5861 int echo_style,
5862 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005863 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 static int recurse = 0;
5866 char_u *r = NULL;
5867
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005869 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005870 if (!did_echo_string_emsg)
5871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 // Only give this message once for a recursive call to avoid
5873 // flooding the user with errors. And stop iterating over lists
5874 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005875 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005876 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005877 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005878 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005879 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 }
5881 ++recurse;
5882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 switch (tv->v_type)
5884 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005885 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005886 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005887 {
5888 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005889 r = tv->vval.v_string;
5890 if (r == NULL)
5891 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005892 }
5893 else
5894 {
5895 *tofree = string_quote(tv->vval.v_string, FALSE);
5896 r = *tofree;
5897 }
5898 break;
5899
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005901 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005902 char_u buf[MAX_FUNC_NAME_LEN];
5903
5904 if (echo_style)
5905 {
LemonBoya5d35902022-04-29 21:15:02 +01005906 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5907 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005908 buf, MAX_FUNC_NAME_LEN);
5909 if (r == buf)
5910 {
5911 r = vim_strsave(buf);
5912 *tofree = r;
5913 }
5914 else
5915 *tofree = NULL;
5916 }
5917 else
5918 {
5919 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5920 : make_ufunc_name_readable(
5921 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5922 TRUE);
5923 r = *tofree;
5924 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005925 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005926 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005927
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005928 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005929 {
5930 partial_T *pt = tv->vval.v_partial;
5931 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005932 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005933 garray_T ga;
5934 int i;
5935 char_u *tf;
5936
5937 ga_init2(&ga, 1, 100);
5938 ga_concat(&ga, (char_u *)"function(");
5939 if (fname != NULL)
5940 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005941 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005942 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005943 && vim_isupper(fname[1]))
5944 {
5945 ga_concat(&ga, (char_u *)"'g:");
5946 ga_concat(&ga, fname + 1);
5947 }
5948 else
5949 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005950 vim_free(fname);
5951 }
5952 if (pt != NULL && pt->pt_argc > 0)
5953 {
5954 ga_concat(&ga, (char_u *)", [");
5955 for (i = 0; i < pt->pt_argc; ++i)
5956 {
5957 if (i > 0)
5958 ga_concat(&ga, (char_u *)", ");
5959 ga_concat(&ga,
5960 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5961 vim_free(tf);
5962 }
5963 ga_concat(&ga, (char_u *)"]");
5964 }
5965 if (pt != NULL && pt->pt_dict != NULL)
5966 {
5967 typval_T dtv;
5968
5969 ga_concat(&ga, (char_u *)", ");
5970 dtv.v_type = VAR_DICT;
5971 dtv.vval.v_dict = pt->pt_dict;
5972 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5973 vim_free(tf);
5974 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005975 // terminate with ')' and a NUL
5976 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005977
5978 *tofree = ga.ga_data;
5979 r = *tofree;
5980 break;
5981 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005982
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005983 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005984 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005985 break;
5986
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 if (tv->vval.v_list == NULL)
5989 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005990 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005992 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005993 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005994 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5995 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996 {
5997 *tofree = NULL;
5998 r = (char_u *)"[...]";
5999 }
6000 else
6001 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006002 int old_copyID = tv->vval.v_list->lv_copyID;
6003
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006004 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006005 *tofree = list2string(tv, copyID, restore_copyID);
6006 if (restore_copyID)
6007 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006008 r = *tofree;
6009 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006010 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006011
Bram Moolenaar8c711452005-01-14 21:53:12 +00006012 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013 if (tv->vval.v_dict == NULL)
6014 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006015 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006017 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006018 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006019 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6020 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021 {
6022 *tofree = NULL;
6023 r = (char_u *)"{...}";
6024 }
6025 else
6026 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006027 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02006028
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006029 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006030 *tofree = dict2string(tv, copyID, restore_copyID);
6031 if (restore_copyID)
6032 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033 r = *tofree;
6034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006035 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006037 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006038 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006039 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006040 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006041 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006042 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006043 break;
6044
Bram Moolenaar835dc632016-02-07 14:27:38 +01006045 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006046 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006047#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02006049 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
6050 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006051 if (composite_val)
6052 {
6053 *tofree = string_quote(r, FALSE);
6054 r = *tofree;
6055 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02006056#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006058
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006059 case VAR_INSTR:
6060 *tofree = NULL;
6061 r = (char_u *)"instructions";
6062 break;
6063
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006064 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006065 {
6066 class_T *cl = tv->vval.v_class;
6067 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
6068 r = *tofree = alloc(len);
6069 vim_snprintf((char *)r, len, "class %s",
6070 cl == NULL ? "[unknown]" : (char *)cl->class_name);
6071 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006072 break;
6073
6074 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006075 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006076 garray_T ga;
6077 ga_init2(&ga, 1, 50);
6078 ga_concat(&ga, (char_u *)"object of ");
6079 object_T *obj = tv->vval.v_object;
6080 class_T *cl = obj == NULL ? NULL : obj->obj_class;
6081 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
6082 : cl->class_name);
6083 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006084 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006085 ga_concat(&ga, (char_u *)" {");
6086 for (int i = 0; i < cl->class_obj_member_count; ++i)
6087 {
6088 if (i > 0)
6089 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00006090 ocmember_T *m = &cl->class_obj_members[i];
6091 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006092 ga_concat(&ga, (char_u *)": ");
6093 char_u *tf = NULL;
6094 ga_concat(&ga, echo_string_core(
6095 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006096 &tf, numbuf, copyID, echo_style,
6097 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006098 vim_free(tf);
6099 }
6100 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006101 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00006102
Bram Moolenaarf94178db2022-12-14 17:50:00 +00006103 *tofree = r = ga.ga_data;
6104 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006105 break;
6106
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006107 case VAR_FLOAT:
6108 *tofree = NULL;
6109 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6110 r = numbuf;
6111 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006112
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006113 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006114 case VAR_SPECIAL:
6115 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006116 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006117 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006119
Bram Moolenaar8502c702014-06-17 12:51:16 +02006120 if (--recurse == 0)
6121 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006123}
6124
6125/*
6126 * Return a string with the string representation of a variable.
6127 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6128 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006129 * Does not put quotes around strings, as ":echo" displays values.
6130 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6131 * May return NULL.
6132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006133 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006134echo_string(
6135 typval_T *tv,
6136 char_u **tofree,
6137 char_u *numbuf,
6138 int copyID)
6139{
6140 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6141}
6142
6143/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006144 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6145 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006146 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006147 */
6148 int
6149buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6150{
6151 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006152 char_u *t;
6153 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006154
6155 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6156 return -1;
6157
6158 if (lnum > buf->b_ml.ml_line_count)
6159 lnum = buf->b_ml.ml_line_count;
6160
6161 str = ml_get_buf(buf, lnum, FALSE);
6162 if (str == NULL)
6163 return -1;
6164
6165 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006166 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006167
Bram Moolenaar91458462021-01-13 20:08:38 +01006168 // count the number of characters
6169 t = str;
6170 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6171 t += mb_ptr2len(t);
6172
6173 // In insert mode, when the cursor is at the end of a non-empty line,
6174 // byteidx points to the NUL character immediately past the end of the
6175 // string. In this case, add one to the character count.
6176 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6177 count++;
6178
6179 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006180}
6181
6182/*
6183 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006184 * byte index. Works only for loaded buffers. Returns -1 on failure.
6185 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006186 */
6187 int
6188buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6189{
6190 char_u *str;
6191 char_u *t;
6192
6193 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6194 return -1;
6195
6196 if (lnum > buf->b_ml.ml_line_count)
6197 lnum = buf->b_ml.ml_line_count;
6198
6199 str = ml_get_buf(buf, lnum, FALSE);
6200 if (str == NULL)
6201 return -1;
6202
6203 // Convert the character offset to a byte offset
6204 t = str;
6205 while (*t != NUL && --charidx > 0)
6206 t += mb_ptr2len(t);
6207
Bram Moolenaar91458462021-01-13 20:08:38 +01006208 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006209}
6210
6211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006216var2fpos(
6217 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006218 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006219 int *fnum, // set to fnum for '0, 'A, etc.
6220 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006222 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006224 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006226 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006227 if (varp->v_type == VAR_LIST)
6228 {
6229 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006230 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006231 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006232 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006233
6234 l = varp->vval.v_list;
6235 if (l == NULL)
6236 return NULL;
6237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006238 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006239 pos.lnum = list_find_nr(l, 0L, &error);
6240 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006242 if (charcol)
6243 len = (long)mb_charlen(ml_get(pos.lnum));
6244 else
6245 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006246
Bram Moolenaarec65d772020-08-20 22:29:12 +02006247 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006248 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006249 li = list_find(l, 1L);
6250 if (li != NULL && li->li_tv.v_type == VAR_STRING
6251 && li->li_tv.vval.v_string != NULL
6252 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006253 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006254 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006255 }
6256 else
6257 {
6258 pos.col = list_find_nr(l, 1L, &error);
6259 if (error)
6260 return NULL;
6261 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006262
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006264 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006266 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006269 pos.coladd = list_find_nr(l, 2L, &error);
6270 if (error)
6271 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006272
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006273 return &pos;
6274 }
6275
Bram Moolenaarc5809432021-03-27 21:23:30 +01006276 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6277 return NULL;
6278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006279 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006280 if (name == NULL)
6281 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006282
6283 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006284 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006285 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006286 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006287 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006288 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006289 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006290 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006291 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006292 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006293 pos = VIsual;
6294 else
6295 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006296 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006297 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006298 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006300 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006301 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6303 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006304 pos = *pp;
6305 }
6306 if (pos.lnum != 0)
6307 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006308 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006309 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6310 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006312
Bram Moolenaara5525202006-03-02 22:52:09 +00006313 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006314
Bram Moolenaar477933c2007-07-17 14:32:23 +00006315 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006316 {
Bram Moolenaar85090142023-06-01 19:27:08 +01006317 // the "w_valid" flags are not reset when moving the cursor, but they
6318 // do matter for update_topline() and validate_botline().
6319 check_cursor_moved(curwin);
6320
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006321 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006322 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006323 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006324 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006325 // In silent Ex mode topline is zero, but that's not a valid line
6326 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006327 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006328 return &pos;
6329 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006330 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006331 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006332 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006334 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006335 return &pos;
6336 }
6337 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006340 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 pos.lnum = curbuf->b_ml.ml_line_count;
6343 pos.col = 0;
6344 }
6345 else
6346 {
6347 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006348 if (charcol)
6349 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6350 else
6351 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353 return &pos;
6354 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006355 if (in_vim9script())
6356 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 return NULL;
6358}
6359
6360/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006361 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006362 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006363 * Note that the column is passed on as-is, the caller may want to decrement
6364 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006365 * If "charcol" is TRUE use the column as the character index instead of the
6366 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006367 * Return FAIL when conversion is not possible, doesn't check the position for
6368 * validity.
6369 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list2fpos(
6372 typval_T *arg,
6373 pos_T *posp,
6374 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006375 colnr_T *curswantp,
6376 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006377{
6378 list_T *l = arg->vval.v_list;
6379 long i = 0;
6380 long n;
6381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006382 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6383 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006384 if (arg->v_type != VAR_LIST
6385 || l == NULL
6386 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006387 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006388 return FAIL;
6389
6390 if (fnump != NULL)
6391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006392 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006393 if (n < 0)
6394 return FAIL;
6395 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006396 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006397 *fnump = n;
6398 }
6399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006401 if (n < 0)
6402 return FAIL;
6403 posp->lnum = n;
6404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006405 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006406 if (n < 0)
6407 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006408 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006409 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006410 if (charcol)
6411 {
6412 buf_T *buf;
6413
6414 // Get the text for the specified line in a loaded buffer
6415 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6416 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6417 return FAIL;
6418
Bram Moolenaar79f23442022-10-10 12:42:57 +01006419 n = buf_charidx_to_byteidx(buf,
6420 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006421 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006422 posp->col = n;
6423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006424 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006425 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006426 posp->coladd = 0;
6427 else
6428 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006429
Bram Moolenaar493c1782014-05-28 14:34:46 +02006430 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006432
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006433 return OK;
6434}
6435
6436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 * Get the length of an environment variable name.
6438 * Advance "arg" to the first character after the name.
6439 * Return 0 for error.
6440 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 char_u *p;
6445 int len;
6446
6447 for (p = *arg; vim_isIDc(*p); ++p)
6448 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006449 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 return 0;
6451
6452 len = (int)(p - *arg);
6453 *arg = p;
6454 return len;
6455}
6456
6457/*
6458 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006459 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * Return 0 if something is wrong.
6461 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006463get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 char_u *p;
6466 int len;
6467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006470 {
6471 if (*p == ':')
6472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // "s:" is start of "s:var", but "n:" is not and can be used in
6474 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006475 len = (int)(p - *arg);
6476 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6477 || len > 1)
6478 break;
6479 }
6480 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 return 0;
6483
6484 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006485 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 return len;
6488}
6489
6490/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006491 * Get the length of the name of a variable or function.
6492 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006494 * Return -1 if curly braces expansion failed.
6495 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 * If the name contains 'magic' {}'s, expand them and return the
6497 * expanded name in an allocated string via 'alias' - caller must free.
6498 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006500get_name_len(
6501 char_u **arg,
6502 char_u **alias,
6503 int evaluate,
6504 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 char_u *p;
6508 char_u *expr_start;
6509 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006511 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512
6513 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6514 && (*arg)[2] == (int)KE_SNR)
6515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006516 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 *arg += 3;
6518 return get_id_len(arg) + 3;
6519 }
6520 len = eval_fname_script(*arg);
6521 if (len > 0)
6522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006523 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 *arg += len;
6525 }
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006528 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006530 p = find_name_end(*arg, &expr_start, &expr_end,
6531 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (expr_start != NULL)
6533 {
6534 char_u *temp_string;
6535
6536 if (!evaluate)
6537 {
6538 len += (int)(p - *arg);
6539 *arg = skipwhite(p);
6540 return len;
6541 }
6542
6543 /*
6544 * Include any <SID> etc in the expanded string:
6545 * Thus the -len here.
6546 */
6547 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6548 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006549 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 *alias = temp_string;
6551 *arg = skipwhite(p);
6552 return (int)STRLEN(temp_string);
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
6555 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006556 // Only give an error when there is something, otherwise it will be
6557 // reported at a higher level.
6558 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006559 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 return len;
6562}
6563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564/*
6565 * Find the end of a variable or function name, taking care of magic braces.
6566 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6567 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006568 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 * Return a pointer to just after the name. Equal to "arg" if there is no
6570 * valid name.
6571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573find_name_end(
6574 char_u *arg,
6575 char_u **expr_start,
6576 char_u **expr_end,
6577 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006579 int mb_nest = 0;
6580 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006582 int len;
zeertzjqa93d9cd2023-05-02 16:25:47 +01006583 int allow_curly = (flags & FNE_ALLOW_CURLY) || !in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006585 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587 *expr_start = NULL;
6588 *expr_end = NULL;
6589 }
6590
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006591 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006592 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006593 && (*arg != '{' || !allow_curly))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006594 return arg;
6595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 for (p = arg; *p != NUL
6597 && (eval_isnamec(*p)
zeertzjqa93d9cd2023-05-02 16:25:47 +01006598 || (*p == '{' && allow_curly)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006599 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006600 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006601 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006602 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006603 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006604 if (*p == '\'')
6605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006606 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006607 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006608 ;
6609 if (*p == NUL)
6610 break;
6611 }
6612 else if (*p == '"')
6613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006615 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006616 if (*p == '\\' && p[1] != NUL)
6617 ++p;
6618 if (*p == NUL)
6619 break;
6620 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006621 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 // "s:" is start of "s:var", but "n:" is not and can be used in
6624 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006625 len = (int)(p - arg);
6626 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006627 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006628 break;
6629 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006631 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006633 if (*p == '[')
6634 ++br_nest;
6635 else if (*p == ']')
6636 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006638
zeertzjqa93d9cd2023-05-02 16:25:47 +01006639 if (br_nest == 0 && allow_curly)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006641 if (*p == '{')
6642 {
6643 mb_nest++;
6644 if (expr_start != NULL && *expr_start == NULL)
6645 *expr_start = p;
6646 }
6647 else if (*p == '}')
6648 {
6649 mb_nest--;
6650 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6651 *expr_end = p;
6652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655
6656 return p;
6657}
6658
6659/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006660 * Expands out the 'magic' {}'s in a variable/function name.
6661 * Note that this can call itself recursively, to deal with
6662 * constructs like foo{bar}{baz}{bam}
6663 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6664 * "in_start" ^
6665 * "expr_start" ^
6666 * "expr_end" ^
6667 * "in_end" ^
6668 *
6669 * Returns a new allocated string, which the caller must free.
6670 * Returns NULL for failure.
6671 */
6672 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006673make_expanded_name(
6674 char_u *in_start,
6675 char_u *expr_start,
6676 char_u *expr_end,
6677 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006678{
6679 char_u c1;
6680 char_u *retval = NULL;
6681 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006682
6683 if (expr_end == NULL || in_end == NULL)
6684 return NULL;
6685 *expr_start = NUL;
6686 *expr_end = NUL;
6687 c1 = *in_end;
6688 *in_end = NUL;
6689
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006690 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006691 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006692 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006693 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6694 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006695 if (retval != NULL)
6696 {
6697 STRCPY(retval, in_start);
6698 STRCAT(retval, temp_result);
6699 STRCAT(retval, expr_end + 1);
6700 }
6701 }
6702 vim_free(temp_result);
6703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006704 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006705 *expr_start = '{';
6706 *expr_end = '}';
6707
6708 if (retval != NULL)
6709 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006710 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006711 if (expr_start != NULL)
6712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006713 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006714 temp_result = make_expanded_name(retval, expr_start,
6715 expr_end, temp_result);
6716 vim_free(retval);
6717 retval = temp_result;
6718 }
6719 }
6720
6721 return retval;
6722}
6723
6724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006726 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006731 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006732}
6733
6734/*
6735 * Return TRUE if character "c" can be used as the first character in a
6736 * variable or function name (excluding '{' and '}').
6737 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006739eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006740{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006741 return ASCII_ISALPHA(c) || c == '_';
6742}
6743
6744/*
6745 * Return TRUE if character "c" can be used as the first character of a
6746 * dictionary key.
6747 */
6748 int
6749eval_isdictc(int c)
6750{
6751 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752}
6753
6754/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006755 * Handle:
6756 * - expr[expr], expr[expr:expr] subscript
6757 * - ".name" lookup
6758 * - function call with Funcref variable: func(expr)
6759 * - method call: var->method()
6760 *
6761 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006762 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006763 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765handle_subscript(
6766 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006767 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006769 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006770 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006771{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006772 int evaluate = evalarg != NULL
6773 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006774 int ret = OK;
6775 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006776 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006777 int getnext;
6778 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006779
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006780 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006781 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006782 // When at the end of the line and ".name" or "->{" or "->X" follows in
6783 // the next line then consume the line break.
6784 p = eval_next_non_blank(*arg, evalarg, &getnext);
6785 if (getnext
Bram Moolenaarb149d222023-01-24 13:03:37 +00006786 && ((*p == '.'
6787 && ((rettv->v_type == VAR_DICT && eval_isdictc(p[1]))
6788 || rettv->v_type == VAR_CLASS
6789 || rettv->v_type == VAR_OBJECT))
Bram Moolenaara7330422021-06-08 20:46:45 +02006790 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6791 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6792 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006793 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006794 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006795 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006796 check_white = FALSE;
6797 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006798
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006799 if (rettv->v_type == VAR_ANY)
6800 {
6801 char_u *exp_name;
6802 int cc;
6803 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006804 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006805 type_T *type;
6806
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006807 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006808 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006809 if (**arg != '.')
6810 {
6811 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006812 semsg(_(e_expected_dot_after_name_str),
6813 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006814 ret = FAIL;
6815 break;
6816 }
6817 ++*arg;
6818 if (IS_WHITE_OR_NUL(**arg))
6819 {
6820 if (verbose)
6821 emsg(_(e_no_white_space_allowed_after_dot));
6822 ret = FAIL;
6823 break;
6824 }
6825
6826 // isolate the name
6827 exp_name = *arg;
6828 while (eval_isnamec(**arg))
6829 ++*arg;
6830 cc = **arg;
6831 **arg = NUL;
6832
6833 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006834 evalarg == NULL ? NULL : evalarg->eval_cctx,
6835 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006836 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006837
6838 if (idx < 0 && ufunc == NULL)
6839 {
6840 ret = FAIL;
6841 break;
6842 }
6843 if (idx >= 0)
6844 {
6845 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6846 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6847
6848 copy_tv(sv->sv_tv, rettv);
6849 }
6850 else
6851 {
6852 rettv->v_type = VAR_FUNC;
6853 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6854 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006855 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006856 }
6857
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006858 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6859 || rettv->v_type == VAR_PARTIAL))
6860 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006861 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006862 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6863 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006864
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006865 // Stop the expression evaluation when immediately aborting on
6866 // error, or when an interrupt occurred or an exception was thrown
6867 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006868 if (aborting())
6869 {
6870 if (ret == OK)
6871 clear_tv(rettv);
6872 ret = FAIL;
6873 }
6874 dict_unref(selfdict);
6875 selfdict = NULL;
6876 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006877 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006878 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006879 if (in_vim9script())
6880 *arg = skipwhite(p + 2);
6881 else
6882 *arg = p + 2;
zeertzjqc481ad32023-03-11 16:18:51 +00006883 if (VIM_ISWHITE(**arg))
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006884 {
zeertzjqc481ad32023-03-11 16:18:51 +00006885 emsg(_(e_no_white_space_allowed_before_parenthesis));
6886 ret = FAIL;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006887 }
zeertzjqc481ad32023-03-11 16:18:51 +00006888 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
6889 // expr->{lambda}() or expr->(lambda)()
6890 ret = eval_lambda(arg, rettv, evalarg, verbose);
6891 else
6892 // expr->name()
6893 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaarac92e252019-08-03 21:58:38 +02006894 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006895 // "." is ".name" lookup when we found a dict or when evaluating and
6896 // scriptversion is at least 2, where string concatenation is "..".
6897 else if (**arg == '['
6898 || (**arg == '.' && (rettv->v_type == VAR_DICT
6899 || (!evaluate
6900 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006901 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006902 {
6903 dict_unref(selfdict);
6904 if (rettv->v_type == VAR_DICT)
6905 {
6906 selfdict = rettv->vval.v_dict;
6907 if (selfdict != NULL)
6908 ++selfdict->dv_refcount;
6909 }
6910 else
6911 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006912 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006913 {
6914 clear_tv(rettv);
6915 ret = FAIL;
6916 }
6917 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006918 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6919 || rettv->v_type == VAR_OBJECT))
6920 {
6921 // class member: SomeClass.varname
6922 // class method: SomeClass.SomeMethod()
6923 // class constructor: SomeClass.new()
6924 // object member: someObject.varname
6925 // object method: someObject.SomeMethod()
6926 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6927 {
6928 clear_tv(rettv);
6929 ret = FAIL;
6930 }
6931 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006932 else
6933 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006934 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006936 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6937 // Don't do this when "Func" is already a partial that was bound
6938 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006939 if (selfdict != NULL
6940 && (rettv->v_type == VAR_FUNC
6941 || (rettv->v_type == VAR_PARTIAL
6942 && (rettv->vval.v_partial->pt_auto
6943 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006944 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006945
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006946 dict_unref(selfdict);
6947 return ret;
6948}
6949
6950/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 * Make a copy of an item.
6952 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006953 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006954 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6955 * reference to an already copied list/dict can be used.
6956 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006957 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959item_copy(
6960 typval_T *from,
6961 typval_T *to,
6962 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006963 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006964 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965{
6966 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006968
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006971 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006972 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 }
6974 ++recurse;
6975
6976 switch (from->v_type)
6977 {
6978 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006979 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 case VAR_STRING:
6981 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006982 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006983 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006984 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006986 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006987 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006988 case VAR_CLASS:
6989 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006990 copy_tv(from, to);
6991 break;
6992 case VAR_LIST:
6993 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006994 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 if (from->vval.v_list == NULL)
6996 to->vval.v_list = NULL;
6997 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006999 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007000 to->vval.v_list = from->vval.v_list->lv_copylist;
7001 ++to->vval.v_list->lv_refcount;
7002 }
7003 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007004 to->vval.v_list = list_copy(from->vval.v_list,
7005 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007006 if (to->vval.v_list == NULL)
7007 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007008 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007009 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01007011 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007012 case VAR_DICT:
7013 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007014 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015 if (from->vval.v_dict == NULL)
7016 to->vval.v_dict = NULL;
7017 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
7018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007020 to->vval.v_dict = from->vval.v_dict->dv_copydict;
7021 ++to->vval.v_dict->dv_refcount;
7022 }
7023 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00007024 to->vval.v_dict = dict_copy(from->vval.v_dict,
7025 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026 if (to->vval.v_dict == NULL)
7027 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007029 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007030 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01007032 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034 }
7035 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007036 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037}
7038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 void
7040echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
7041{
7042 char_u *tofree;
7043 char_u numbuf[NUMBUFLEN];
7044 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
7045
7046 if (*atstart)
7047 {
7048 *atstart = FALSE;
7049 // Call msg_start() after eval1(), evaluating the expression
7050 // may cause a message to appear.
7051 if (with_space)
7052 {
7053 // Mark the saved text as finishing the line, so that what
7054 // follows is displayed on a new line when scrolling back
7055 // at the more prompt.
7056 msg_sb_eol();
7057 msg_start();
7058 }
7059 }
7060 else if (with_space)
7061 msg_puts_attr(" ", echo_attr);
7062
7063 if (p != NULL)
7064 for ( ; *p != NUL && !got_int; ++p)
7065 {
7066 if (*p == '\n' || *p == '\r' || *p == TAB)
7067 {
7068 if (*p != TAB && *needclr)
7069 {
7070 // remove any text still there from the command
7071 msg_clr_eos();
7072 *needclr = FALSE;
7073 }
7074 msg_putchar_attr(*p, echo_attr);
7075 }
7076 else
7077 {
7078 if (has_mbyte)
7079 {
7080 int i = (*mb_ptr2len)(p);
7081
7082 (void)msg_outtrans_len_attr(p, i, echo_attr);
7083 p += i - 1;
7084 }
7085 else
7086 (void)msg_outtrans_len_attr(p, 1, echo_attr);
7087 }
7088 }
7089 vim_free(tofree);
7090}
7091
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 * ":echo expr1 ..." print each argument separated with a space, add a
7094 * newline at the end.
7095 * ":echon expr1 ..." print each argument plain.
7096 */
7097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02007102 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 int needclr = TRUE;
7104 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01007105 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007106 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007107 evalarg_T evalarg;
7108
Bram Moolenaare707c882020-07-01 13:07:37 +02007109 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 if (eap->skip)
7112 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007113 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007115 // If eval1() causes an error message the text from the command may
7116 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007117 need_clr_eos = needclr;
7118
Bram Moolenaar68db9962021-05-09 23:19:22 +02007119 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007120 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
7122 /*
7123 * Report the invalid expression unless the expression evaluation
7124 * has been cancelled due to an aborting error, an interrupt, or an
7125 * exception.
7126 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007127 if (!aborting() && did_emsg == did_emsg_before
7128 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007129 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007130 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 break;
7132 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007133 need_clr_eos = FALSE;
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007136 {
7137 if (rettv.v_type == VAR_VOID)
7138 {
7139 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7140 break;
7141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007143 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007145 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 arg = skipwhite(arg);
7147 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007148 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007149 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151 if (eap->skip)
7152 --emsg_skip;
7153 else
7154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007155 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 if (needclr)
7157 msg_clr_eos();
7158 if (eap->cmdidx == CMD_echo)
7159 msg_end();
7160 }
7161}
7162
7163/*
7164 * ":echohl {name}".
7165 */
7166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007169 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
7172/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007173 * Returns the :echo attribute
7174 */
7175 int
7176get_echo_attr(void)
7177{
7178 return echo_attr;
7179}
7180
7181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 * ":execute expr1 ..." execute the result of an expression.
7183 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007184 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007186 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 * Each gets spaces around each argument and a newline at the end for
7188 * echo commands
7189 */
7190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007191ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 int ret = OK;
7196 char_u *p;
7197 garray_T ga;
7198 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007199 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
7201 ga_init2(&ga, 1, 80);
7202
7203 if (eap->skip)
7204 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007205 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007207 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007208 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211 if (!eap->skip)
7212 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007213 char_u buf[NUMBUFLEN];
7214
7215 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007216 {
7217 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7218 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007219 semsg(_(e_using_invalid_value_as_string_str),
7220 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007221 p = NULL;
7222 }
7223 else
7224 p = tv_get_string_buf(&rettv, buf);
7225 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007226 else
7227 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007228 if (p == NULL)
7229 {
7230 clear_tv(&rettv);
7231 ret = FAIL;
7232 break;
7233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 len = (int)STRLEN(p);
7235 if (ga_grow(&ga, len + 2) == FAIL)
7236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 ret = FAIL;
7239 break;
7240 }
7241 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 ga.ga_len += len;
7245 }
7246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 arg = skipwhite(arg);
7249 }
7250
7251 if (ret != FAIL && ga.ga_data != NULL)
7252 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007253 // use the first line of continuation lines for messages
7254 SOURCING_LNUM = start_lnum;
7255
Bram Moolenaar37fef162022-08-29 18:16:32 +01007256 if (eap->cmdidx == CMD_echomsg
7257 || eap->cmdidx == CMD_echowindow
7258 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007260 // Mark the already saved text as finishing the line, so that what
7261 // follows is displayed on a new line when scrolling back at the
7262 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007263 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007264 }
7265
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007266 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007267 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007268 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007269 out_flush();
7270 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007271 else if (eap->cmdidx == CMD_echowindow)
7272 {
7273#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007274 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007275#endif
7276 msg_attr(ga.ga_data, echo_attr);
7277#ifdef HAS_MESSAGE_WINDOW
7278 end_echowindow();
7279#endif
7280 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007281 else if (eap->cmdidx == CMD_echoconsole)
7282 {
7283 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7284 ui_write((char_u *)"\r\n", 2, TRUE);
7285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 else if (eap->cmdidx == CMD_echoerr)
7287 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007288 int save_did_emsg = did_emsg;
7289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007290 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007291 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (!force_abort)
7293 did_emsg = save_did_emsg;
7294 }
7295 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007296 {
7297 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7298
7299 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7300 sticky_cmdmod_flags = cmdmod.cmod_flags
7301 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 do_cmdline((char_u *)ga.ga_data,
7303 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007304 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 }
7307
7308 ga_clear(&ga);
7309
7310 if (eap->skip)
7311 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007312 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315/*
7316 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7317 * "arg" points to the "&" or '+' when called, to "option" when returning.
7318 * Returns NULL when no option name found. Otherwise pointer to the char
7319 * after the option name.
7320 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007321 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007322find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
7324 char_u *p = *arg;
7325
7326 ++p;
7327 if (*p == 'g' && p[1] == ':')
7328 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007329 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 p += 2;
7331 }
7332 else if (*p == 'l' && p[1] == ':')
7333 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007334 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 p += 2;
7336 }
7337 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007338 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
7340 if (!ASCII_ISALPHA(*p))
7341 return NULL;
7342 *arg = p;
7343
7344 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007345 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
7347 while (ASCII_ISALPHA(*p))
7348 ++p;
7349 return p;
7350}
7351
7352/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007353 * Display script name where an item was last set.
7354 * Should only be invoked when 'verbose' is non-zero.
7355 */
7356 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007357last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007358{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007359 char_u *p;
7360
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007361 if (script_ctx.sc_sid == 0)
7362 return;
7363
7364 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7365 if (p == NULL)
7366 return;
7367
7368 verbose_enter();
7369 msg_puts(_("\n\tLast set from "));
7370 msg_puts((char *)p);
7371 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007372 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007373 msg_puts(_(line_msg));
7374 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007375 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007376 verbose_leave();
7377 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007378}
7379
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007380#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382/*
7383 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007384 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 * "flags" can be "g" to do a global substitute.
7386 * Returns an allocated string, NULL for error.
7387 */
7388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389do_string_sub(
7390 char_u *str,
7391 char_u *pat,
7392 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007393 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007394 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 int sublen;
7397 regmatch_T regmatch;
7398 int i;
7399 int do_all;
7400 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007401 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 garray_T ga;
7403 char_u *ret;
7404 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007405 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007407 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007409 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411 ga_init2(&ga, 1, 200);
7412
7413 do_all = (flags[0] == 'g');
7414
7415 regmatch.rm_ic = p_ic;
7416 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7417 if (regmatch.regprog != NULL)
7418 {
7419 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007420 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007423 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007424 if (regmatch.startp[0] == regmatch.endp[0])
7425 {
7426 if (zero_width == regmatch.startp[0])
7427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007428 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007429 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007430 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7431 (size_t)i);
7432 ga.ga_len += i;
7433 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007434 continue;
7435 }
7436 zero_width = regmatch.startp[0];
7437 }
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 /*
7440 * Get some space for a temporary buffer to do the substitution
7441 * into. It will contain:
7442 * - The text up to where the match is.
7443 * - The substituted text.
7444 * - The text after the match.
7445 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007446 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007447 if (sublen <= 0)
7448 {
7449 ga_clear(&ga);
7450 break;
7451 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007452 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7454 {
7455 ga_clear(&ga);
7456 break;
7457 }
7458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007459 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 i = (int)(regmatch.startp[0] - tail);
7461 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007462 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007463 (void)vim_regsub(&regmatch, sub, expr,
7464 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7465 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007467 tail = regmatch.endp[0];
7468 if (*tail == NUL)
7469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 if (!do_all)
7471 break;
7472 }
7473
7474 if (ga.ga_data != NULL)
7475 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7476
Bram Moolenaar473de612013-06-08 18:19:48 +02007477 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479
7480 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7481 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007482 if (p_cpo == empty_option)
7483 p_cpo = save_cpo;
7484 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007485 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007486 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007487 // If it's still empty it was changed and restored, need to restore in
7488 // the complicated way.
7489 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007490 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007491 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 return ret;
7495}