blob: 0460dec3cc7407f51cb54b3de6a4f3e8e800be19 [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.
573 * When "convert" is TRUE convert a List into a sequence of lines and convert
574 * a Float to a String.
575 * Returns an allocated string (NULL when out of memory).
576 */
577 char_u *
578typval2string(typval_T *tv, int convert)
579{
580 garray_T ga;
581 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100582 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100583
584 if (convert && tv->v_type == VAR_LIST)
585 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000586 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100587 if (tv->vval.v_list != NULL)
588 {
589 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
590 if (tv->vval.v_list->lv_len > 0)
591 ga_append(&ga, NL);
592 }
593 ga_append(&ga, NUL);
594 retval = (char_u *)ga.ga_data;
595 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100596 else if (convert && tv->v_type == VAR_FLOAT)
597 {
598 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
599 retval = vim_strsave(numbuf);
600 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100601 else
602 retval = vim_strsave(tv_get_string(tv));
603 return retval;
604}
605
606/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200607 * Top level evaluation function, returning a string. Does not handle line
608 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000609 * When "convert" is TRUE convert a List into a sequence of lines and convert
610 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * Return pointer to allocated memory, or NULL for failure.
612 */
613 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100614eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100615 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100616 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100617 exarg_T *eap,
618 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar33570922005-01-25 22:26:29 +0000620 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100622 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100623 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100625 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100626 if (use_simple_function)
627 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
628 else
629 r = eval0(arg, &tv, NULL, &evalarg);
630 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 retval = NULL;
632 else
633 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100634 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000635 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100637 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 return retval;
640}
641
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100642 char_u *
643eval_to_string(
644 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100645 int convert,
646 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100647{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100648 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100649}
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000652 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100653 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200654 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 */
656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657eval_to_string_safe(
658 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000659 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100660 int keep_script_version,
661 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200664 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200665 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200666 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar9530b582022-01-22 13:39:08 +0000668 if (!keep_script_version)
669 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200670 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000671 if (use_sandbox)
672 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100673 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200674 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100675 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000676 if (use_sandbox)
677 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100678 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200679 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200680 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200681 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return retval;
683}
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685/*
686 * Top level evaluation function, returning a number.
687 * Evaluates "expr" silently.
688 * Returns -1 for an error.
689 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200690 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100691eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar33570922005-01-25 22:26:29 +0000693 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200694 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000695 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100696 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 ++emsg_off;
699
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100700 if (use_simple_function)
701 r = may_call_simple_func(expr, &rettv);
702 if (r == NOTDONE)
703 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
704 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 retval = -1;
706 else
707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100708 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 --emsg_off;
712
713 return retval;
714}
715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000717 * Top level evaluation function.
718 * Returns an allocated typval_T with the result.
719 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000720 */
721 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200722eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100724 return eval_expr_ext(arg, eap, FALSE);
725}
726
727 typval_T *
728eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
729{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200731 evalarg_T evalarg;
732
733 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000734
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200735 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100736 if (tv != NULL)
737 {
738 int r = NOTDONE;
739
740 if (use_simple_function)
741 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
742 if (r == NOTDONE)
743 r = eval0(arg, tv, eap, &evalarg);
744
745 if (r == FAIL)
746 VIM_CLEAR(tv);
747 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000748
Bram Moolenaar37c83712020-06-30 21:18:36 +0200749 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000750 return tv;
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000754 * "*arg" points to what can be a function name in the form of "import.Name" or
755 * "Funcref". Return the name of the function. Set "tofree" to something that
756 * was allocated.
757 * If "verbose" is FALSE no errors are given.
758 * Return NULL for any failure.
759 */
760 static char_u *
761deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000762 char_u **arg,
763 char_u **tofree,
764 evalarg_T *evalarg,
765 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000766{
767 typval_T ref;
768 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100769 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000770
771 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100772 if (evalarg != NULL)
773 {
774 // need to evaluate this to get an import, like in "a.Func"
775 save_flags = evalarg->eval_flags;
776 evalarg->eval_flags |= EVAL_EVALUATE;
777 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100778 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100779 {
780 dictitem_T *v;
781
782 // If <SID>VarName was used it would not be found, try another way.
783 v = find_var_also_in_script(name, NULL, FALSE);
784 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100785 {
786 name = NULL;
787 goto theend;
788 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100789 copy_tv(&v->di_tv, &ref);
790 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000791 if (*skipwhite(*arg) != NUL)
792 {
793 if (verbose)
794 semsg(_(e_trailing_characters_str), *arg);
795 name = NULL;
796 }
797 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
798 {
799 name = ref.vval.v_string;
800 ref.vval.v_string = NULL;
801 *tofree = name;
802 }
803 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
804 {
805 if (ref.vval.v_partial->pt_argc > 0
806 || ref.vval.v_partial->pt_dict != NULL)
807 {
808 if (verbose)
809 emsg(_(e_cannot_use_partial_here));
810 name = NULL;
811 }
812 else
813 {
814 name = vim_strsave(partial_name(ref.vval.v_partial));
815 *tofree = name;
816 }
817 }
818 else
819 {
820 if (verbose)
821 semsg(_(e_not_callable_type_str), name);
822 name = NULL;
823 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100824
825theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000826 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100827 if (evalarg != NULL)
828 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000829 return name;
830}
831
832/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100833 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200834 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
835 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839call_vim_function(
840 char_u *func,
841 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200842 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200843 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000845 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200846 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000847 char_u *arg;
848 char_u *name;
849 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000850 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200853 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000854 funcexe.fe_firstline = curwin->w_cursor.lnum;
855 funcexe.fe_lastline = curwin->w_cursor.lnum;
856 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000857
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000858 // The name might be "import.Func" or "Funcref". We don't know, we need to
859 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000860 // autoload script has errors. Guess that when there is a dot in the name
861 // showing errors is the right choice.
862 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000863 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000864 if (ignore_errors)
865 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000866 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000867 if (ignore_errors)
868 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000869 if (name == NULL)
870 name = func;
871
872 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
873
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000874 if (ret == FAIL)
875 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000876 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000877
878 return ret;
879}
880
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100881/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100882 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000883 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
884 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000885 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000886 */
887 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888call_func_retstr(
889 char_u *func,
890 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200891 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000892{
893 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000894 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000895
Bram Moolenaarded27a12018-08-01 19:06:03 +0200896 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000897 return NULL;
898
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100899 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return retval;
902}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000903
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000904/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100905 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000906 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000907 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100908 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000909 */
910 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911call_func_retlist(
912 char_u *func,
913 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200914 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000915{
916 typval_T rettv;
917
Bram Moolenaarded27a12018-08-01 19:06:03 +0200918 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000919 return NULL;
920
921 if (rettv.v_type != VAR_LIST)
922 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100923 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
924 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000925 clear_tv(&rettv);
926 return NULL;
927 }
928
929 return rettv.vval.v_list;
930}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaare70dd112022-01-21 16:31:11 +0000932#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100934 * Evaluate "arg", which is 'foldexpr'.
935 * Note: caller must set "curwin" to match "arg".
936 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
937 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
939 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000940eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000942 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200944 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000946 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000947 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100948 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100950 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000951 current_sctx = wp->w_p_script_ctx[WV_FDE];
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000954 if (use_sandbox)
955 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100956 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100958
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100959 // Evaluate the expression. If the expression is "FuncName()" call the
960 // function directly.
961 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 retval = 0;
963 else
964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100965 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 if (tv.v_type == VAR_NUMBER)
967 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000968 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 retval = 0;
970 else
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // If the result is a string, check if there is a non-digit before
973 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000974 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (!VIM_ISDIGIT(*s) && *s != '-')
976 *cp = *s++;
977 retval = atol((char *)s);
978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000979 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000982 if (use_sandbox)
983 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100984 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200985 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000986 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200988 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 * Get an lval: variable, Dict item or List item that can be assigned a value
994 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
995 * "name.key", "name.key[expr]" etc.
996 * Indexing only works if "name" is an existing List or Dictionary.
997 * "name" points to the start of the name.
998 * If "rettv" is not NULL it points to the value to be assigned.
999 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1000 * wrong; must end in space or cmd separator.
1001 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001002 * flags:
1003 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001004 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001005 * GLV_NO_AUTOLOAD: do not use script autoloading
1006 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001008 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012get_lval(
1013 char_u *name,
1014 typval_T *rettv,
1015 lval_T *lp,
1016 int unlet,
1017 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001018 int flags, // GLV_ values
1019 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 char_u *expr_start, *expr_end;
1023 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001024 dictitem_T *v;
1025 typval_T var1;
1026 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001030 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001031 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001032 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001033 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001035 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001036 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001038 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001040 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001042 lp->ll_name_end = find_name_end(name, NULL, NULL,
1043 FNE_INCL_BR | fne_flags);
1044 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 }
1046
Bram Moolenaara749a422022-02-12 19:52:25 +00001047 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001048 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001049 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1050 {
1051 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1052 return NULL;
1053 }
1054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001055 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001056 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 if (expr_start != NULL)
1059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001061 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 && *p != '[' && *p != '.')
1063 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001064 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 return NULL;
1066 }
1067
1068 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1069 if (lp->ll_exp_name == NULL)
1070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // Report an invalid expression in braces, unless the
1072 // expression evaluation has been cancelled due to an
1073 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 if (!aborting() && !quiet)
1075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001076 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001077 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 return NULL;
1079 }
1080 }
1081 lp->ll_name = lp->ll_exp_name;
1082 }
1083 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 lp->ll_name = name;
1086
Bram Moolenaar4525a572022-02-13 11:57:33 +00001087 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001089 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001090 // However, "g:[key]" is indexing a dictionary.
1091 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001092 {
1093 --p;
1094 lp->ll_name_end = p;
1095 }
1096 if (*p == ':')
1097 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001098 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001099
Bram Moolenaar9510d222022-09-11 15:14:05 +01001100 if (is_scoped_variable(name))
1101 {
1102 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1103 return NULL;
1104 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001105 if (tp == p + 1 && !quiet)
1106 {
1107 semsg(_(e_white_space_required_after_str_str), ":", p);
1108 return NULL;
1109 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001110 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001111 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001112 semsg(_(e_using_type_not_in_script_context_str), p);
1113 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001114 }
1115
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001116 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001117 lp->ll_type = parse_type(&tp,
1118 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1119 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001120 if (lp->ll_type == NULL && !quiet)
1121 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001122 lp->ll_name_end = tp;
1123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 }
1125 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001126 if (lp->ll_name == NULL)
1127 return p;
1128
Bram Moolenaar62aec932022-01-29 21:45:34 +00001129 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001130 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001131 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001132
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001133 if (import != NULL)
1134 {
1135 ufunc_T *ufunc;
1136 type_T *type;
1137
Bram Moolenaar753885b2022-08-24 16:30:36 +01001138 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001139 lp->ll_sid = import->imp_sid;
1140 lp->ll_name = skipwhite(p + 1);
1141 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1142 lp->ll_name_end = p;
1143
1144 // check the item is exported
1145 cc = *p;
1146 *p = NUL;
1147 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001148 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001149 {
1150 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001151 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001152 }
1153 *p = cc;
1154 }
1155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001158 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 return p;
1160
Bram Moolenaar4525a572022-02-13 11:57:33 +00001161 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001162 {
1163 // using local variable
1164 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001165 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001166 }
1167 else
1168 {
1169 cc = *p;
1170 *p = NUL;
1171 // When we would write to the variable pass &ht and prevent autoload.
1172 writing = !(flags & GLV_READ_ONLY);
1173 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001174 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001175 if (v == NULL && !quiet)
1176 semsg(_(e_undefined_variable_str), lp->ll_name);
1177 *p = cc;
1178 if (v == NULL)
1179 return NULL;
1180 lp->ll_tv = &v->di_tv;
1181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182
Bram Moolenaar4525a572022-02-13 11:57:33 +00001183 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001184 {
1185 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001186 semsg(_(e_variable_already_declared_str), lp->ll_name);
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001187 return NULL;
1188 }
1189
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001190 /*
1191 * Loop until no more [idx] or .key is following.
1192 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001193 var1.v_type = VAR_UNKNOWN;
1194 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001195 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001197 vartype_T v_type = lp->ll_tv->v_type;
1198
1199 if (*p == '.' && v_type != VAR_DICT
1200 && v_type != VAR_OBJECT
1201 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001202 {
1203 if (!quiet)
1204 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1205 return NULL;
1206 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001207 if (v_type != VAR_LIST
1208 && v_type != VAR_DICT
1209 && v_type != VAR_BLOB
1210 && v_type != VAR_OBJECT
1211 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001214 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001216 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001217
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001218 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001219 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001220 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001221 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001222 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001223 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001224 r = rettv_blob_alloc(lp->ll_tv);
1225 if (r == FAIL)
1226 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001227
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001231 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001234
Bram Moolenaar4525a572022-02-13 11:57:33 +00001235 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001236 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001237 && lp->ll_tv == &v->di_tv
1238 && ht != NULL && ht == get_script_local_ht())
1239 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001240 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001241
1242 // Vim9 script local variable: get the type
1243 if (sv != NULL)
1244 lp->ll_valtype = sv->sv_type;
1245 }
1246
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 len = -1;
1248 if (*p == '.')
1249 {
1250 key = p + 1;
1251 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1252 ;
1253 if (len == 0)
1254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001256 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
1259 p = key + len;
1260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 else
1262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001263 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001265 if (*p == ':')
1266 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 else
1268 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001269 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001270 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001272 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001274 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001275 clear_tv(&var1);
1276 return NULL;
1277 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001278 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 }
1280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001282 if (*p == ':')
1283 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001284 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001285 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001287 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001288 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 if (rettv != NULL
1292 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001293 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001296 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001298 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001299 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001301 }
1302 p = skipwhite(p + 1);
1303 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001305 else
1306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001308 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001309 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001311 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001314 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001316 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001317 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001318 clear_tv(&var2);
1319 return NULL;
1320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001326
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001330 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001331 clear_tv(&var1);
1332 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 }
1335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001336 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337 ++p;
1338 }
1339
Bram Moolenaard505d172022-12-18 21:42:55 +00001340 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341 {
1342 if (len == -1)
1343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // "[key]": get key from "var1"
1345 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001346 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001350 }
1351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001353
1354 // a NULL dict is equivalent with an empty dict
1355 if (lp->ll_tv->vval.v_dict == NULL)
1356 {
1357 lp->ll_tv->vval.v_dict = dict_alloc();
1358 if (lp->ll_tv->vval.v_dict == NULL)
1359 {
1360 clear_tv(&var1);
1361 return NULL;
1362 }
1363 ++lp->ll_tv->vval.v_dict->dv_refcount;
1364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001366
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001367 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001369 // When assigning to a scope dictionary check that a function and
1370 // variable name is valid (only variable name unless it is l: or
1371 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001372 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001373 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001374 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001375
1376 if (len != -1)
1377 {
1378 prevval = key[len];
1379 key[len] = NUL;
1380 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001381 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001382 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001383 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001384 && (rettv->v_type == VAR_FUNC
1385 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001386 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001387 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001388 if (len != -1)
1389 key[len] = prevval;
1390 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001391 {
1392 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001393 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001394 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001395 }
1396
Bram Moolenaar10c65862020-10-08 21:16:42 +02001397 if (lp->ll_valtype != NULL)
1398 // use the type of the member
1399 lp->ll_valtype = lp->ll_valtype->tt_member;
1400
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001402 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001403 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001404 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001405 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001406 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001407 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001408 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001409 return NULL;
1410 }
1411
Bram Moolenaar31b81602019-02-10 22:14:27 +01001412 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 if (!quiet)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001416 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001417 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 }
1420 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001422 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001424 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001426 p = NULL;
1427 break;
1428 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001429 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001430 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001431 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1432 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001433 {
1434 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001435 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001436 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001437
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001438 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001440 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001441 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001443 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1444
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001445 /*
1446 * Get the number and item for the only or first index of the List.
1447 */
1448 if (empty1)
1449 lp->ll_n1 = 0;
1450 else
1451 // is number or string
1452 lp->ll_n1 = (long)tv_get_number(&var1);
1453 clear_tv(&var1);
1454
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001455 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001457 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 return NULL;
1459 }
1460 if (lp->ll_range && !lp->ll_empty2)
1461 {
1462 lp->ll_n2 = (long)tv_get_number(&var2);
1463 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001464 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1465 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001466 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 }
1468 lp->ll_blob = lp->ll_tv->vval.v_blob;
1469 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001470 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001471 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001472 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 {
1474 /*
1475 * Get the number and item for the only or first index of the List.
1476 */
1477 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001479 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001480 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001481 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001482 clear_tv(&var1);
1483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001486 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1487 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001488 if (lp->ll_li == NULL)
1489 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001490 clear_tv(&var2);
1491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001492 }
1493
Bram Moolenaar10c65862020-10-08 21:16:42 +02001494 if (lp->ll_valtype != NULL)
1495 // use the type of the member
1496 lp->ll_valtype = lp->ll_valtype->tt_member;
1497
Bram Moolenaar8c711452005-01-14 21:53:12 +00001498 /*
1499 * May need to find the item or absolute index for the second
1500 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 * When no index given: "lp->ll_empty2" is TRUE.
1502 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001507 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001508 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001509 if (check_range_index_two(lp->ll_list,
1510 &lp->ll_n1, lp->ll_li,
1511 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001513 }
1514
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001516 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001517 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001518 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001519 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001520 && lp->ll_tv->vval.v_object != NULL)
1521 ? lp->ll_tv->vval.v_object->obj_class
1522 : lp->ll_tv->vval.v_class;
1523 // TODO: what if class is NULL?
1524 if (cl != NULL)
1525 {
1526 lp->ll_valtype = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001527 int count = v_type == VAR_OBJECT ? cl->class_obj_member_count
1528 : cl->class_class_member_count;
1529 ocmember_T *members = v_type == VAR_OBJECT
1530 ? cl->class_obj_members
1531 : cl->class_class_members;
1532 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001533 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001534 ocmember_T *om = members + i;
1535 if (STRNCMP(om->ocm_name, key, p - key) == 0
1536 && om->ocm_name[p - key] == NUL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001537 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001538 switch (om->ocm_access)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001539 {
1540 case ACCESS_PRIVATE:
Bram Moolenaard505d172022-12-18 21:42:55 +00001541 semsg(_(e_cannot_access_private_member_str),
1542 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001543 return NULL;
1544 case ACCESS_READ:
1545 if (!(flags & GLV_READ_ONLY))
1546 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001547 semsg(_(e_member_is_not_writable_str),
1548 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001549 return NULL;
1550 }
1551 break;
1552 case ACCESS_ALL:
1553 break;
1554 }
1555
Bram Moolenaard505d172022-12-18 21:42:55 +00001556 lp->ll_valtype = om->ocm_type;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001557
Bram Moolenaard505d172022-12-18 21:42:55 +00001558 if (v_type == VAR_OBJECT)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001559 lp->ll_tv = ((typval_T *)(
1560 lp->ll_tv->vval.v_object + 1)) + i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001561 else
1562 lp->ll_tv = &cl->class_members_tv[i];
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001563 break;
1564 }
1565 }
1566 if (lp->ll_valtype == NULL)
1567 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001568 if (v_type == VAR_OBJECT)
1569 semsg(_(e_object_member_not_found_str), key);
1570 else
1571 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001572 return NULL;
1573 }
1574 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 }
1577
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001578 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001580 return p;
1581}
1582
1583/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001584 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001585 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001587clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001588{
1589 vim_free(lp->ll_exp_name);
1590 vim_free(lp->ll_newkey);
1591}
1592
1593/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001594 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001595 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1597 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600set_var_lval(
1601 lval_T *lp,
1602 char_u *endp,
1603 typval_T *rettv,
1604 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001605 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1606 char_u *op,
1607 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001608{
1609 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001611
1612 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001614 cc = *endp;
1615 *endp = NUL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001616 if (in_vim9script() && check_reserved_name(lp->ll_name, NULL) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001617 return;
1618
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001619 if (lp->ll_blob != NULL)
1620 {
1621 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001622
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001623 if (op != NULL && *op != '=')
1624 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001625 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001626 return;
1627 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001628 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001629 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001630
1631 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1632 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001633 if (lp->ll_empty2)
1634 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1635
Bram Moolenaar68452172021-04-12 21:21:02 +02001636 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1637 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001638 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001639 }
1640 else
1641 {
1642 val = (int)tv_get_number_chk(rettv, &error);
1643 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001644 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 }
1646 }
1647 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001649 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001651 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1652 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001653 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001654 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001655 *endp = cc;
1656 return;
1657 }
1658
Bram Moolenaarff697e62019-02-12 22:28:33 +01001659 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001660 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001661 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001662 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001663 {
1664 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001665 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1666 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001667 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001668 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001669 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001670 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001673 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001674 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001675 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1676 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001677 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001678 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001679 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001680 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001681 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001682 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001683 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001684 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001685 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001686 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001687 else if (lp->ll_range)
1688 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001689 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1690 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001691 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001692 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001693 return;
1694 }
1695
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001696 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1697 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001698 }
1699 else
1700 {
1701 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001702 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001704 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1705 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001706 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001707 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001708 return;
1709 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001710
1711 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001712 && check_typval_arg_type(lp->ll_valtype, rettv,
1713 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001714 return;
1715
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001716 if (lp->ll_newkey != NULL)
1717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 if (op != NULL && *op != '=')
1719 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001720 semsg(_(e_key_not_present_in_dictionary_str), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 return;
1722 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001723 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1724 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001725 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001727 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001728 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001729 if (di == NULL)
1730 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001731 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1732 {
1733 vim_free(di);
1734 return;
1735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001736 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 else if (op != NULL && *op != '=')
1739 {
1740 tv_op(lp->ll_tv, rettv, op);
1741 return;
1742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001744 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001745
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001746 /*
1747 * Assign the value to the variable or list item.
1748 */
1749 if (copy)
1750 copy_tv(rettv, lp->ll_tv);
1751 else
1752 {
1753 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001754 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001755 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 }
1757 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758}
1759
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001761 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1762 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 * Returns OK or FAIL.
1764 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001767{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001768 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 char_u numbuf[NUMBUFLEN];
1770 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001771 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001773 // Can't do anything with a Funcref or Dict on the right.
1774 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001775 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001776 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1777 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 {
1779 switch (tv1->v_type)
1780 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001781 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001782 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001784 case VAR_DICT:
1785 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001786 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001787 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001788 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001789 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001790 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001791 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001792 case VAR_CLASS:
1793 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 break;
1795
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001796 case VAR_BLOB:
1797 if (*op != '+' || tv2->v_type != VAR_BLOB)
1798 break;
1799 // BLOB += BLOB
1800 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1801 {
1802 blob_T *b1 = tv1->vval.v_blob;
1803 blob_T *b2 = tv2->vval.v_blob;
1804 int i, len = blob_len(b2);
1805 for (i = 0; i < len; i++)
1806 ga_append(&b1->bv_ga, blob_get(b2, i));
1807 }
1808 return OK;
1809
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001810 case VAR_LIST:
1811 if (*op != '+' || tv2->v_type != VAR_LIST)
1812 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001813 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001814 if (tv2->vval.v_list != NULL)
1815 {
1816 if (tv1->vval.v_list == NULL)
1817 {
1818 tv1->vval.v_list = tv2->vval.v_list;
1819 ++tv1->vval.v_list->lv_refcount;
1820 }
1821 else
1822 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1823 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 return OK;
1825
1826 case VAR_NUMBER:
1827 case VAR_STRING:
1828 if (tv2->v_type == VAR_LIST)
1829 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001830 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001832 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001833 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001834 if (tv2->v_type == VAR_FLOAT)
1835 {
1836 float_T f = n;
1837
Bram Moolenaarff697e62019-02-12 22:28:33 +01001838 if (*op == '%')
1839 break;
1840 switch (*op)
1841 {
1842 case '+': f += tv2->vval.v_float; break;
1843 case '-': f -= tv2->vval.v_float; break;
1844 case '*': f *= tv2->vval.v_float; break;
1845 case '/': f /= tv2->vval.v_float; break;
1846 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001847 clear_tv(tv1);
1848 tv1->v_type = VAR_FLOAT;
1849 tv1->vval.v_float = f;
1850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001851 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001852 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001853 switch (*op)
1854 {
1855 case '+': n += tv_get_number(tv2); break;
1856 case '-': n -= tv_get_number(tv2); break;
1857 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001858 case '/': n = num_divide(n, tv_get_number(tv2),
1859 &failed); break;
1860 case '%': n = num_modulus(n, tv_get_number(tv2),
1861 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001862 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001863 clear_tv(tv1);
1864 tv1->v_type = VAR_NUMBER;
1865 tv1->vval.v_number = n;
1866 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 }
1868 else
1869 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001870 if (tv2->v_type == VAR_FLOAT)
1871 break;
1872
Bram Moolenaarff697e62019-02-12 22:28:33 +01001873 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001874 s = tv_get_string(tv1);
1875 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 clear_tv(tv1);
1877 tv1->v_type = VAR_STRING;
1878 tv1->vval.v_string = s;
1879 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001880 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001881
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001882 case VAR_FLOAT:
1883 {
1884 float_T f;
1885
Bram Moolenaarff697e62019-02-12 22:28:33 +01001886 if (*op == '%' || *op == '.'
1887 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001888 && tv2->v_type != VAR_NUMBER
1889 && tv2->v_type != VAR_STRING))
1890 break;
1891 if (tv2->v_type == VAR_FLOAT)
1892 f = tv2->vval.v_float;
1893 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001894 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001895 switch (*op)
1896 {
1897 case '+': tv1->vval.v_float += f; break;
1898 case '-': tv1->vval.v_float -= f; break;
1899 case '*': tv1->vval.v_float *= f; break;
1900 case '/': tv1->vval.v_float /= f; break;
1901 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001902 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001903 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 }
1905 }
1906
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001907 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 return FAIL;
1909}
1910
1911/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Evaluate the expression used in a ":for var in expr" command.
1913 * "arg" points to "var".
1914 * Set "*errp" to TRUE for an error, FALSE otherwise;
1915 * Return a pointer that holds the info. Null when there is an error.
1916 */
1917 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918eval_for_line(
1919 char_u *arg,
1920 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001921 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001922 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923{
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001925 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 typval_T tv;
1928 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001929 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001933 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 if (fi == NULL)
1935 return NULL;
1936
Bram Moolenaar404557e2021-07-05 21:41:48 +02001937 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1938 &fi->fi_semicolon, FALSE);
1939 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 return fi;
1941
Bram Moolenaar404557e2021-07-05 21:41:48 +02001942 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001943 if (expr[0] != 'i' || expr[1] != 'n'
1944 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001946 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1947 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1948 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001949 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return fi;
1951 }
1952
1953 if (skip)
1954 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001955 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1956 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 {
1958 *errp = FALSE;
1959 if (!skip)
1960 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001961 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001962 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001963 l = tv.vval.v_list;
1964 if (l == NULL)
1965 {
1966 // a null list is like an empty list: do nothing
1967 clear_tv(&tv);
1968 }
1969 else
1970 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001972 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001974 // No need to increment the refcount, it's already set for
1975 // the list being used in "tv".
1976 fi->fi_list = l;
1977 list_add_watch(l, &fi->fi_lw);
1978 fi->fi_lw.lw_item = l->lv_first;
1979 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001980 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001981 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001982 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001983 fi->fi_bi = 0;
1984 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001985 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001986 typval_T btv;
1987
1988 // Make a copy, so that the iteration still works when the
1989 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001991 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001992 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001993 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001994 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001995 else if (tv.v_type == VAR_STRING)
1996 {
1997 fi->fi_byte_idx = 0;
1998 fi->fi_string = tv.vval.v_string;
1999 tv.vval.v_string = NULL;
2000 if (fi->fi_string == NULL)
2001 fi->fi_string = vim_strsave((char_u *)"");
2002 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 else
2004 {
Sean Dewar80d73952021-08-04 19:25:54 +02002005 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002006 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 }
2008 }
2009 }
2010 if (skip)
2011 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002012 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013
2014 return fi;
2015}
2016
2017/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002018 * Used when looping over a :for line, skip the "in expr" part.
2019 */
2020 void
2021skip_for_lines(void *fi_void, evalarg_T *evalarg)
2022{
2023 forinfo_T *fi = (forinfo_T *)fi_void;
2024 int i;
2025
2026 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002027 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002028}
2029
2030/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 * Use the first item in a ":for" list. Advance to the next.
2032 * Assign the values to the variable (list). "arg" points to the first one.
2033 * Return TRUE when a valid item was found, FALSE when at end of list or
2034 * something wrong.
2035 */
2036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002037next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002039 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002041 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002042 ? (ASSIGN_FINAL
2043 // first round: error if variable exists
2044 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002045 | ASSIGN_NO_MEMBER_TYPE
2046 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002047 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002048 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002049 int skip_assign = in_vim9script() && arg[0] == '_'
2050 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002052 if (fi->fi_blob != NULL)
2053 {
2054 typval_T tv;
2055
2056 if (fi->fi_bi >= blob_len(fi->fi_blob))
2057 return FALSE;
2058 tv.v_type = VAR_NUMBER;
2059 tv.v_lock = VAR_FIXED;
2060 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2061 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002062 if (skip_assign)
2063 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002064 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002065 fi->fi_varcount, flag, NULL) == OK;
2066 }
2067
2068 if (fi->fi_string != NULL)
2069 {
2070 typval_T tv;
2071 int len;
2072
2073 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2074 if (len == 0)
2075 return FALSE;
2076 tv.v_type = VAR_STRING;
2077 tv.v_lock = VAR_FIXED;
2078 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2079 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002080 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002081 if (skip_assign)
2082 result = TRUE;
2083 else
2084 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002085 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002086 vim_free(tv.vval.v_string);
2087 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002088 }
2089
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 item = fi->fi_lw.lw_item;
2091 if (item == NULL)
2092 result = FALSE;
2093 else
2094 {
2095 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002096 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002097 if (skip_assign)
2098 result = TRUE;
2099 else
2100 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002101 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 }
2103 return result;
2104}
2105
2106/*
2107 * Free the structure used to store info used by ":for".
2108 */
2109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002110free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002111{
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002114 if (fi == NULL)
2115 return;
2116 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002117 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002119 list_unref(fi->fi_list);
2120 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002121 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002122 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002123 else
2124 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 vim_free(fi);
2126}
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002129set_context_for_expression(
2130 expand_T *xp,
2131 char_u *arg,
2132 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002134 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002136 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002138 if (cmdidx == CMD_let || cmdidx == CMD_var
2139 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002140 {
2141 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 {
2147 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002148 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002149 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 break;
2151 }
2152 return;
2153 }
2154 }
2155 else
2156 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2157 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 while ((xp->xp_pattern = vim_strpbrk(arg,
2159 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2160 {
2161 c = *xp->xp_pattern;
2162 if (c == '&')
2163 {
2164 c = xp->xp_pattern[1];
2165 if (c == '&')
2166 {
2167 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002168 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002173 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2174 xp->xp_pattern += 2;
2175
2176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 else if (c == '$')
2179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002180 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 xp->xp_context = EXPAND_ENV_VARS;
2182 }
2183 else if (c == '=')
2184 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002185 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 xp->xp_context = EXPAND_EXPRESSION;
2187 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002188 else if (c == '#'
2189 && xp->xp_context == EXPAND_EXPRESSION)
2190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002191 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002192 break;
2193 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002194 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 && xp->xp_context == EXPAND_FUNCTIONS
2196 && vim_strchr(xp->xp_pattern, '(') == NULL)
2197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002198 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 break;
2200 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002201 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002203 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2206 if (c == '\\' && xp->xp_pattern[1] != NUL)
2207 ++xp->xp_pattern;
2208 xp->xp_context = EXPAND_NOTHING;
2209 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002210 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2214 /* skip */ ;
2215 xp->xp_context = EXPAND_NOTHING;
2216 }
2217 else if (c == '|')
2218 {
2219 if (xp->xp_pattern[1] == '|')
2220 {
2221 ++xp->xp_pattern;
2222 xp->xp_context = EXPAND_EXPRESSION;
2223 }
2224 else
2225 xp->xp_context = EXPAND_COMMANDS;
2226 }
2227 else
2228 xp->xp_context = EXPAND_EXPRESSION;
2229 }
2230 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002231 // Doesn't look like something valid, expand as an expression
2232 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002233 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 arg = xp->xp_pattern;
2235 if (*arg != NUL)
2236 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2237 /* skip */ ;
2238 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002239
2240 // ":exe one two" completes "two"
2241 if ((cmdidx == CMD_execute
2242 || cmdidx == CMD_echo
2243 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002244 || cmdidx == CMD_echomsg
2245 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002246 && xp->xp_context == EXPAND_EXPRESSION)
2247 {
2248 for (;;)
2249 {
2250 char_u *n = skiptowhite(arg);
2251
2252 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2253 break;
2254 arg = skipwhite(n);
2255 }
2256 }
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 xp->xp_pattern = arg;
2259}
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002262 * Return TRUE if "pat" matches "text".
2263 * Does not use 'cpo' and always uses 'magic'.
2264 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002265 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002266pattern_match(char_u *pat, char_u *text, int ic)
2267{
2268 int matches = FALSE;
2269 char_u *save_cpo;
2270 regmatch_T regmatch;
2271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002272 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002273 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002274 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002275 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2276 if (regmatch.regprog != NULL)
2277 {
2278 regmatch.rm_ic = ic;
2279 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2280 vim_regfree(regmatch.regprog);
2281 }
2282 p_cpo = save_cpo;
2283 return matches;
2284}
2285
2286/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002287 * Handle a name followed by "(". Both for just "name(arg)" and for
2288 * "expr->name(arg)".
2289 * Returns OK or FAIL.
2290 */
2291 static int
2292eval_func(
2293 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002294 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002295 char_u *name,
2296 int name_len,
2297 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002298 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002299 typval_T *basetv) // "expr" for "expr->name(arg)"
2300{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002301 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002302 char_u *s = name;
2303 int len = name_len;
2304 partial_T *partial;
2305 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002306 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002307 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002308
2309 if (!evaluate)
2310 check_vars(s, len);
2311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 // If "s" is the name of a variable of type VAR_FUNC
2313 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002314 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002315 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 // Need to make a copy, in case evaluating the arguments makes
2318 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002319 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002320 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002321 ret = FAIL;
2322 else
2323 {
2324 funcexe_T funcexe;
2325
2326 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002327 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002328 funcexe.fe_firstline = curwin->w_cursor.lnum;
2329 funcexe.fe_lastline = curwin->w_cursor.lnum;
2330 funcexe.fe_evaluate = evaluate;
2331 funcexe.fe_partial = partial;
2332 funcexe.fe_basetv = basetv;
2333 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002334 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002335 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002336 }
2337 vim_free(s);
2338
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002339 // If evaluate is FALSE rettv->v_type was not set in
2340 // get_func_tv, but it's needed in handle_subscript() to parse
2341 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002342 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2343 {
2344 rettv->vval.v_string = NULL;
2345 rettv->v_type = VAR_FUNC;
2346 }
2347
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002348 // Stop the expression evaluation when immediately
2349 // aborting on error, or when an interrupt occurred or
2350 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002351 if (evaluate && aborting())
2352 {
2353 if (ret == OK)
2354 clear_tv(rettv);
2355 ret = FAIL;
2356 }
2357 return ret;
2358}
2359
2360/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002361 * After a NL, skip over empty lines and comment-only lines.
2362 */
2363 static char_u *
2364newline_skip_comments(char_u *arg)
2365{
2366 char_u *p = arg + 1;
2367
2368 for (;;)
2369 {
2370 p = skipwhite(p);
2371
2372 if (*p == NUL)
2373 break;
2374 if (vim9_comment_start(p))
2375 {
2376 char_u *nl = vim_strchr(p, NL);
2377
2378 if (nl == NULL)
2379 break;
2380 p = nl;
2381 }
2382 if (*p != NL)
2383 break;
2384 ++p; // skip another NL
2385 }
2386 return p;
2387}
2388
2389/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002390 * Get the next line source line without advancing. But do skip over comment
2391 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002392 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002393 */
2394 static char_u *
2395getline_peek_skip_comments(evalarg_T *evalarg)
2396{
2397 for (;;)
2398 {
2399 char_u *next = getline_peek(evalarg->eval_getline,
2400 evalarg->eval_cookie);
2401 char_u *p;
2402
2403 if (next == NULL)
2404 break;
2405 p = skipwhite(next);
2406 if (*p != NUL && !vim9_comment_start(p))
2407 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002408 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002409 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002410 }
2411 return NULL;
2412}
2413
2414/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002415 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2416 * comment) and there is a next line, return the next line (skipping blanks)
2417 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002418 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2419 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 * "arg" must point somewhere inside a line, not at the start.
2421 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002422 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2424{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002425 char_u *p = skipwhite(arg);
2426
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002427 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002428 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002430 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2431 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002432 && (*p == NUL || *p == NL
2433 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002435 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002436
Bram Moolenaare442d592022-05-05 12:20:28 +01002437 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002438 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002439 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002440 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002441 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002442 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443
Bram Moolenaar9d489562020-07-30 20:08:50 +02002444 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002446 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002447 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 }
2449 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002450 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451}
2452
2453/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002454 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002455 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002456 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002457 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002458eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002459{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002460 garray_T *gap = &evalarg->eval_ga;
2461 char_u *line;
2462
Bram Moolenaar39be4982022-05-06 21:51:50 +01002463 if (arg != NULL)
2464 {
2465 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002466 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002467 // Truncate before a trailing comment, so that concatenating the lines
2468 // won't turn the rest into a comment.
2469 if (*skipwhite(arg) == '#')
2470 *arg = NUL;
2471 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002472
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002473 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002474 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2475 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002476 else
2477 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002478 if (line == NULL)
2479 return NULL;
2480
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002481 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002482 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2483 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002484 char_u *p = skipwhite(line);
2485
2486 // Going to concatenate the lines after parsing. For an empty or
2487 // comment line use an empty string.
2488 if (*p == NUL || vim9_comment_start(p))
2489 {
2490 vim_free(line);
2491 line = vim_strsave((char_u *)"");
2492 }
2493
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002494 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2495 ++gap->ga_len;
2496 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002497 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002498 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002499 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002500 evalarg->eval_tofree = line;
2501 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002502
2503 // Advanced to the next line, "arg" no longer points into the previous
2504 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002505 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002506 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002507}
2508
Bram Moolenaare6b53242020-07-01 17:28:33 +02002509/*
2510 * Call eval_next_non_blank() and get the next line if needed.
2511 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002512 char_u *
2513skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2514{
2515 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002516 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002517
Bram Moolenaar962d7212020-07-04 14:15:00 +02002518 if (evalarg == NULL)
2519 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002520 eval_next_non_blank(p, evalarg, &getnext);
2521 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002522 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002523 return p;
2524}
2525
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002526/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002527 * The "eval" functions have an "evalarg" argument: When NULL or
2528 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2529 * parsed but not executed. The functions may return OK, but the rettv will be
2530 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
2532
2533/*
2534 * Handle zero level expression.
2535 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002537 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002538 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * Return OK or FAIL.
2540 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542eval0(
2543 char_u *arg,
2544 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002545 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002548 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2549}
2550
2551/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002552 * If "arg" is a simple function call without arguments then call it and return
2553 * the result. Otherwise return NOTDONE.
2554 */
2555 int
2556may_call_simple_func(
2557 char_u *arg,
2558 typval_T *rettv)
2559{
2560 char_u *parens = (char_u *)strstr((char *)arg, "()");
2561 int r = NOTDONE;
2562
2563 // If the expression is "FuncName()" then we can skip a lot of overhead.
2564 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2565 {
2566 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2567
2568 if (to_name_end(p, TRUE) == parens)
2569 r = call_simple_func(arg, (int)(parens - arg), rettv);
2570 }
2571 return r;
2572}
2573
2574/*
2575 * Handle zero level expression with optimization for a simple function call.
2576 * Same arguments and return value as eval0().
2577 */
2578 int
2579eval0_simple_funccal(
2580 char_u *arg,
2581 typval_T *rettv,
2582 exarg_T *eap,
2583 evalarg_T *evalarg)
2584{
2585 int r = may_call_simple_func(arg, rettv);
2586
2587 if (r == NOTDONE)
2588 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2589 return r;
2590}
2591
2592/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002593 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2594 * expression and don't check what comes after the expression.
2595 */
2596 int
2597eval0_retarg(
2598 char_u *arg,
2599 typval_T *rettv,
2600 exarg_T *eap,
2601 evalarg_T *evalarg,
2602 char_u **retarg)
2603{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int ret;
2605 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002606 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002607 int did_emsg_before = did_emsg;
2608 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002610 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002611 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002615
Bram Moolenaar79481362022-06-27 20:15:10 +01002616 if (ret != FAIL)
2617 {
2618 expr_end = p;
2619 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002620
Bram Moolenaar79481362022-06-27 20:15:10 +01002621 // In Vim9 script a command block is not split at NL characters for
2622 // commands using an expression argument. Skip over a '#' comment to
2623 // check for a following NL. Require white space before the '#'.
2624 if (in_vim9script() && p > expr_end && retarg == NULL)
2625 while (*p == '#')
2626 {
2627 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002628
Bram Moolenaar79481362022-06-27 20:15:10 +01002629 if (nl == NULL)
2630 break;
2631 p = skipwhite(nl + 1);
2632 if (eap != NULL && *p != NUL)
2633 eap->nextcmd = p;
2634 check_for_end = FALSE;
2635 }
2636
2637 if (check_for_end)
2638 end_error = !ends_excmd2(arg, p);
2639 }
2640
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002641 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 /*
2646 * Report the invalid expression unless the expression evaluation has
2647 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002648 * exception, or we already gave a more specific error.
2649 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002651 if (!aborting()
2652 && did_emsg == did_emsg_before
2653 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002654 && (flags & EVAL_CONSTANT) == 0
2655 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002656 {
2657 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002658 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002659 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002660 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002661 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002662
2663 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002664 // a next command to avoid more errors, unless "|" is following, which
2665 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002666 if (eap != NULL && p != NULL
2667 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002668 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002671
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002672 if (retarg != NULL)
2673 *retarg = p;
2674 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002675 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return ret;
2678}
2679
2680/*
2681 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002682 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002683 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 *
2685 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002686 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002688 * Note: "rettv.v_lock" is not set.
2689 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 * Return OK or FAIL.
2691 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002692 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002695 char_u *p;
2696 int getnext;
2697
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002698 CLEAR_POINTER(rettv);
2699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 /*
2701 * Get the first variable.
2702 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002703 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return FAIL;
2705
Bram Moolenaar793648f2020-06-26 21:28:25 +02002706 p = eval_next_non_blank(*arg, evalarg, &getnext);
2707 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002709 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 int result;
2711 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 evalarg_T *evalarg_used = evalarg;
2713 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002715 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002716 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717
2718 if (evalarg == NULL)
2719 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002720 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723 orig_flags = evalarg_used->eval_flags;
2724 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002727 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002728 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002729 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002730 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002731 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002732 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002733 clear_tv(rettv);
2734 return FAIL;
2735 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002736 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002737 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 int error = FALSE;
2743
Bram Moolenaar13106602020-10-04 16:06:05 +02002744 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002745 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002746 else if (vim9script)
2747 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002748 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002750 if (error || !op_falsy || !result)
2751 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002752 if (error)
2753 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755
2756 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002757 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002759 if (op_falsy)
2760 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002761 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002762 {
mityu4ac198c2021-05-28 17:52:40 +02002763 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002764 clear_tv(rettv);
2765 return FAIL;
2766 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002768 evalarg_used->eval_flags = (op_falsy ? !result : result)
2769 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2770 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002771 {
2772 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002774 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002775 if (!op_falsy || !result)
2776 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002778 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002780 /*
2781 * Check for the ":".
2782 */
2783 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2784 if (*p != ':')
2785 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002786 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002787 if (evaluate && result)
2788 clear_tv(rettv);
2789 evalarg_used->eval_flags = orig_flags;
2790 return FAIL;
2791 }
2792 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002793 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002794 else
2795 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002796 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002797 {
2798 error_white_both(p, 1);
2799 clear_tv(rettv);
2800 evalarg_used->eval_flags = orig_flags;
2801 return FAIL;
2802 }
2803 *arg = p;
2804 }
2805
2806 /*
2807 * Get the third variable. Recursive!
2808 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002809 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002810 {
mityu4ac198c2021-05-28 17:52:40 +02002811 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002812 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002813 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002814 return FAIL;
2815 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002816 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2817 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002819 if (eval1(arg, &var2, evalarg_used) == FAIL)
2820 {
2821 if (evaluate && result)
2822 clear_tv(rettv);
2823 evalarg_used->eval_flags = orig_flags;
2824 return FAIL;
2825 }
2826 if (evaluate && !result)
2827 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002829
2830 if (evalarg == NULL)
2831 clear_evalarg(&local_evalarg, NULL);
2832 else
2833 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835
2836 return OK;
2837}
2838
2839/*
2840 * Handle first level expression:
2841 * expr2 || expr2 || expr2 logical OR
2842 *
2843 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002844 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 *
2846 * Return OK or FAIL.
2847 */
2848 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002849eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002851 char_u *p;
2852 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002855 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return FAIL;
2859
2860 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002861 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002863 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002864 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002866 evalarg_T *evalarg_used = evalarg;
2867 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002868 int evaluate;
2869 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002870 long result = FALSE;
2871 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002872 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002873 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002874
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002875 if (evalarg == NULL)
2876 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002877 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002878 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002879 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002880 orig_flags = evalarg_used->eval_flags;
2881 evaluate = orig_flags & EVAL_EVALUATE;
2882 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002884 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002885 result = tv_get_bool_chk(rettv, &error);
2886 else if (tv_get_number_chk(rettv, &error) != 0)
2887 result = TRUE;
2888 clear_tv(rettv);
2889 if (error)
2890 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892
2893 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002894 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002896 while (p[0] == '|' && p[1] == '|')
2897 {
2898 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002899 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002900 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002901 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002902 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002903 {
2904 error_white_both(p, 2);
2905 clear_tv(rettv);
2906 return FAIL;
2907 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002908 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002909 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002910
2911 /*
2912 * Get the second variable.
2913 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002914 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002915 {
mityu4ac198c2021-05-28 17:52:40 +02002916 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002917 clear_tv(rettv);
2918 return FAIL;
2919 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002920 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2921 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002923 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002924 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002925
2926 /*
2927 * Compute the result.
2928 */
2929 if (evaluate && !result)
2930 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002931 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002932 result = tv_get_bool_chk(&var2, &error);
2933 else if (tv_get_number_chk(&var2, &error) != 0)
2934 result = TRUE;
2935 clear_tv(&var2);
2936 if (error)
2937 return FAIL;
2938 }
2939 if (evaluate)
2940 {
2941 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002942 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002943 rettv->v_type = VAR_BOOL;
2944 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002945 }
2946 else
2947 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002948 rettv->v_type = VAR_NUMBER;
2949 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002950 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002951 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002952
2953 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002955
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002956 if (evalarg == NULL)
2957 clear_evalarg(&local_evalarg, NULL);
2958 else
2959 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961
2962 return OK;
2963}
2964
2965/*
2966 * Handle second level expression:
2967 * expr3 && expr3 && expr3 logical AND
2968 *
2969 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002970 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 *
2972 * Return OK or FAIL.
2973 */
2974 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002975eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002977 char_u *p;
2978 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002981 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002983 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return FAIL;
2985
2986 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002987 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002989 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002990 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002992 evalarg_T *evalarg_used = evalarg;
2993 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 int orig_flags;
2995 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002996 long result = TRUE;
2997 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002998 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002999 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003000
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003001 if (evalarg == NULL)
3002 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003003 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003004 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003006 orig_flags = evalarg_used->eval_flags;
3007 evaluate = orig_flags & EVAL_EVALUATE;
3008 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003009 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003010 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003011 result = tv_get_bool_chk(rettv, &error);
3012 else if (tv_get_number_chk(rettv, &error) == 0)
3013 result = FALSE;
3014 clear_tv(rettv);
3015 if (error)
3016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018
3019 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003020 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003022 while (p[0] == '&' && p[1] == '&')
3023 {
3024 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003025 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003026 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003027 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003028 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003029 {
3030 error_white_both(p, 2);
3031 clear_tv(rettv);
3032 return FAIL;
3033 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003034 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003035 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003036
3037 /*
3038 * Get the second variable.
3039 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003040 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003041 {
mityu4ac198c2021-05-28 17:52:40 +02003042 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003043 clear_tv(rettv);
3044 return FAIL;
3045 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003046 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3047 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003048 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003049 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003050 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003052
3053 /*
3054 * Compute the result.
3055 */
3056 if (evaluate && result)
3057 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003058 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003059 result = tv_get_bool_chk(&var2, &error);
3060 else if (tv_get_number_chk(&var2, &error) == 0)
3061 result = FALSE;
3062 clear_tv(&var2);
3063 if (error)
3064 return FAIL;
3065 }
3066 if (evaluate)
3067 {
3068 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003069 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003070 rettv->v_type = VAR_BOOL;
3071 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003072 }
3073 else
3074 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003075 rettv->v_type = VAR_NUMBER;
3076 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003077 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003078 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003079
3080 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003082
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003083 if (evalarg == NULL)
3084 clear_evalarg(&local_evalarg, NULL);
3085 else
3086 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088
3089 return OK;
3090}
3091
3092/*
3093 * Handle third level expression:
3094 * var1 == var2
3095 * var1 =~ var2
3096 * var1 != var2
3097 * var1 !~ var2
3098 * var1 > var2
3099 * var1 >= var2
3100 * var1 < var2
3101 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003102 * var1 is var2
3103 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 *
3105 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003106 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 *
3108 * Return OK or FAIL.
3109 */
3110 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003111eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003114 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003115 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003117 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003120 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003122 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 return FAIL;
3124
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003125 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003126
Bram Moolenaar696ba232020-07-29 21:20:41 +02003127 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003132 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003134 typval_T var2;
3135 int ic;
3136 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003137 int evaluate = evalarg == NULL
3138 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003139 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003140
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003141 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003142 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003143 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003144 p = *arg;
3145 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003146 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3147 {
mityu4ac198c2021-05-28 17:52:40 +02003148 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003149 clear_tv(rettv);
3150 return FAIL;
3151 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003152
Bram Moolenaar696ba232020-07-29 21:20:41 +02003153 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3154 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003155 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003156 clear_tv(rettv);
3157 return FAIL;
3158 }
3159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003160 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (p[len] == '?')
3162 {
3163 ic = TRUE;
3164 ++len;
3165 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003166 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 else if (p[len] == '#')
3168 {
3169 ic = FALSE;
3170 ++len;
3171 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003172 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003174 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /*
3177 * Get the second variable.
3178 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003179 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3180 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003181 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003182 clear_tv(rettv);
3183 return FAIL;
3184 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003185 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003186 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 return FAIL;
3190 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003191 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003192 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003193 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003194
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003195 // use the line of the comparison for messages
3196 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003197 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003198 {
3199 ret = FAIL;
3200 clear_tv(rettv);
3201 }
3202 else
3203 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003204 clear_tv(&var2);
3205 return ret;
3206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208
3209 return OK;
3210}
3211
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003212/*
3213 * Make a copy of blob "tv1" and append blob "tv2".
3214 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 void
3216eval_addblob(typval_T *tv1, typval_T *tv2)
3217{
3218 blob_T *b1 = tv1->vval.v_blob;
3219 blob_T *b2 = tv2->vval.v_blob;
3220 blob_T *b = blob_alloc();
3221 int i;
3222
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003223 if (b == NULL)
3224 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00003226 for (i = 0; i < blob_len(b1); i++)
3227 ga_append(&b->bv_ga, blob_get(b1, i));
3228 for (i = 0; i < blob_len(b2); i++)
3229 ga_append(&b->bv_ga, blob_get(b2, i));
3230
3231 clear_tv(tv1);
3232 rettv_blob_set(tv1, b);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233}
3234
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003235/*
3236 * Make a copy of list "tv1" and append list "tv2".
3237 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 int
3239eval_addlist(typval_T *tv1, typval_T *tv2)
3240{
3241 typval_T var3;
3242
3243 // concatenate Lists
3244 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3245 {
3246 clear_tv(tv1);
3247 clear_tv(tv2);
3248 return FAIL;
3249 }
3250 clear_tv(tv1);
3251 *tv1 = var3;
3252 return OK;
3253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003256 * Handle the bitwise left/right shift operator expression:
3257 * var1 << var2
3258 * var1 >> var2
3259 *
3260 * "arg" must point to the first non-white of the expression.
3261 * "arg" is advanced to just after the recognized expression.
3262 *
3263 * Return OK or FAIL.
3264 */
3265 static int
3266eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3267{
3268 /*
3269 * Get the first expression.
3270 */
3271 if (eval6(arg, rettv, evalarg) == FAIL)
3272 return FAIL;
3273
3274 /*
3275 * Repeat computing, until no '<<' or '>>' is following.
3276 */
3277 for (;;)
3278 {
3279 char_u *p;
3280 int getnext;
3281 exprtype_T type;
3282 int evaluate;
3283 typval_T var2;
3284 int vim9script;
3285
3286 p = eval_next_non_blank(*arg, evalarg, &getnext);
3287 if (p[0] == '<' && p[1] == '<')
3288 type = EXPR_LSHIFT;
3289 else if (p[0] == '>' && p[1] == '>')
3290 type = EXPR_RSHIFT;
3291 else
3292 return OK;
3293
3294 // Handle a bitwise left or right shift operator
3295 if (rettv->v_type != VAR_NUMBER)
3296 {
3297 // left operand should be a number
3298 emsg(_(e_bitshift_ops_must_be_number));
3299 clear_tv(rettv);
3300 return FAIL;
3301 }
3302
3303 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3304 vim9script = in_vim9script();
3305 if (getnext)
3306 {
3307 *arg = eval_next_line(*arg, evalarg);
3308 p = *arg;
3309 }
3310 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3311 {
3312 error_white_both(*arg, 2);
3313 clear_tv(rettv);
3314 return FAIL;
3315 }
3316
3317 /*
3318 * Get the second variable.
3319 */
3320 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3321 {
3322 error_white_both(p, 2);
3323 clear_tv(rettv);
3324 return FAIL;
3325 }
3326 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3327 if (eval6(arg, &var2, evalarg) == FAIL)
3328 {
3329 clear_tv(rettv);
3330 return FAIL;
3331 }
3332
3333 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3334 {
3335 // right operand should be a positive number
3336 if (var2.v_type != VAR_NUMBER)
3337 emsg(_(e_bitshift_ops_must_be_number));
3338 else
dundargocc57b5bc2022-11-02 13:30:51 +00003339 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003340 clear_tv(rettv);
3341 clear_tv(&var2);
3342 return FAIL;
3343 }
3344
3345 if (evaluate)
3346 {
3347 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3348 // shifting more bits than we have always results in zero
3349 rettv->vval.v_number = 0;
3350 else if (type == EXPR_LSHIFT)
3351 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003352 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003353 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003354 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003355 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003356 }
3357
3358 clear_tv(&var2);
3359 }
3360
3361 return OK;
3362}
3363
3364/*
3365 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003366 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003368 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003369 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 *
3371 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003372 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 *
3374 * Return OK or FAIL.
3375 */
3376 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003377eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003380 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003382 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 return FAIL;
3384
3385 /*
3386 * Repeat computing, until no '+', '-' or '.' is following.
3387 */
3388 for (;;)
3389 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003390 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003391 int getnext;
3392 char_u *p;
3393 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003394 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003395 int concat;
3396 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003397 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003398
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003399 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003400 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003401 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003402 p = eval_next_non_blank(*arg, evalarg, &getnext);
3403 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003404 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003405 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3406 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003408 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3409 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003410
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003411 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003412 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003413 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003414 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003415 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003416 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003417 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003418 {
mityu4ac198c2021-05-28 17:52:40 +02003419 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003420 clear_tv(rettv);
3421 return FAIL;
3422 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003423 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003424 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003425 if ((op != '+' || (rettv->v_type != VAR_LIST
3426 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003427 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003428 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003430 int error = FALSE;
3431
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003432 // For "list + ...", an illegal use of the first operand as
3433 // a number cannot be determined before evaluating the 2nd
3434 // operand: if this is also a list, all is ok.
3435 // For "something . ...", "something - ..." or "non-list + ...",
3436 // we know that the first operand needs to be a string or number
3437 // without evaluating the 2nd operand. So check before to avoid
3438 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003439 if (op != '.')
3440 tv_get_number_chk(rettv, &error);
3441 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003442 {
3443 clear_tv(rettv);
3444 return FAIL;
3445 }
3446 }
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /*
3449 * Get the second variable.
3450 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003451 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003452 {
mityu89dcb4d2021-05-28 13:50:17 +02003453 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003454 clear_tv(rettv);
3455 return FAIL;
3456 }
3457 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003458 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003460 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return FAIL;
3462 }
3463
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003464 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 /*
3467 * Compute the result.
3468 */
3469 if (op == '.')
3470 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003471 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3472 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003473 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003474
Bram Moolenaar2e086612020-08-25 22:37:48 +02003475 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003476 || var2.v_type == VAR_CHANNEL
3477 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003478 semsg(_(e_using_invalid_value_as_string_str),
3479 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003480 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003481 {
3482 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3483 var2.vval.v_float);
3484 s2 = buf2;
3485 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003486 else
3487 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003488 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003489 {
3490 clear_tv(rettv);
3491 clear_tv(&var2);
3492 return FAIL;
3493 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003494 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 clear_tv(rettv);
3496 rettv->v_type = VAR_STRING;
3497 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003499 else if (op == '+' && rettv->v_type == VAR_BLOB
3500 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003502 else if (op == '+' && rettv->v_type == VAR_LIST
3503 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003504 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003506 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 else
3509 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003510 int error = FALSE;
3511 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003512 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003514 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003515 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003516 f1 = rettv->vval.v_float;
3517 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003519 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003521 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522 if (error)
3523 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003524 // This can only happen for "list + non-list" or
3525 // "blob + non-blob". For "non-list + ..." or
3526 // "something - ...", we returned before evaluating the
3527 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003528 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003529 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003530 return FAIL;
3531 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003532 if (var2.v_type == VAR_FLOAT)
3533 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535 if (var2.v_type == VAR_FLOAT)
3536 {
3537 f2 = var2.vval.v_float;
3538 n2 = 0;
3539 }
3540 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003542 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 if (error)
3544 {
3545 clear_tv(rettv);
3546 clear_tv(&var2);
3547 return FAIL;
3548 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003549 if (rettv->v_type == VAR_FLOAT)
3550 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003552 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003554 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3556 {
3557 if (op == '+')
3558 f1 = f1 + f2;
3559 else
3560 f1 = f1 - f2;
3561 rettv->v_type = VAR_FLOAT;
3562 rettv->vval.v_float = f1;
3563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003565 {
3566 if (op == '+')
3567 n1 = n1 + n2;
3568 else
3569 n1 = n1 - n2;
3570 rettv->v_type = VAR_NUMBER;
3571 rettv->vval.v_number = n1;
3572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 }
3577 return OK;
3578}
3579
3580/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003581 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 * * number multiplication
3583 * / number division
3584 * % number modulo
3585 *
3586 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003587 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 *
3589 * Return OK or FAIL.
3590 */
3591 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003592eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593 char_u **arg,
3594 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003595 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003596 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003598 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003601 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003603 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 return FAIL;
3605
3606 /*
3607 * Repeat computing, until no '*', '/' or '%' is following.
3608 */
3609 for (;;)
3610 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003611 int evaluate;
3612 int getnext;
3613 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003614 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003615 int op;
3616 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003617 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003618 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003619
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003620 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003621 p = eval_next_non_blank(*arg, evalarg, &getnext);
3622 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003623 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003625
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003626 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003627 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003628 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003629 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003630 {
3631 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3632 {
mityu4ac198c2021-05-28 17:52:40 +02003633 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003634 clear_tv(rettv);
3635 return FAIL;
3636 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003637 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 f1 = 0;
3641 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003642 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003643 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003645 if (rettv->v_type == VAR_FLOAT)
3646 {
3647 f1 = rettv->vval.v_float;
3648 use_float = TRUE;
3649 n1 = 0;
3650 }
3651 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003652 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003654 if (error)
3655 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 else
3658 n1 = 0;
3659
3660 /*
3661 * Get the second variable.
3662 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003663 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3664 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003665 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003666 clear_tv(rettv);
3667 return FAIL;
3668 }
3669 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003670 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003673 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003675 if (var2.v_type == VAR_FLOAT)
3676 {
3677 if (!use_float)
3678 {
3679 f1 = n1;
3680 use_float = TRUE;
3681 }
3682 f2 = var2.vval.v_float;
3683 n2 = 0;
3684 }
3685 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003686 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003687 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003688 clear_tv(&var2);
3689 if (error)
3690 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003691 if (use_float)
3692 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695 /*
3696 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003697 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003699 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003701 if (op == '*')
3702 f1 = f1 * f2;
3703 else if (op == '/')
3704 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003705#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003706 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003707 if (f2 == 0.0)
3708 {
3709 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003710 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003711 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003712 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003713 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003714 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003715 }
3716 else
3717 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003718#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // We rely on the floating point library to handle divide
3720 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003721 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003722#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003725 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003726 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003727 return FAIL;
3728 }
3729 rettv->v_type = VAR_FLOAT;
3730 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
3733 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003734 int failed = FALSE;
3735
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003736 if (op == '*')
3737 n1 = n1 * n2;
3738 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003739 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003741 n1 = num_modulus(n1, n2, &failed);
3742 if (failed)
3743 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003745 rettv->v_type = VAR_NUMBER;
3746 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 }
3750
3751 return OK;
3752}
3753
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003754/*
3755 * Handle a type cast before a base level expression.
3756 * "arg" must point to the first non-white of the expression.
3757 * "arg" is advanced to just after the recognized expression.
3758 * Return OK or FAIL.
3759 */
3760 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003761eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003762 char_u **arg,
3763 typval_T *rettv,
3764 evalarg_T *evalarg,
3765 int want_string) // after "." operator
3766{
3767 type_T *want_type = NULL;
3768 garray_T type_list; // list of pointers to allocated types
3769 int res;
3770 int evaluate = evalarg == NULL ? 0
3771 : (evalarg->eval_flags & EVAL_EVALUATE);
3772
3773 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003774 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3775 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003776 {
3777 ++*arg;
3778 ga_init2(&type_list, sizeof(type_T *), 10);
3779 want_type = parse_type(arg, &type_list, TRUE);
3780 if (want_type == NULL && (evaluate || **arg != '>'))
3781 {
3782 clear_type_list(&type_list);
3783 return FAIL;
3784 }
3785
3786 if (**arg != '>')
3787 {
3788 if (*skipwhite(*arg) == '>')
3789 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3790 else
3791 emsg(_(e_missing_gt));
3792 clear_type_list(&type_list);
3793 return FAIL;
3794 }
3795 ++*arg;
3796 *arg = skipwhite_and_linebreak(*arg, evalarg);
3797 }
3798
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003799 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003800
3801 if (want_type != NULL && evaluate)
3802 {
3803 if (res == OK)
3804 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003805 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3806 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003807
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003808 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003809 {
3810 if (want_type == &t_bool && actual != &t_bool
3811 && (actual->tt_flags & TTFLAG_BOOL_OK))
3812 {
3813 int n = tv2bool(rettv);
3814
3815 // can use "0" and "1" for boolean in some places
3816 clear_tv(rettv);
3817 rettv->v_type = VAR_BOOL;
3818 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3819 }
3820 else
3821 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003822 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003823
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003824 where.wt_variable = TRUE;
3825 res = check_type(want_type, actual, TRUE, where);
3826 }
3827 }
3828 }
3829 clear_type_list(&type_list);
3830 }
3831
3832 return res;
3833}
3834
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003835 int
3836eval_leader(char_u **arg, int vim9)
3837{
3838 char_u *s = *arg;
3839 char_u *p = *arg;
3840
3841 while (*p == '!' || *p == '-' || *p == '+')
3842 {
3843 char_u *n = skipwhite(p + 1);
3844
3845 // ++, --, -+ and +- are not accepted in Vim9 script
3846 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3847 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003848 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003849 return FAIL;
3850 }
3851 p = n;
3852 }
3853 *arg = p;
3854 return OK;
3855}
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003858 * Check for a predefined value "true", "false" and "null.*".
3859 * Return OK when recognized.
3860 */
3861 int
3862handle_predefined(char_u *s, int len, typval_T *rettv)
3863{
3864 switch (len)
3865 {
3866 case 4: if (STRNCMP(s, "true", 4) == 0)
3867 {
3868 rettv->v_type = VAR_BOOL;
3869 rettv->vval.v_number = VVAL_TRUE;
3870 return OK;
3871 }
3872 if (STRNCMP(s, "null", 4) == 0)
3873 {
3874 rettv->v_type = VAR_SPECIAL;
3875 rettv->vval.v_number = VVAL_NULL;
3876 return OK;
3877 }
3878 break;
3879 case 5: if (STRNCMP(s, "false", 5) == 0)
3880 {
3881 rettv->v_type = VAR_BOOL;
3882 rettv->vval.v_number = VVAL_FALSE;
3883 return OK;
3884 }
3885 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003886 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3887 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003888#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003889 rettv->v_type = VAR_JOB;
3890 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003891#else
3892 rettv->v_type = VAR_SPECIAL;
3893 rettv->vval.v_number = VVAL_NULL;
3894#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003895 return OK;
3896 }
3897 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003898 case 9:
3899 if (STRNCMP(s, "null_", 5) != 0)
3900 break;
3901 if (STRNCMP(s + 5, "list", 4) == 0)
3902 {
3903 rettv->v_type = VAR_LIST;
3904 rettv->vval.v_list = NULL;
3905 return OK;
3906 }
3907 if (STRNCMP(s + 5, "dict", 4) == 0)
3908 {
3909 rettv->v_type = VAR_DICT;
3910 rettv->vval.v_dict = NULL;
3911 return OK;
3912 }
3913 if (STRNCMP(s + 5, "blob", 4) == 0)
3914 {
3915 rettv->v_type = VAR_BLOB;
3916 rettv->vval.v_blob = NULL;
3917 return OK;
3918 }
3919 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003920 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3921 {
3922 rettv->v_type = VAR_CLASS;
3923 rettv->vval.v_class = NULL;
3924 return OK;
3925 }
3926 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003927 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3928 {
3929 rettv->v_type = VAR_STRING;
3930 rettv->vval.v_string = NULL;
3931 return OK;
3932 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003933 if (STRNCMP(s, "null_object", 11) == 0)
3934 {
3935 rettv->v_type = VAR_OBJECT;
3936 rettv->vval.v_object = NULL;
3937 return OK;
3938 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003939 break;
3940 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003941 if (STRNCMP(s, "null_channel", 12) == 0)
3942 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003944 rettv->v_type = VAR_CHANNEL;
3945 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003946#else
3947 rettv->v_type = VAR_SPECIAL;
3948 rettv->vval.v_number = VVAL_NULL;
3949#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003950 return OK;
3951 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003952 if (STRNCMP(s, "null_partial", 12) == 0)
3953 {
3954 rettv->v_type = VAR_PARTIAL;
3955 rettv->vval.v_partial = NULL;
3956 return OK;
3957 }
3958 break;
3959 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3960 {
3961 rettv->v_type = VAR_FUNC;
3962 rettv->vval.v_string = NULL;
3963 return OK;
3964 }
3965 break;
3966 }
3967 return FAIL;
3968}
3969
3970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * Handle sixth level expression:
3972 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003973 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003974 * "string" string constant
3975 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 * &option-name option value
3977 * @r register contents
3978 * identifier variable value
3979 * function() function call
3980 * $VAR environment variable
3981 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003982 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003984 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003985 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 *
3987 * Also handle:
3988 * ! in front logical NOT
3989 * - in front unary minus
3990 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003991 * trailing [] subscript in String or List
3992 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003993 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 *
3995 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003996 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *
3998 * Return OK or FAIL.
3999 */
4000 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004001eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002 char_u **arg,
4003 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004004 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004007 int evaluate = evalarg != NULL
4008 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 int len;
4010 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004011 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *start_leader, *end_leader;
4013 int ret = OK;
4014 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004015 static int recurse = 0;
4016 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004020 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004025 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 */
4027 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004028 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 end_leader = *arg;
4031
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004032 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004033 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004034 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004035 ++*arg;
4036 return FAIL;
4037 }
4038
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004039 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004040 // and crash. With MSVC the stack is smaller.
4041 if (recurse ==
4042#ifdef _MSC_VER
4043 300
4044#else
4045 1000
4046#endif
4047 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004048 {
4049 semsg(_(e_expression_too_recursive_str), *arg);
4050 return FAIL;
4051 }
4052 ++recurse;
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 switch (**arg)
4055 {
4056 /*
4057 * Number constant.
4058 */
4059 case '0':
4060 case '1':
4061 case '2':
4062 case '3':
4063 case '4':
4064 case '5':
4065 case '6':
4066 case '7':
4067 case '8':
4068 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004069 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004070
4071 // Apply prefixed "-" and "+" now. Matters especially when
4072 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004073 if (ret == OK && evaluate && end_leader > start_leader
4074 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004075 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077
4078 /*
4079 * String constant: "string".
4080 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004081 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 break;
4083
4084 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004085 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004087 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004088 break;
4089
4090 /*
4091 * List: [expr, expr]
4092 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004093 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 break;
4095
4096 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004097 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004098 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004099 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004100 {
4101 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4102 }
4103 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004104 {
4105 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004106 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004107 }
4108 else
4109 ret = NOTDONE;
4110 break;
4111
4112 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004113 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004114 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004115 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004116 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004117 ret = NOTDONE;
4118 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004119 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004120 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004121 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004122 break;
4123
4124 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004125 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004127 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129
4130 /*
4131 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004132 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 */
LemonBoy2eaef102022-05-06 13:14:50 +01004134 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4135 ret = eval_interp_string(arg, rettv, evaluate);
4136 else
4137 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 break;
4139
4140 /*
4141 * Register contents: @r.
4142 */
4143 case '@': ++*arg;
4144 if (evaluate)
4145 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004146 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004147 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004148 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004149 emsg_invreg(**arg);
4150 else
4151 {
4152 rettv->v_type = VAR_STRING;
4153 rettv->vval.v_string = get_reg_contents(**arg,
4154 GREG_EXPR_SRC);
4155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157 if (**arg != NUL)
4158 ++*arg;
4159 break;
4160
4161 /*
4162 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004163 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004165 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004166 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004167 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004168 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004169 if (ret == OK && evaluate)
4170 {
4171 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4172
Bram Moolenaara9931532021-06-12 15:58:16 +02004173 // Compile it here to get the return type. The return
4174 // type is optional, when it's missing use t_unknown.
4175 // This is recognized in compile_return().
4176 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4177 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004178 if (compile_def_function(ufunc, FALSE,
4179 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004180 {
4181 clear_tv(rettv);
4182 ret = FAIL;
4183 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004184 }
4185 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004186 if (ret == NOTDONE)
4187 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004188 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004189 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004190
Bram Moolenaar9215f012020-06-27 21:18:00 +02004191 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004192 if (**arg == ')')
4193 ++*arg;
4194 else if (ret == OK)
4195 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004196 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004197 clear_tv(rettv);
4198 ret = FAIL;
4199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 break;
4202
Bram Moolenaar8c711452005-01-14 21:53:12 +00004203 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 break;
4205 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004206
4207 if (ret == NOTDONE)
4208 {
4209 /*
4210 * Must be a variable or function name.
4211 * Can also be a curly-braces kind of name: {expr}.
4212 */
4213 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004214 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004215 if (alias != NULL)
4216 s = alias;
4217
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004218 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004219 ret = FAIL;
4220 else
4221 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004222 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4223
Bram Moolenaar4525a572022-02-13 11:57:33 +00004224 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004225 {
4226 emsg(_(e_cannot_use_underscore_here));
4227 ret = FAIL;
4228 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004229 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004230 && s[0] == 's' && s[1] == ':')
4231 {
4232 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4233 ret = FAIL;
4234 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004235 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004236 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004237 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004238 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004239 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004240 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004241 else if (flags & EVAL_CONSTANT)
4242 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004244 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004245 // get the value of "true", "false", etc. or a variable
4246 ret = FAIL;
4247 if (vim9script)
4248 ret = handle_predefined(s, len, rettv);
4249 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004250 {
4251 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004252 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004253 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004254 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004255 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004256 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004257 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004258 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004259 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004260 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004262 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004263 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004264 }
4265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004266 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4267 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004268 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004269 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 /*
4272 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4273 */
4274 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004275 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004276
4277 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004278 return ret;
4279}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004281/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004282 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004283 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004284 * Adjusts "end_leaderp" until it is at "start_leader".
4285 */
4286 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004287eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004288 typval_T *rettv,
4289 int numeric_only,
4290 char_u *start_leader,
4291 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004292{
4293 char_u *end_leader = *end_leaderp;
4294 int ret = OK;
4295 int error = FALSE;
4296 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004297 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004298 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004299 float_T f = 0.0;
4300
4301 if (rettv->v_type == VAR_FLOAT)
4302 f = rettv->vval.v_float;
4303 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004304 {
4305 while (VIM_ISWHITE(end_leader[-1]))
4306 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004307 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004308 val = tv2bool(rettv);
4309 else
4310 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004311 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004312 if (error)
4313 {
4314 clear_tv(rettv);
4315 ret = FAIL;
4316 }
4317 else
4318 {
4319 while (end_leader > start_leader)
4320 {
4321 --end_leader;
4322 if (*end_leader == '!')
4323 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004324 if (numeric_only)
4325 {
4326 ++end_leader;
4327 break;
4328 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004329 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004330 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004331 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004332 {
4333 rettv->v_type = VAR_BOOL;
4334 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4335 }
4336 else
4337 f = !f;
4338 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004339 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004340 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004341 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004342 type = VAR_BOOL;
4343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004344 }
4345 else if (*end_leader == '-')
4346 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004347 if (rettv->v_type == VAR_FLOAT)
4348 f = -f;
4349 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004350 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004351 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004352 type = VAR_NUMBER;
4353 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004354 }
4355 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004356 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004359 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 else
4362 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004363 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004364 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004365 rettv->v_type = type;
4366 else
4367 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004368 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004371 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return ret;
4373}
4374
4375/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004376 * Call the function referred to in "rettv".
4377 */
4378 static int
4379call_func_rettv(
4380 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004381 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004382 typval_T *rettv,
4383 int evaluate,
4384 dict_T *selfdict,
4385 typval_T *basetv)
4386{
4387 partial_T *pt = NULL;
4388 funcexe_T funcexe;
4389 typval_T functv;
4390 char_u *s;
4391 int ret;
4392
4393 // need to copy the funcref so that we can clear rettv
4394 if (evaluate)
4395 {
4396 functv = *rettv;
4397 rettv->v_type = VAR_UNKNOWN;
4398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004399 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004400 if (functv.v_type == VAR_PARTIAL)
4401 {
4402 pt = functv.vval.v_partial;
4403 s = partial_name(pt);
4404 }
4405 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004406 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004407 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004408 if (s == NULL || *s == NUL)
4409 {
4410 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004411 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004412 goto theend;
4413 }
4414 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004415 }
4416 else
4417 s = (char_u *)"";
4418
Bram Moolenaara80faa82020-04-12 19:37:17 +02004419 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004420 funcexe.fe_firstline = curwin->w_cursor.lnum;
4421 funcexe.fe_lastline = curwin->w_cursor.lnum;
4422 funcexe.fe_evaluate = evaluate;
4423 funcexe.fe_partial = pt;
4424 funcexe.fe_selfdict = selfdict;
4425 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004426 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004427
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004428theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004429 // Clear the funcref afterwards, so that deleting it while
4430 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004431 if (evaluate)
4432 clear_tv(&functv);
4433
4434 return ret;
4435}
4436
4437/*
4438 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004439 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004440 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4441 */
4442 static int
4443eval_lambda(
4444 char_u **arg,
4445 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004446 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004447 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004448{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004449 int evaluate = evalarg != NULL
4450 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004451 typval_T base = *rettv;
4452 int ret;
4453
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004454 rettv->v_type = VAR_UNKNOWN;
4455
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004456 if (**arg == '{')
4457 {
4458 // ->{lambda}()
4459 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4460 }
4461 else
4462 {
4463 // ->(lambda)()
4464 ++*arg;
4465 ret = eval1(arg, rettv, evalarg);
4466 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004467 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004468 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004469 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004470 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004471 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004472 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4473 && rettv->v_type != VAR_PARTIAL)
4474 {
4475 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4476 return FAIL;
4477 }
4478 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004479 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004480 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004481 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004482
4483 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004484 {
4485 if (verbose)
4486 {
4487 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004488 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004489 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004490 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004491 }
4492 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004493 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004494 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004495 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004496 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004497
4498 // Clear the funcref afterwards, so that deleting it while
4499 // evaluating the arguments is possible (see test55).
4500 if (evaluate)
4501 clear_tv(&base);
4502
4503 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004504}
4505
4506/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004507 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004508 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004509 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4510 */
4511 static int
4512eval_method(
4513 char_u **arg,
4514 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004515 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004517{
4518 char_u *name;
4519 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004520 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004521 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004522 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004523 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004524 int evaluate = evalarg != NULL
4525 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004526
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004527 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004528
Bram Moolenaarac92e252019-08-03 21:58:38 +02004529 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004530 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004531 if (alias != NULL)
4532 name = alias;
4533
4534 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004535 {
4536 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004537 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004538 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004539 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004540 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004541 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004542 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004543
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004544 // If there is no "(" immediately following, but there is further on,
4545 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4546 // Does not handle anything where "(" is part of the expression.
4547 *arg = skipwhite(*arg);
4548
4549 if (**arg != '(' && alias == NULL
4550 && (paren = vim_strchr(*arg, '(')) != NULL)
4551 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004552 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004553
4554 // Truncate the name a the "(". Avoid trying to get another line
4555 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004556 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004557 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4558 if (evalarg != NULL)
4559 {
4560 getline = evalarg->eval_getline;
4561 evalarg->eval_getline = NULL;
4562 }
4563
4564 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004565 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004566 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004567 *arg = name + len;
4568 ret = FAIL;
4569 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004570 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004571 {
4572 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004573 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004574 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004575
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004576 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004577 if (getline != NULL)
4578 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004579 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004580
4581 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004582 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004583 *arg = skipwhite(*arg);
4584
4585 if (**arg != '(')
4586 {
4587 if (verbose)
4588 semsg(_(e_missing_parenthesis_str), name);
4589 ret = FAIL;
4590 }
4591 else if (VIM_ISWHITE((*arg)[-1]))
4592 {
4593 if (verbose)
4594 emsg(_(e_no_white_space_allowed_before_parenthesis));
4595 ret = FAIL;
4596 }
4597 else
4598 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004599 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004600 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004601 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004602
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004603 // Clear the funcref afterwards, so that deleting it while
4604 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004605 if (evaluate)
4606 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004607 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004608
Bram Moolenaarac92e252019-08-03 21:58:38 +02004609 return ret;
4610}
4611
4612/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004613 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4614 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4616 */
4617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004618eval_index(
4619 char_u **arg,
4620 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004621 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004624 int evaluate = evalarg != NULL
4625 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004627 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004630 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004631 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004633 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4634 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004636 init_tv(&var1);
4637 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640 /*
4641 * dict.name
4642 */
4643 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004644 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004646 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004647 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004648 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 }
4650 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004652 /*
4653 * something[idx]
4654 *
4655 * Get the (first) variable from inside the [].
4656 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004657 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 if (**arg == ':')
4659 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004660 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004662 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004663 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004664 semsg(_(e_white_space_required_before_and_after_str_at_str),
4665 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004666 clear_tv(&var1);
4667 return FAIL;
4668 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004669 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004671 int error = FALSE;
4672
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004673 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004674 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004675 && var1.v_type == VAR_FLOAT)
4676 {
4677 var1.vval.v_string = typval_tostring(&var1, TRUE);
4678 var1.v_type = VAR_STRING;
4679 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004680
Bram Moolenaar4525a572022-02-13 11:57:33 +00004681 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004682 tv_get_number_chk(&var1, &error);
4683 else
4684 error = tv_get_string_chk(&var1) == NULL;
4685 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004686 {
4687 // not a number or string
4688 clear_tv(&var1);
4689 return FAIL;
4690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692
4693 /*
4694 * Get the second variable from inside the [:].
4695 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004696 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 if (**arg == ':')
4698 {
4699 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004700 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004701 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004702 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004703 semsg(_(e_white_space_required_before_and_after_str_at_str),
4704 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004705 if (!empty1)
4706 clear_tv(&var1);
4707 return FAIL;
4708 }
4709 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710 if (**arg == ']')
4711 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004712 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 if (!empty1)
4715 clear_tv(&var1);
4716 return FAIL;
4717 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004718 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004720 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 if (!empty1)
4722 clear_tv(&var1);
4723 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004724 return FAIL;
4725 }
4726 }
4727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004729 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 if (**arg != ']')
4731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004732 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004733 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004734 clear_tv(&var1);
4735 if (range)
4736 clear_tv(&var2);
4737 return FAIL;
4738 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004739 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741
4742 if (evaluate)
4743 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004744 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004745 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004746 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004747
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004748 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004751 clear_tv(&var2);
4752 return res;
4753 }
4754 return OK;
4755}
4756
4757/*
4758 * Check if "rettv" can have an [index] or [sli:ce]
4759 */
4760 int
4761check_can_index(typval_T *rettv, int evaluate, int verbose)
4762{
4763 switch (rettv->v_type)
4764 {
4765 case VAR_FUNC:
4766 case VAR_PARTIAL:
4767 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004768 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004769 return FAIL;
4770 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004771 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004772 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004774 case VAR_BOOL:
4775 case VAR_SPECIAL:
4776 case VAR_JOB:
4777 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004778 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004779 case VAR_CLASS:
4780 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004781 if (verbose)
4782 emsg(_(e_cannot_index_special_variable));
4783 return FAIL;
4784 case VAR_UNKNOWN:
4785 case VAR_ANY:
4786 case VAR_VOID:
4787 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004789 emsg(_(e_cannot_index_special_variable));
4790 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004792 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004793
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794 case VAR_STRING:
4795 case VAR_LIST:
4796 case VAR_DICT:
4797 case VAR_BLOB:
4798 break;
4799 case VAR_NUMBER:
4800 if (in_vim9script())
4801 emsg(_(e_cannot_index_number));
4802 break;
4803 }
4804 return OK;
4805}
4806
4807/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004808 * slice() function
4809 */
4810 void
4811f_slice(typval_T *argvars, typval_T *rettv)
4812{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004813 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004814 && ((argvars[0].v_type != VAR_STRING
4815 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004816 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004817 && check_for_list_arg(argvars, 0) == FAIL)
4818 || check_for_number_arg(argvars, 1) == FAIL
4819 || check_for_opt_number_arg(argvars, 2) == FAIL))
4820 return;
4821
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00004822 if (check_can_index(argvars, TRUE, FALSE) != OK)
4823 return;
4824
4825 copy_tv(argvars, rettv);
4826 eval_index_inner(rettv, TRUE, argvars + 1,
4827 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4828 TRUE, NULL, 0, FALSE);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004829}
4830
4831/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004832 * Apply index or range to "rettv".
4833 * "var1" is the first index, NULL for [:expr].
4834 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004835 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4836 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004837 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4838 */
4839 int
4840eval_index_inner(
4841 typval_T *rettv,
4842 int is_range,
4843 typval_T *var1,
4844 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004845 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004846 char_u *key,
4847 int keylen,
4848 int verbose)
4849{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004850 varnumber_T n1, n2 = 0;
4851 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004852
4853 n1 = 0;
4854 if (var1 != NULL && rettv->v_type != VAR_DICT)
4855 n1 = tv_get_number(var1);
4856
4857 if (is_range)
4858 {
4859 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004861 if (verbose)
4862 emsg(_(e_cannot_slice_dictionary));
4863 return FAIL;
4864 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004865 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004866 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004867 else
4868 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004869 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004870
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004871 switch (rettv->v_type)
4872 {
4873 case VAR_UNKNOWN:
4874 case VAR_ANY:
4875 case VAR_VOID:
4876 case VAR_FUNC:
4877 case VAR_PARTIAL:
4878 case VAR_FLOAT:
4879 case VAR_BOOL:
4880 case VAR_SPECIAL:
4881 case VAR_JOB:
4882 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004883 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004884 case VAR_CLASS:
4885 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004886 break; // not evaluating, skipping over subscript
4887
4888 case VAR_NUMBER:
4889 case VAR_STRING:
4890 {
4891 char_u *s = tv_get_string(rettv);
4892
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004894 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004895 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004896 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004897 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004898 else
4899 s = char_from_string(s, n1);
4900 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004901 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // The resulting variable is a substring. If the indexes
4904 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 if (n1 < 0)
4906 {
4907 n1 = len + n1;
4908 if (n1 < 0)
4909 n1 = 0;
4910 }
4911 if (n2 < 0)
4912 n2 = len + n2;
4913 else if (n2 >= len)
4914 n2 = len;
4915 if (n1 >= len || n2 < 0 || n1 > n2)
4916 s = NULL;
4917 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004918 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 }
4920 else
4921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 // The resulting variable is a string of a single
4923 // character. If the index is too big or negative the
4924 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004925 if (n1 >= len || n1 < 0)
4926 s = NULL;
4927 else
4928 s = vim_strnsave(s + n1, 1);
4929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
4931 rettv->v_type = VAR_STRING;
4932 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004933 }
4934 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004936 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004937 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4938 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004939 break;
4940
4941 case VAR_LIST:
4942 if (var1 == NULL)
4943 n1 = 0;
4944 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004945 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004946 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004947 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004948 return FAIL;
4949 break;
4950
4951 case VAR_DICT:
4952 {
4953 dictitem_T *item;
4954 typval_T tmp;
4955
4956 if (key == NULL)
4957 {
4958 key = tv_get_string_chk(var1);
4959 if (key == NULL)
4960 return FAIL;
4961 }
4962
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004963 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004964
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004965 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004966 {
4967 if (verbose)
4968 {
4969 if (keylen > 0)
4970 key[keylen] = NUL;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004971 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004972 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004973 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004974 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004975
4976 copy_tv(&item->di_tv, &tmp);
4977 clear_tv(rettv);
4978 *rettv = tmp;
4979 }
4980 break;
4981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 return OK;
4983}
4984
4985/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004986 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004987 */
4988 char_u *
4989partial_name(partial_T *pt)
4990{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004991 if (pt != NULL)
4992 {
4993 if (pt->pt_name != NULL)
4994 return pt->pt_name;
4995 if (pt->pt_func != NULL)
4996 return pt->pt_func->uf_name;
4997 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004998 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004999}
5000
Bram Moolenaarddecc252016-04-06 22:59:37 +02005001 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005002partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005003{
5004 int i;
5005
5006 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005007 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005008 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005009 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005010 if (pt->pt_name != NULL)
5011 {
5012 func_unref(pt->pt_name);
5013 vim_free(pt->pt_name);
5014 }
5015 else
5016 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005017
Bram Moolenaar54656012021-06-09 20:50:46 +02005018 // "out_up" is no longer used, decrement refcount on partial that owns it.
5019 partial_unref(pt->pt_outer.out_up_partial);
5020
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005021 // Using pt_outer from another partial.
5022 partial_unref(pt->pt_outer_partial);
5023
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005024 // Decrease the reference count for the context of a closure. If down
5025 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005026 if (pt->pt_funcstack != NULL)
5027 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005028 --pt->pt_funcstack->fs_refcount;
5029 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005030 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005031 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005032 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5033 if (pt->pt_loopvars[i] != NULL)
5034 {
5035 --pt->pt_loopvars[i]->lvs_refcount;
5036 loopvars_check_refcount(pt->pt_loopvars[i]);
5037 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005038
Bram Moolenaarddecc252016-04-06 22:59:37 +02005039 vim_free(pt);
5040}
5041
5042/*
5043 * Unreference a closure: decrement the reference count and free it when it
5044 * becomes zero.
5045 */
5046 void
5047partial_unref(partial_T *pt)
5048{
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005049 if (pt == NULL)
5050 return;
5051
5052 int done = FALSE;
5053
5054 if (--pt->pt_refcount <= 0)
5055 partial_free(pt);
5056
5057 // If the reference count goes down to one, the funcstack may be the
5058 // only reference and can be freed if no other partials reference it.
5059 else if (pt->pt_refcount == 1)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005060 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005061 // careful: if the funcstack is freed it may contain this partial
5062 // and it gets freed as well
5063 if (pt->pt_funcstack != NULL)
5064 done = funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005065
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005066 if (!done)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005067 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005068 int depth;
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005069
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00005070 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5071 if (pt->pt_loopvars[depth] != NULL
5072 && loopvars_check_refcount(pt->pt_loopvars[depth]))
Bram Moolenaarcc341812022-09-19 15:54:34 +01005073 break;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005074 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005075 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005076}
5077
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005078/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005079 * Return the next (unique) copy ID.
5080 * Used for serializing nested structures.
5081 */
5082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005083get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005084{
5085 current_copyID += COPYID_INC;
5086 return current_copyID;
5087}
5088
5089/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090 * Garbage collection for lists and dictionaries.
5091 *
5092 * We use reference counts to be able to free most items right away when they
5093 * are no longer used. But for composite items it's possible that it becomes
5094 * unused while the reference count is > 0: When there is a recursive
5095 * reference. Example:
5096 * :let l = [1, 2, 3]
5097 * :let d = {9: l}
5098 * :let l[1] = d
5099 *
5100 * Since this is quite unusual we handle this with garbage collection: every
5101 * once in a while find out which lists and dicts are not referenced from any
5102 * variable.
5103 *
5104 * Here is a good reference text about garbage collection (refers to Python
5105 * but it applies to all reference-counting mechanisms):
5106 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005107 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005108
5109/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005110 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005111 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005112 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005113 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005114 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005115garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005116{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005117 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005118 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005119 buf_T *buf;
5120 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005121 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005122 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005123
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005124 if (!testing)
5125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005127 want_garbage_collect = FALSE;
5128 may_garbage_collect = FALSE;
5129 garbage_collect_at_exit = FALSE;
5130 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005131
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005132 // The execution stack can grow big, limit the size.
5133 if (exestack.ga_maxlen - exestack.ga_len > 500)
5134 {
5135 size_t new_len;
5136 char_u *pp;
5137 int n;
5138
5139 // Keep 150% of the current size, with a minimum of the growth size.
5140 n = exestack.ga_len / 2;
5141 if (n < exestack.ga_growsize)
5142 n = exestack.ga_growsize;
5143
5144 // Don't make it bigger though.
5145 if (exestack.ga_len + n < exestack.ga_maxlen)
5146 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005147 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005148 pp = vim_realloc(exestack.ga_data, new_len);
5149 if (pp == NULL)
5150 return FAIL;
5151 exestack.ga_maxlen = exestack.ga_len + n;
5152 exestack.ga_data = pp;
5153 }
5154 }
5155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 // We advance by two because we add one for items referenced through
5157 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005158 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005159
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005160 /*
5161 * 1. Go through all accessible variables and mark all lists and dicts
5162 * with copyID.
5163 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // Don't free variables in the previous_funccal list unless they are only
5166 // referenced through previous_funccal. This must be first, because if
5167 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005168 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005170 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005171 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005173 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005174 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005175 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5176 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005179 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005180 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5181 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005182 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005183 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005184 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005185 abort = abort || set_ref_in_item(
5186 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005187#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005188 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005189 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5190 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005191 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005192 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005193 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5194 NULL, NULL);
5195#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005198 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5200 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005201 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005202 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005204 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005208 abort = abort || set_ref_in_functions(copyID);
5209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005212
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005213 // funcstacks keep variables for closures
5214 abort = abort || set_ref_in_funcstacks(copyID);
5215
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005216 // loopvars keep variables for loop blocks
5217 abort = abort || set_ref_in_loopvars(copyID);
5218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005220 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005221
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005222 // callbacks in buffers
5223 abort = abort || set_ref_in_buffers(copyID);
5224
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005225 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5226 abort = abort || set_ref_in_insexpand_funcs(copyID);
5227
5228 // 'operatorfunc' callback
5229 abort = abort || set_ref_in_opfunc(copyID);
5230
5231 // 'tagfunc' callback
5232 abort = abort || set_ref_in_tagfunc(copyID);
5233
5234 // 'imactivatefunc' and 'imstatusfunc' callbacks
5235 abort = abort || set_ref_in_im_funcs(copyID);
5236
Bram Moolenaar1dced572012-04-05 16:54:08 +02005237#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005238 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005239#endif
5240
Bram Moolenaardb913952012-06-29 12:54:53 +02005241#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005242 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005243#endif
5244
5245#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005246 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005247#endif
5248
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005249#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005250 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005251 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005252#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005253#ifdef FEAT_NETBEANS_INTG
5254 abort = abort || set_ref_in_nb_channel(copyID);
5255#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005256
Bram Moolenaare3188e22016-05-31 21:13:04 +02005257#ifdef FEAT_TIMERS
5258 abort = abort || set_ref_in_timer(copyID);
5259#endif
5260
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005261#ifdef FEAT_QUICKFIX
5262 abort = abort || set_ref_in_quickfix(copyID);
5263#endif
5264
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005265#ifdef FEAT_TERMINAL
5266 abort = abort || set_ref_in_term(copyID);
5267#endif
5268
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005269#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005270 abort = abort || set_ref_in_popups(copyID);
5271#endif
5272
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005274 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005275 /*
5276 * 2. Free lists and dictionaries that are not referenced.
5277 */
5278 did_free = free_unref_items(copyID);
5279
5280 /*
5281 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005283 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005285 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005286 else if (p_verbose > 0)
5287 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005288 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005289 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005290
5291 return did_free;
5292}
5293
5294/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005295 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005296 */
5297 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005298free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005299{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005300 int did_free = FALSE;
5301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // Let all "free" functions know that we are here. This means no
5303 // dictionaries, lists, channels or jobs are to be freed, because we will
5304 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005305 in_free_unref_items = TRUE;
5306
5307 /*
5308 * PASS 1: free the contents of the items. We don't free the items
5309 * themselves yet, so that it is possible to decrement refcount counters
5310 */
5311
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005312 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005313 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005315 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005316 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005317
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005318 // Go through the list of objects and free items without this copyID.
5319 did_free |= object_free_nonref(copyID);
5320
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 // Go through the list of jobs and free items without the copyID. This
5323 // must happen before doing channels, because jobs refer to channels, but
5324 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005325 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005328 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5329#endif
5330
5331 /*
5332 * PASS 2: free the items themselves.
5333 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005334 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005335 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005336
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005337#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // Go through the list of jobs and free items without the copyID. This
5339 // must happen before doing channels, because jobs refer to channels, but
5340 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005341 free_unused_jobs(copyID, COPYID_MASK);
5342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005344 free_unused_channels(copyID, COPYID_MASK);
5345#endif
5346
5347 in_free_unref_items = FALSE;
5348
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349 return did_free;
5350}
5351
5352/*
5353 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 * "list_stack" is used to add lists to be marked. Can be NULL.
5355 *
5356 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005357 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005358 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005359set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005360{
5361 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005363 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005364 hashtab_T *cur_ht;
5365 ht_stack_T *ht_stack = NULL;
5366 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005367
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005368 cur_ht = ht;
5369 for (;;)
5370 {
5371 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 // Mark each item in the hashtab. If the item contains a hashtab
5374 // it is added to ht_stack, if it contains a list it is added to
5375 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005376 todo = (int)cur_ht->ht_used;
5377 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5378 if (!HASHITEM_EMPTY(hi))
5379 {
5380 --todo;
5381 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5382 &ht_stack, list_stack);
5383 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005384 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005385
5386 if (ht_stack == NULL)
5387 break;
5388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005389 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005390 cur_ht = ht_stack->ht;
5391 tempitem = ht_stack;
5392 ht_stack = ht_stack->prev;
5393 free(tempitem);
5394 }
5395
5396 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005397}
5398
Dominique Pelle748b3082022-01-08 12:41:16 +00005399#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5400 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005401/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005402 * Mark a dict and its items with "copyID".
5403 * Returns TRUE if setting references failed somehow.
5404 */
5405 int
5406set_ref_in_dict(dict_T *d, int copyID)
5407{
5408 if (d != NULL && d->dv_copyID != copyID)
5409 {
5410 d->dv_copyID = copyID;
5411 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5412 }
5413 return FALSE;
5414}
Dominique Pelle748b3082022-01-08 12:41:16 +00005415#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005416
5417/*
5418 * Mark a list and its items with "copyID".
5419 * Returns TRUE if setting references failed somehow.
5420 */
5421 int
5422set_ref_in_list(list_T *ll, int copyID)
5423{
5424 if (ll != NULL && ll->lv_copyID != copyID)
5425 {
5426 ll->lv_copyID = copyID;
5427 return set_ref_in_list_items(ll, copyID, NULL);
5428 }
5429 return FALSE;
5430}
5431
5432/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005433 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005434 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5435 *
5436 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005437 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005439set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005440{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005441 listitem_T *li;
5442 int abort = FALSE;
5443 list_T *cur_l;
5444 list_stack_T *list_stack = NULL;
5445 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005446
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005447 cur_l = l;
5448 for (;;)
5449 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005450 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 // Mark each item in the list. If the item contains a hashtab
5452 // it is added to ht_stack, if it contains a list it is added to
5453 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005454 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5455 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5456 ht_stack, &list_stack);
5457 if (list_stack == NULL)
5458 break;
5459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005460 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005461 cur_l = list_stack->list;
5462 tempitem = list_stack;
5463 list_stack = list_stack->prev;
5464 free(tempitem);
5465 }
5466
5467 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005468}
5469
5470/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005471 * Mark the partial in callback 'cb' with "copyID".
5472 */
5473 int
5474set_ref_in_callback(callback_T *cb, int copyID)
5475{
5476 typval_T tv;
5477
5478 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5479 return FALSE;
5480
5481 tv.v_type = VAR_PARTIAL;
5482 tv.vval.v_partial = cb->cb_partial;
5483 return set_ref_in_item(&tv, copyID, NULL, NULL);
5484}
5485
5486/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005487 * Mark all lists, dicts and other container types referenced through typval
5488 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005489 * "list_stack" is used to add lists to be marked. Can be NULL.
5490 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5491 *
5492 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005493 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495set_ref_in_item(
5496 typval_T *tv,
5497 int copyID,
5498 ht_stack_T **ht_stack,
5499 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005500{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005501 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005502
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005503 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005504 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005505 case VAR_DICT:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005506 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005507 dict_T *dd = tv->vval.v_dict;
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005508
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005509 if (dd != NULL && dd->dv_copyID != copyID)
5510 {
5511 // Didn't see this dict yet.
5512 dd->dv_copyID = copyID;
5513 if (ht_stack == NULL)
5514 {
5515 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5516 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005517 else
5518 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005519 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005520
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005521 if (newitem == NULL)
5522 abort = TRUE;
5523 else
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005525 newitem->ht = &dd->dv_hashtab;
5526 newitem->prev = *ht_stack;
5527 *ht_stack = newitem;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005528 }
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005529 }
5530 }
5531 break;
5532 }
5533
5534 case VAR_LIST:
5535 {
5536 list_T *ll = tv->vval.v_list;
5537
5538 if (ll != NULL && ll->lv_copyID != copyID)
5539 {
5540 // Didn't see this list yet.
5541 ll->lv_copyID = copyID;
5542 if (list_stack == NULL)
5543 {
5544 abort = set_ref_in_list_items(ll, copyID, ht_stack);
5545 }
5546 else
5547 {
5548 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5549
5550 if (newitem == NULL)
5551 abort = TRUE;
5552 else
5553 {
5554 newitem->list = ll;
5555 newitem->prev = *list_stack;
5556 *list_stack = newitem;
5557 }
5558 }
5559 }
5560 break;
5561 }
5562
5563 case VAR_FUNC:
5564 {
5565 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5566 break;
5567 }
5568
5569 case VAR_PARTIAL:
5570 {
5571 partial_T *pt = tv->vval.v_partial;
5572 int i;
5573
5574 if (pt != NULL && pt->pt_copyID != copyID)
5575 {
5576 // Didn't see this partial yet.
5577 pt->pt_copyID = copyID;
5578
5579 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5580
5581 if (pt->pt_dict != NULL)
5582 {
5583 typval_T dtv;
5584
5585 dtv.v_type = VAR_DICT;
5586 dtv.vval.v_dict = pt->pt_dict;
5587 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5588 }
5589
5590 for (i = 0; i < pt->pt_argc; ++i)
5591 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5592 ht_stack, list_stack);
5593 // pt_funcstack is handled in set_ref_in_funcstacks()
5594 // pt_loopvars is handled in set_ref_in_loopvars()
5595 }
5596 break;
5597 }
5598
5599 case VAR_JOB:
5600 {
5601#ifdef FEAT_JOB_CHANNEL
5602 job_T *job = tv->vval.v_job;
5603 typval_T dtv;
5604
5605 if (job != NULL && job->jv_copyID != copyID)
5606 {
5607 job->jv_copyID = copyID;
5608 if (job->jv_channel != NULL)
5609 {
5610 dtv.v_type = VAR_CHANNEL;
5611 dtv.vval.v_channel = job->jv_channel;
5612 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5613 }
5614 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005615 {
5616 dtv.v_type = VAR_PARTIAL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005617 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5619 }
5620 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005621#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005622 break;
5623 }
5624
5625 case VAR_CHANNEL:
5626 {
5627#ifdef FEAT_JOB_CHANNEL
5628 channel_T *ch = tv->vval.v_channel;
5629 ch_part_T part;
5630 typval_T dtv;
5631 jsonq_T *jq;
5632 cbq_T *cq;
5633
5634 if (ch != NULL && ch->ch_copyID != copyID)
5635 {
5636 ch->ch_copyID = copyID;
5637 for (part = PART_SOCK; part < PART_COUNT; ++part)
5638 {
5639 for (jq = ch->ch_part[part].ch_json_head.jq_next;
5640 jq != NULL; jq = jq->jq_next)
5641 set_ref_in_item(jq->jq_value, copyID,
5642 ht_stack, list_stack);
5643 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5644 cq = cq->cq_next)
5645 if (cq->cq_callback.cb_partial != NULL)
5646 {
5647 dtv.v_type = VAR_PARTIAL;
5648 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5649 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5650 }
5651 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5652 {
5653 dtv.v_type = VAR_PARTIAL;
5654 dtv.vval.v_partial =
5655 ch->ch_part[part].ch_callback.cb_partial;
5656 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5657 }
5658 }
5659 if (ch->ch_callback.cb_partial != NULL)
5660 {
5661 dtv.v_type = VAR_PARTIAL;
5662 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5663 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5664 }
5665 if (ch->ch_close_cb.cb_partial != NULL)
5666 {
5667 dtv.v_type = VAR_PARTIAL;
5668 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5669 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5670 }
5671 }
5672#endif
5673 break;
5674 }
5675
5676 case VAR_CLASS:
Bram Moolenaarcf760d52023-01-05 13:16:04 +00005677 {
5678 class_T *cl = tv->vval.v_class;
Bram Moolenaar94674f22023-01-06 18:42:20 +00005679 if (cl != NULL && cl->class_copyID != copyID
5680 && (cl->class_flags && CLASS_INTERFACE) == 0)
Bram Moolenaarcf760d52023-01-05 13:16:04 +00005681 {
5682 cl->class_copyID = copyID;
5683 for (int i = 0; !abort
5684 && i < cl->class_class_member_count; ++i)
5685 abort = abort || set_ref_in_item(
5686 &cl->class_members_tv[i],
5687 copyID, ht_stack, list_stack);
5688
5689
5690 for (int i = 0; !abort
5691 && i < cl->class_class_function_count; ++i)
5692 abort = abort || set_ref_in_func(NULL,
5693 cl->class_class_functions[i], copyID);
5694
5695 for (int i = 0; !abort
5696 && i < cl->class_obj_method_count; ++i)
5697 abort = abort || set_ref_in_func(NULL,
5698 cl->class_obj_methods[i], copyID);
5699
5700 // Mark initializer expressions?
5701 }
5702 break;
5703 }
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005704
5705 case VAR_OBJECT:
5706 {
5707 object_T *obj = tv->vval.v_object;
5708 if (obj != NULL && obj->obj_copyID != copyID)
5709 {
5710 obj->obj_copyID = copyID;
5711
5712 // The typval_T array is right after the object_T.
5713 typval_T *mtv = (typval_T *)(obj + 1);
5714 for (int i = 0; !abort
5715 && i < obj->obj_class->class_obj_member_count; ++i)
5716 abort = abort || set_ref_in_item(mtv + i, copyID,
5717 ht_stack, list_stack);
5718 }
5719 break;
5720 }
5721
5722 case VAR_UNKNOWN:
5723 case VAR_ANY:
5724 case VAR_VOID:
5725 case VAR_BOOL:
5726 case VAR_SPECIAL:
5727 case VAR_NUMBER:
5728 case VAR_FLOAT:
5729 case VAR_STRING:
5730 case VAR_BLOB:
5731 case VAR_INSTR:
5732 // Types that do not contain any other item
5733 break;
5734 }
5735
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005736 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005737}
5738
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 * Return a string with the string representation of a variable.
5741 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005742 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005743 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005744 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005745 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005746 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005747 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5748 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005749 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005751 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005752echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005753 typval_T *tv,
5754 char_u **tofree,
5755 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005756 int copyID,
5757 int echo_style,
5758 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005759 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761 static int recurse = 0;
5762 char_u *r = NULL;
5763
Bram Moolenaar33570922005-01-25 22:26:29 +00005764 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005765 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005766 if (!did_echo_string_emsg)
5767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005768 // Only give this message once for a recursive call to avoid
5769 // flooding the user with errors. And stop iterating over lists
5770 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005771 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005772 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005774 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005775 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776 }
5777 ++recurse;
5778
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 switch (tv->v_type)
5780 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005781 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005782 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005783 {
5784 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005785 r = tv->vval.v_string;
5786 if (r == NULL)
5787 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005788 }
5789 else
5790 {
5791 *tofree = string_quote(tv->vval.v_string, FALSE);
5792 r = *tofree;
5793 }
5794 break;
5795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005797 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005798 char_u buf[MAX_FUNC_NAME_LEN];
5799
5800 if (echo_style)
5801 {
LemonBoya5d35902022-04-29 21:15:02 +01005802 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5803 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005804 buf, MAX_FUNC_NAME_LEN);
5805 if (r == buf)
5806 {
5807 r = vim_strsave(buf);
5808 *tofree = r;
5809 }
5810 else
5811 *tofree = NULL;
5812 }
5813 else
5814 {
5815 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5816 : make_ufunc_name_readable(
5817 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5818 TRUE);
5819 r = *tofree;
5820 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005823
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005824 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005825 {
5826 partial_T *pt = tv->vval.v_partial;
5827 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005828 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005829 garray_T ga;
5830 int i;
5831 char_u *tf;
5832
5833 ga_init2(&ga, 1, 100);
5834 ga_concat(&ga, (char_u *)"function(");
5835 if (fname != NULL)
5836 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005837 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005838 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005839 && vim_isupper(fname[1]))
5840 {
5841 ga_concat(&ga, (char_u *)"'g:");
5842 ga_concat(&ga, fname + 1);
5843 }
5844 else
5845 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005846 vim_free(fname);
5847 }
5848 if (pt != NULL && pt->pt_argc > 0)
5849 {
5850 ga_concat(&ga, (char_u *)", [");
5851 for (i = 0; i < pt->pt_argc; ++i)
5852 {
5853 if (i > 0)
5854 ga_concat(&ga, (char_u *)", ");
5855 ga_concat(&ga,
5856 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5857 vim_free(tf);
5858 }
5859 ga_concat(&ga, (char_u *)"]");
5860 }
5861 if (pt != NULL && pt->pt_dict != NULL)
5862 {
5863 typval_T dtv;
5864
5865 ga_concat(&ga, (char_u *)", ");
5866 dtv.v_type = VAR_DICT;
5867 dtv.vval.v_dict = pt->pt_dict;
5868 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5869 vim_free(tf);
5870 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005871 // terminate with ')' and a NUL
5872 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005873
5874 *tofree = ga.ga_data;
5875 r = *tofree;
5876 break;
5877 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005878
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005879 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005880 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005881 break;
5882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005884 if (tv->vval.v_list == NULL)
5885 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005886 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005887 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005888 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005889 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005890 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5891 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005892 {
5893 *tofree = NULL;
5894 r = (char_u *)"[...]";
5895 }
5896 else
5897 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 int old_copyID = tv->vval.v_list->lv_copyID;
5899
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005901 *tofree = list2string(tv, copyID, restore_copyID);
5902 if (restore_copyID)
5903 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 r = *tofree;
5905 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005906 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005907
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005909 if (tv->vval.v_dict == NULL)
5910 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005911 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005912 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005913 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005914 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005915 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5916 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005917 {
5918 *tofree = NULL;
5919 r = (char_u *)"{...}";
5920 }
5921 else
5922 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005923 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005924
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005925 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005926 *tofree = dict2string(tv, copyID, restore_copyID);
5927 if (restore_copyID)
5928 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005929 r = *tofree;
5930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005933 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005934 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005935 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005937 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005938 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005939 break;
5940
Bram Moolenaar835dc632016-02-07 14:27:38 +01005941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005942 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005944 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005945 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5946 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005947 if (composite_val)
5948 {
5949 *tofree = string_quote(r, FALSE);
5950 r = *tofree;
5951 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005952#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005954
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005955 case VAR_INSTR:
5956 *tofree = NULL;
5957 r = (char_u *)"instructions";
5958 break;
5959
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005960 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005961 {
5962 class_T *cl = tv->vval.v_class;
5963 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5964 r = *tofree = alloc(len);
5965 vim_snprintf((char *)r, len, "class %s",
5966 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5967 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005968 break;
5969
5970 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005971 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005972 garray_T ga;
5973 ga_init2(&ga, 1, 50);
5974 ga_concat(&ga, (char_u *)"object of ");
5975 object_T *obj = tv->vval.v_object;
5976 class_T *cl = obj == NULL ? NULL : obj->obj_class;
5977 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
5978 : cl->class_name);
5979 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005980 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005981 ga_concat(&ga, (char_u *)" {");
5982 for (int i = 0; i < cl->class_obj_member_count; ++i)
5983 {
5984 if (i > 0)
5985 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00005986 ocmember_T *m = &cl->class_obj_members[i];
5987 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005988 ga_concat(&ga, (char_u *)": ");
5989 char_u *tf = NULL;
5990 ga_concat(&ga, echo_string_core(
5991 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005992 &tf, numbuf, copyID, echo_style,
5993 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005994 vim_free(tf);
5995 }
5996 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005997 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005998
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005999 *tofree = r = ga.ga_data;
6000 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006001 break;
6002
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006003 case VAR_FLOAT:
6004 *tofree = NULL;
6005 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6006 r = numbuf;
6007 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006008
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006009 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006010 case VAR_SPECIAL:
6011 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006012 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006013 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006015
Bram Moolenaar8502c702014-06-17 12:51:16 +02006016 if (--recurse == 0)
6017 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006018 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006019}
6020
6021/*
6022 * Return a string with the string representation of a variable.
6023 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6024 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006025 * Does not put quotes around strings, as ":echo" displays values.
6026 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6027 * May return NULL.
6028 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006029 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006030echo_string(
6031 typval_T *tv,
6032 char_u **tofree,
6033 char_u *numbuf,
6034 int copyID)
6035{
6036 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6037}
6038
6039/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006040 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6041 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006042 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006043 */
6044 int
6045buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6046{
6047 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006048 char_u *t;
6049 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006050
6051 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6052 return -1;
6053
6054 if (lnum > buf->b_ml.ml_line_count)
6055 lnum = buf->b_ml.ml_line_count;
6056
6057 str = ml_get_buf(buf, lnum, FALSE);
6058 if (str == NULL)
6059 return -1;
6060
6061 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006062 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006063
Bram Moolenaar91458462021-01-13 20:08:38 +01006064 // count the number of characters
6065 t = str;
6066 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6067 t += mb_ptr2len(t);
6068
6069 // In insert mode, when the cursor is at the end of a non-empty line,
6070 // byteidx points to the NUL character immediately past the end of the
6071 // string. In this case, add one to the character count.
6072 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6073 count++;
6074
6075 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006076}
6077
6078/*
6079 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006080 * byte index. Works only for loaded buffers. Returns -1 on failure.
6081 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006082 */
6083 int
6084buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6085{
6086 char_u *str;
6087 char_u *t;
6088
6089 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6090 return -1;
6091
6092 if (lnum > buf->b_ml.ml_line_count)
6093 lnum = buf->b_ml.ml_line_count;
6094
6095 str = ml_get_buf(buf, lnum, FALSE);
6096 if (str == NULL)
6097 return -1;
6098
6099 // Convert the character offset to a byte offset
6100 t = str;
6101 while (*t != NUL && --charidx > 0)
6102 t += mb_ptr2len(t);
6103
Bram Moolenaar91458462021-01-13 20:08:38 +01006104 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006105}
6106
6107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006109 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006111 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112var2fpos(
6113 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006115 int *fnum, // set to fnum for '0, 'A, etc.
6116 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006118 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006120 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006122 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006123 if (varp->v_type == VAR_LIST)
6124 {
6125 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006126 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006127 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006128 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006129
6130 l = varp->vval.v_list;
6131 if (l == NULL)
6132 return NULL;
6133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006135 pos.lnum = list_find_nr(l, 0L, &error);
6136 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006137 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006138 if (charcol)
6139 len = (long)mb_charlen(ml_get(pos.lnum));
6140 else
6141 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006142
Bram Moolenaarec65d772020-08-20 22:29:12 +02006143 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006144 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006145 li = list_find(l, 1L);
6146 if (li != NULL && li->li_tv.v_type == VAR_STRING
6147 && li->li_tv.vval.v_string != NULL
6148 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006149 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006150 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006151 }
6152 else
6153 {
6154 pos.col = list_find_nr(l, 1L, &error);
6155 if (error)
6156 return NULL;
6157 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006160 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006161 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006162 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006164 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006165 pos.coladd = list_find_nr(l, 2L, &error);
6166 if (error)
6167 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006168
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006169 return &pos;
6170 }
6171
Bram Moolenaarc5809432021-03-27 21:23:30 +01006172 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6173 return NULL;
6174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006175 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006176 if (name == NULL)
6177 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006178
6179 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006180 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006181 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006182 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006183 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006184 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006185 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006186 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006187 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006188 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006189 pos = VIsual;
6190 else
6191 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006192 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006193 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006194 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006196 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006197 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6199 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006200 pos = *pp;
6201 }
6202 if (pos.lnum != 0)
6203 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006204 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006205 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6206 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006208
Bram Moolenaara5525202006-03-02 22:52:09 +00006209 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006210
Bram Moolenaar477933c2007-07-17 14:32:23 +00006211 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006212 {
6213 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006214 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006215 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006216 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006217 // In silent Ex mode topline is zero, but that's not a valid line
6218 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006219 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006220 return &pos;
6221 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006222 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006223 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006224 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006226 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006227 return &pos;
6228 }
6229 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006230 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006232 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 pos.lnum = curbuf->b_ml.ml_line_count;
6235 pos.col = 0;
6236 }
6237 else
6238 {
6239 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006240 if (charcol)
6241 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6242 else
6243 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245 return &pos;
6246 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006247 if (in_vim9script())
6248 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 return NULL;
6250}
6251
6252/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006253 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006254 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006255 * Note that the column is passed on as-is, the caller may want to decrement
6256 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006257 * If "charcol" is TRUE use the column as the character index instead of the
6258 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006259 * Return FAIL when conversion is not possible, doesn't check the position for
6260 * validity.
6261 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006263list2fpos(
6264 typval_T *arg,
6265 pos_T *posp,
6266 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006267 colnr_T *curswantp,
6268 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006269{
6270 list_T *l = arg->vval.v_list;
6271 long i = 0;
6272 long n;
6273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006274 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6275 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006276 if (arg->v_type != VAR_LIST
6277 || l == NULL
6278 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006279 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006280 return FAIL;
6281
6282 if (fnump != NULL)
6283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006285 if (n < 0)
6286 return FAIL;
6287 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006288 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006289 *fnump = n;
6290 }
6291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006292 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006293 if (n < 0)
6294 return FAIL;
6295 posp->lnum = n;
6296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006297 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006298 if (n < 0)
6299 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006300 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006301 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006302 if (charcol)
6303 {
6304 buf_T *buf;
6305
6306 // Get the text for the specified line in a loaded buffer
6307 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6308 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6309 return FAIL;
6310
Bram Moolenaar79f23442022-10-10 12:42:57 +01006311 n = buf_charidx_to_byteidx(buf,
6312 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006313 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006314 posp->col = n;
6315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006317 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006318 posp->coladd = 0;
6319 else
6320 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006321
Bram Moolenaar493c1782014-05-28 14:34:46 +02006322 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006323 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006324
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006325 return OK;
6326}
6327
6328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 * Get the length of an environment variable name.
6330 * Advance "arg" to the first character after the name.
6331 * Return 0 for error.
6332 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 char_u *p;
6337 int len;
6338
6339 for (p = *arg; vim_isIDc(*p); ++p)
6340 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 return 0;
6343
6344 len = (int)(p - *arg);
6345 *arg = p;
6346 return len;
6347}
6348
6349/*
6350 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006351 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * Return 0 if something is wrong.
6353 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
6357 char_u *p;
6358 int len;
6359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006360 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006362 {
6363 if (*p == ':')
6364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 // "s:" is start of "s:var", but "n:" is not and can be used in
6366 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006367 len = (int)(p - *arg);
6368 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6369 || len > 1)
6370 break;
6371 }
6372 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 return 0;
6375
6376 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006377 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 return len;
6380}
6381
6382/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006383 * Get the length of the name of a variable or function.
6384 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006386 * Return -1 if curly braces expansion failed.
6387 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 * If the name contains 'magic' {}'s, expand them and return the
6389 * expanded name in an allocated string via 'alias' - caller must free.
6390 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006392get_name_len(
6393 char_u **arg,
6394 char_u **alias,
6395 int evaluate,
6396 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 char_u *p;
6400 char_u *expr_start;
6401 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006403 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6406 && (*arg)[2] == (int)KE_SNR)
6407 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 *arg += 3;
6410 return get_id_len(arg) + 3;
6411 }
6412 len = eval_fname_script(*arg);
6413 if (len > 0)
6414 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006415 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 *arg += len;
6417 }
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006422 p = find_name_end(*arg, &expr_start, &expr_end,
6423 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (expr_start != NULL)
6425 {
6426 char_u *temp_string;
6427
6428 if (!evaluate)
6429 {
6430 len += (int)(p - *arg);
6431 *arg = skipwhite(p);
6432 return len;
6433 }
6434
6435 /*
6436 * Include any <SID> etc in the expanded string:
6437 * Thus the -len here.
6438 */
6439 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6440 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006441 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 *alias = temp_string;
6443 *arg = skipwhite(p);
6444 return (int)STRLEN(temp_string);
6445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006448 // Only give an error when there is something, otherwise it will be
6449 // reported at a higher level.
6450 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006451 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 return len;
6454}
6455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456/*
6457 * Find the end of a variable or function name, taking care of magic braces.
6458 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6459 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006460 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006461 * Return a pointer to just after the name. Equal to "arg" if there is no
6462 * valid name.
6463 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006465find_name_end(
6466 char_u *arg,
6467 char_u **expr_start,
6468 char_u **expr_end,
6469 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006471 int mb_nest = 0;
6472 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006474 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006475 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006477 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006479 *expr_start = NULL;
6480 *expr_end = NULL;
6481 }
6482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006484 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6485 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006486 return arg;
6487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006488 for (p = arg; *p != NUL
6489 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006490 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006491 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006492 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006493 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006494 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006495 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006496 if (*p == '\'')
6497 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006498 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006499 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006500 ;
6501 if (*p == NUL)
6502 break;
6503 }
6504 else if (*p == '"')
6505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006507 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006508 if (*p == '\\' && p[1] != NUL)
6509 ++p;
6510 if (*p == NUL)
6511 break;
6512 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006513 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6514 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 // "s:" is start of "s:var", but "n:" is not and can be used in
6516 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006517 len = (int)(p - arg);
6518 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006519 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006520 break;
6521 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 if (*p == '[')
6526 ++br_nest;
6527 else if (*p == ']')
6528 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006530
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006531 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 if (*p == '{')
6534 {
6535 mb_nest++;
6536 if (expr_start != NULL && *expr_start == NULL)
6537 *expr_start = p;
6538 }
6539 else if (*p == '}')
6540 {
6541 mb_nest--;
6542 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6543 *expr_end = p;
6544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547
6548 return p;
6549}
6550
6551/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006552 * Expands out the 'magic' {}'s in a variable/function name.
6553 * Note that this can call itself recursively, to deal with
6554 * constructs like foo{bar}{baz}{bam}
6555 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6556 * "in_start" ^
6557 * "expr_start" ^
6558 * "expr_end" ^
6559 * "in_end" ^
6560 *
6561 * Returns a new allocated string, which the caller must free.
6562 * Returns NULL for failure.
6563 */
6564 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565make_expanded_name(
6566 char_u *in_start,
6567 char_u *expr_start,
6568 char_u *expr_end,
6569 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006570{
6571 char_u c1;
6572 char_u *retval = NULL;
6573 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006574
6575 if (expr_end == NULL || in_end == NULL)
6576 return NULL;
6577 *expr_start = NUL;
6578 *expr_end = NUL;
6579 c1 = *in_end;
6580 *in_end = NUL;
6581
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006582 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006583 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006584 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006585 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6586 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006587 if (retval != NULL)
6588 {
6589 STRCPY(retval, in_start);
6590 STRCAT(retval, temp_result);
6591 STRCAT(retval, expr_end + 1);
6592 }
6593 }
6594 vim_free(temp_result);
6595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006596 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006597 *expr_start = '{';
6598 *expr_end = '}';
6599
6600 if (retval != NULL)
6601 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006602 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006603 if (expr_start != NULL)
6604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006605 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006606 temp_result = make_expanded_name(retval, expr_start,
6607 expr_end, temp_result);
6608 vim_free(retval);
6609 retval = temp_result;
6610 }
6611 }
6612
6613 return retval;
6614}
6615
6616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006618 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006623 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006624}
6625
6626/*
6627 * Return TRUE if character "c" can be used as the first character in a
6628 * variable or function name (excluding '{' and '}').
6629 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006632{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006633 return ASCII_ISALPHA(c) || c == '_';
6634}
6635
6636/*
6637 * Return TRUE if character "c" can be used as the first character of a
6638 * dictionary key.
6639 */
6640 int
6641eval_isdictc(int c)
6642{
6643 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006647 * Handle:
6648 * - expr[expr], expr[expr:expr] subscript
6649 * - ".name" lookup
6650 * - function call with Funcref variable: func(expr)
6651 * - method call: var->method()
6652 *
6653 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006654 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006655 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006657handle_subscript(
6658 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006659 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006661 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006662 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006663{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006664 int evaluate = evalarg != NULL
6665 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006666 int ret = OK;
6667 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006668 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006669 int getnext;
6670 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006671
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006672 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006673 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006674 // When at the end of the line and ".name" or "->{" or "->X" follows in
6675 // the next line then consume the line break.
6676 p = eval_next_non_blank(*arg, evalarg, &getnext);
6677 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006678 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006679 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6680 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6681 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006682 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006683 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006684 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006685 check_white = FALSE;
6686 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006687
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006688 if (rettv->v_type == VAR_ANY)
6689 {
6690 char_u *exp_name;
6691 int cc;
6692 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006693 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006694 type_T *type;
6695
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006696 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006697 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006698 if (**arg != '.')
6699 {
6700 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006701 semsg(_(e_expected_dot_after_name_str),
6702 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006703 ret = FAIL;
6704 break;
6705 }
6706 ++*arg;
6707 if (IS_WHITE_OR_NUL(**arg))
6708 {
6709 if (verbose)
6710 emsg(_(e_no_white_space_allowed_after_dot));
6711 ret = FAIL;
6712 break;
6713 }
6714
6715 // isolate the name
6716 exp_name = *arg;
6717 while (eval_isnamec(**arg))
6718 ++*arg;
6719 cc = **arg;
6720 **arg = NUL;
6721
6722 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006723 evalarg == NULL ? NULL : evalarg->eval_cctx,
6724 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006725 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006726
6727 if (idx < 0 && ufunc == NULL)
6728 {
6729 ret = FAIL;
6730 break;
6731 }
6732 if (idx >= 0)
6733 {
6734 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6735 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6736
6737 copy_tv(sv->sv_tv, rettv);
6738 }
6739 else
6740 {
6741 rettv->v_type = VAR_FUNC;
6742 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6743 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006744 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006745 }
6746
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006747 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6748 || rettv->v_type == VAR_PARTIAL))
6749 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006750 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006751 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6752 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006753
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006754 // Stop the expression evaluation when immediately aborting on
6755 // error, or when an interrupt occurred or an exception was thrown
6756 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006757 if (aborting())
6758 {
6759 if (ret == OK)
6760 clear_tv(rettv);
6761 ret = FAIL;
6762 }
6763 dict_unref(selfdict);
6764 selfdict = NULL;
6765 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006766 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006767 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006768 if (in_vim9script())
6769 *arg = skipwhite(p + 2);
6770 else
6771 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006772 if (ret == OK)
6773 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006774 if (VIM_ISWHITE(**arg))
6775 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006776 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006777 ret = FAIL;
6778 }
6779 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006780 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006781 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006782 else
6783 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006784 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006785 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006786 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006787 // "." is ".name" lookup when we found a dict or when evaluating and
6788 // scriptversion is at least 2, where string concatenation is "..".
6789 else if (**arg == '['
6790 || (**arg == '.' && (rettv->v_type == VAR_DICT
6791 || (!evaluate
6792 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006793 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006794 {
6795 dict_unref(selfdict);
6796 if (rettv->v_type == VAR_DICT)
6797 {
6798 selfdict = rettv->vval.v_dict;
6799 if (selfdict != NULL)
6800 ++selfdict->dv_refcount;
6801 }
6802 else
6803 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006804 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006805 {
6806 clear_tv(rettv);
6807 ret = FAIL;
6808 }
6809 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006810 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6811 || rettv->v_type == VAR_OBJECT))
6812 {
6813 // class member: SomeClass.varname
6814 // class method: SomeClass.SomeMethod()
6815 // class constructor: SomeClass.new()
6816 // object member: someObject.varname
6817 // object method: someObject.SomeMethod()
6818 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6819 {
6820 clear_tv(rettv);
6821 ret = FAIL;
6822 }
6823 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006824 else
6825 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006826 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006828 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6829 // Don't do this when "Func" is already a partial that was bound
6830 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006831 if (selfdict != NULL
6832 && (rettv->v_type == VAR_FUNC
6833 || (rettv->v_type == VAR_PARTIAL
6834 && (rettv->vval.v_partial->pt_auto
6835 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006838 dict_unref(selfdict);
6839 return ret;
6840}
6841
6842/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 * Make a copy of an item.
6844 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006845 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006846 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6847 * reference to an already copied list/dict can be used.
6848 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006851item_copy(
6852 typval_T *from,
6853 typval_T *to,
6854 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006855 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857{
6858 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006859 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860
Bram Moolenaar33570922005-01-25 22:26:29 +00006861 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006862 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006863 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006864 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 }
6866 ++recurse;
6867
6868 switch (from->v_type)
6869 {
6870 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006871 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 case VAR_STRING:
6873 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006874 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006875 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006876 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006877 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006878 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006879 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006880 case VAR_CLASS:
6881 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882 copy_tv(from, to);
6883 break;
6884 case VAR_LIST:
6885 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006886 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006887 if (from->vval.v_list == NULL)
6888 to->vval.v_list = NULL;
6889 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006891 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006892 to->vval.v_list = from->vval.v_list->lv_copylist;
6893 ++to->vval.v_list->lv_refcount;
6894 }
6895 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006896 to->vval.v_list = list_copy(from->vval.v_list,
6897 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006898 if (to->vval.v_list == NULL)
6899 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006901 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006903 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006904 case VAR_DICT:
6905 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006906 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006907 if (from->vval.v_dict == NULL)
6908 to->vval.v_dict = NULL;
6909 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006911 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006912 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6913 ++to->vval.v_dict->dv_refcount;
6914 }
6915 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006916 to->vval.v_dict = dict_copy(from->vval.v_dict,
6917 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006918 if (to->vval.v_dict == NULL)
6919 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006920 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006921 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006922 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006924 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006925 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926 }
6927 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006928 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929}
6930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006931 void
6932echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6933{
6934 char_u *tofree;
6935 char_u numbuf[NUMBUFLEN];
6936 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6937
6938 if (*atstart)
6939 {
6940 *atstart = FALSE;
6941 // Call msg_start() after eval1(), evaluating the expression
6942 // may cause a message to appear.
6943 if (with_space)
6944 {
6945 // Mark the saved text as finishing the line, so that what
6946 // follows is displayed on a new line when scrolling back
6947 // at the more prompt.
6948 msg_sb_eol();
6949 msg_start();
6950 }
6951 }
6952 else if (with_space)
6953 msg_puts_attr(" ", echo_attr);
6954
6955 if (p != NULL)
6956 for ( ; *p != NUL && !got_int; ++p)
6957 {
6958 if (*p == '\n' || *p == '\r' || *p == TAB)
6959 {
6960 if (*p != TAB && *needclr)
6961 {
6962 // remove any text still there from the command
6963 msg_clr_eos();
6964 *needclr = FALSE;
6965 }
6966 msg_putchar_attr(*p, echo_attr);
6967 }
6968 else
6969 {
6970 if (has_mbyte)
6971 {
6972 int i = (*mb_ptr2len)(p);
6973
6974 (void)msg_outtrans_len_attr(p, i, echo_attr);
6975 p += i - 1;
6976 }
6977 else
6978 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6979 }
6980 }
6981 vim_free(tofree);
6982}
6983
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 * ":echo expr1 ..." print each argument separated with a space, add a
6986 * newline at the end.
6987 * ":echon expr1 ..." print each argument plain.
6988 */
6989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006990ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006994 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 int needclr = TRUE;
6996 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006997 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006998 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006999 evalarg_T evalarg;
7000
Bram Moolenaare707c882020-07-01 13:07:37 +02007001 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 if (eap->skip)
7004 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02007005 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007007 // If eval1() causes an error message the text from the command may
7008 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007009 need_clr_eos = needclr;
7010
Bram Moolenaar68db9962021-05-09 23:19:22 +02007011 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02007012 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
7014 /*
7015 * Report the invalid expression unless the expression evaluation
7016 * has been cancelled due to an aborting error, an interrupt, or an
7017 * exception.
7018 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01007019 if (!aborting() && did_emsg == did_emsg_before
7020 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02007021 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007022 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 break;
7024 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007025 need_clr_eos = FALSE;
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007028 {
7029 if (rettv.v_type == VAR_VOID)
7030 {
7031 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7032 break;
7033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007035 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007036
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007037 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 arg = skipwhite(arg);
7039 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007040 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007041 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
7043 if (eap->skip)
7044 --emsg_skip;
7045 else
7046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007047 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (needclr)
7049 msg_clr_eos();
7050 if (eap->cmdidx == CMD_echo)
7051 msg_end();
7052 }
7053}
7054
7055/*
7056 * ":echohl {name}".
7057 */
7058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007061 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
7064/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007065 * Returns the :echo attribute
7066 */
7067 int
7068get_echo_attr(void)
7069{
7070 return echo_attr;
7071}
7072
7073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 * ":execute expr1 ..." execute the result of an expression.
7075 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007076 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007078 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 * Each gets spaces around each argument and a newline at the end for
7080 * echo commands
7081 */
7082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007083ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 int ret = OK;
7088 char_u *p;
7089 garray_T ga;
7090 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007091 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093 ga_init2(&ga, 1, 80);
7094
7095 if (eap->skip)
7096 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007097 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007099 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007100 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
7103 if (!eap->skip)
7104 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007105 char_u buf[NUMBUFLEN];
7106
7107 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007108 {
7109 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7110 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007111 semsg(_(e_using_invalid_value_as_string_str),
7112 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007113 p = NULL;
7114 }
7115 else
7116 p = tv_get_string_buf(&rettv, buf);
7117 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007118 else
7119 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007120 if (p == NULL)
7121 {
7122 clear_tv(&rettv);
7123 ret = FAIL;
7124 break;
7125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 len = (int)STRLEN(p);
7127 if (ga_grow(&ga, len + 2) == FAIL)
7128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007129 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 ret = FAIL;
7131 break;
7132 }
7133 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 ga.ga_len += len;
7137 }
7138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007139 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 arg = skipwhite(arg);
7141 }
7142
7143 if (ret != FAIL && ga.ga_data != NULL)
7144 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007145 // use the first line of continuation lines for messages
7146 SOURCING_LNUM = start_lnum;
7147
Bram Moolenaar37fef162022-08-29 18:16:32 +01007148 if (eap->cmdidx == CMD_echomsg
7149 || eap->cmdidx == CMD_echowindow
7150 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007152 // Mark the already saved text as finishing the line, so that what
7153 // follows is displayed on a new line when scrolling back at the
7154 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007155 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007156 }
7157
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007158 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007159 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007160 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007161 out_flush();
7162 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007163 else if (eap->cmdidx == CMD_echowindow)
7164 {
7165#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007166 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007167#endif
7168 msg_attr(ga.ga_data, echo_attr);
7169#ifdef HAS_MESSAGE_WINDOW
7170 end_echowindow();
7171#endif
7172 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007173 else if (eap->cmdidx == CMD_echoconsole)
7174 {
7175 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7176 ui_write((char_u *)"\r\n", 2, TRUE);
7177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 else if (eap->cmdidx == CMD_echoerr)
7179 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007180 int save_did_emsg = did_emsg;
7181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007182 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007183 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (!force_abort)
7185 did_emsg = save_did_emsg;
7186 }
7187 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007188 {
7189 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7190
7191 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7192 sticky_cmdmod_flags = cmdmod.cmod_flags
7193 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 do_cmdline((char_u *)ga.ga_data,
7195 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007196 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199
7200 ga_clear(&ga);
7201
7202 if (eap->skip)
7203 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007204 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205}
7206
7207/*
7208 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7209 * "arg" points to the "&" or '+' when called, to "option" when returning.
7210 * Returns NULL when no option name found. Otherwise pointer to the char
7211 * after the option name.
7212 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007213 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007214find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 char_u *p = *arg;
7217
7218 ++p;
7219 if (*p == 'g' && p[1] == ':')
7220 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007221 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 p += 2;
7223 }
7224 else if (*p == 'l' && p[1] == ':')
7225 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007226 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 p += 2;
7228 }
7229 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007230 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 if (!ASCII_ISALPHA(*p))
7233 return NULL;
7234 *arg = p;
7235
7236 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007237 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 else
7239 while (ASCII_ISALPHA(*p))
7240 ++p;
7241 return p;
7242}
7243
7244/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007245 * Display script name where an item was last set.
7246 * Should only be invoked when 'verbose' is non-zero.
7247 */
7248 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007249last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007250{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007251 char_u *p;
7252
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007253 if (script_ctx.sc_sid == 0)
7254 return;
7255
7256 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
7257 if (p == NULL)
7258 return;
7259
7260 verbose_enter();
7261 msg_puts(_("\n\tLast set from "));
7262 msg_puts((char *)p);
7263 if (script_ctx.sc_lnum > 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007264 {
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007265 msg_puts(_(line_msg));
7266 msg_outnum((long)script_ctx.sc_lnum);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007267 }
Yegappan Lakshmanan87c1cbb2022-12-27 19:54:52 +00007268 verbose_leave();
7269 vim_free(p);
Bram Moolenaar661b1822005-07-28 22:36:45 +00007270}
7271
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007272#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
7274/*
7275 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007276 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 * "flags" can be "g" to do a global substitute.
7278 * Returns an allocated string, NULL for error.
7279 */
7280 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281do_string_sub(
7282 char_u *str,
7283 char_u *pat,
7284 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007285 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007286 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288 int sublen;
7289 regmatch_T regmatch;
7290 int i;
7291 int do_all;
7292 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007293 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 garray_T ga;
7295 char_u *ret;
7296 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007297 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007299 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007301 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
7303 ga_init2(&ga, 1, 200);
7304
7305 do_all = (flags[0] == 'g');
7306
7307 regmatch.rm_ic = p_ic;
7308 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7309 if (regmatch.regprog != NULL)
7310 {
7311 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007312 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007315 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007316 if (regmatch.startp[0] == regmatch.endp[0])
7317 {
7318 if (zero_width == regmatch.startp[0])
7319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007320 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007321 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007322 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7323 (size_t)i);
7324 ga.ga_len += i;
7325 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007326 continue;
7327 }
7328 zero_width = regmatch.startp[0];
7329 }
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /*
7332 * Get some space for a temporary buffer to do the substitution
7333 * into. It will contain:
7334 * - The text up to where the match is.
7335 * - The substituted text.
7336 * - The text after the match.
7337 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007338 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaar3ac1d972023-01-04 17:17:54 +00007339 if (sublen <= 0)
7340 {
7341 ga_clear(&ga);
7342 break;
7343 }
Bram Moolenaare90c8532014-11-05 16:03:44 +01007344 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7346 {
7347 ga_clear(&ga);
7348 break;
7349 }
7350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007351 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 i = (int)(regmatch.startp[0] - tail);
7353 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007354 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007355 (void)vim_regsub(&regmatch, sub, expr,
7356 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7357 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007359 tail = regmatch.endp[0];
7360 if (*tail == NUL)
7361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (!do_all)
7363 break;
7364 }
7365
7366 if (ga.ga_data != NULL)
7367 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7368
Bram Moolenaar473de612013-06-08 18:19:48 +02007369 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 }
7371
7372 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7373 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007374 if (p_cpo == empty_option)
7375 p_cpo = save_cpo;
7376 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007377 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007378 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007379 // If it's still empty it was changed and restored, need to restore in
7380 // the complicated way.
7381 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007382 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007383 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 return ret;
7387}