blob: 342f93ca2e3e10cef06535d86f27df01b73f21e4 [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 }
69 else
70 result = n1 / n2;
71
72 return result;
73}
74
75/*
76 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010077 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020079 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010080num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010081{
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010084 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085 if (failed != NULL)
86 *failed = TRUE;
87 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010088 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaar33570922005-01-25 22:26:29 +000091/*
92 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000093 */
94 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010095eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020097 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020098 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000099}
100
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000101#if defined(EXITFREE) || defined(PROTO)
102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100103eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000104{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200105 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100106 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200107 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000108
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200109 // autoloaded script names
110 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000111
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200112 // unreferenced lists and dicts
113 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200114
115 // functions not garbage collected
116 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117}
118#endif
119
Bram Moolenaare6b53242020-07-01 17:28:33 +0200120 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200121fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
122{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100123 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200124 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200125 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200126 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200127 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100128 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200129 {
130 evalarg->eval_getline = eap->getline;
131 evalarg->eval_cookie = eap->cookie;
132 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200133 }
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Top level evaluation function, returning a boolean.
138 * Sets "error" to TRUE if there was an error.
139 * Return TRUE or FALSE.
140 */
141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_to_bool(
143 char_u *arg,
144 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200145 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100146 int skip, // only parse, don't execute
147 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148{
Bram Moolenaar33570922005-01-25 22:26:29 +0000149 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200150 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200151 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100152 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200153
Bram Moolenaar37c83712020-06-30 21:18:36 +0200154 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156 if (skip)
157 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100158 if (use_simple_function)
159 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
160 else
161 r = eval0(arg, &tv, eap, &evalarg);
162 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 else
165 {
166 *error = FALSE;
167 if (!skip)
168 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200169 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200170 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200171 else
172 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000173 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 }
175 }
176 if (skip)
177 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200180 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181}
182
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100183/*
184 * Call eval1() and give an error message if not done at a lower level.
185 */
186 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200187eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100188{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100189 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100190 int ret;
191 int did_emsg_before = did_emsg;
192 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200193 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100194
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200195 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
196
197 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100198 if (ret == FAIL)
199 {
200 // Report the invalid expression unless the expression evaluation has
201 // been cancelled due to an aborting error, an interrupt, or an
202 // exception, or we already gave a more specific error.
203 // Also check called_emsg for when using assert_fails().
204 if (!aborting() && did_emsg == did_emsg_before
205 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200206 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200208 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100209 return ret;
210}
211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100212/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200213 * Return whether a typval is a valid expression to pass to eval_expr_typval()
214 * or eval_expr_to_bool(). An empty string returns FALSE;
215 */
216 int
217eval_expr_valid_arg(typval_T *tv)
218{
219 return tv->v_type != VAR_UNKNOWN
220 && (tv->v_type != VAR_STRING
221 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
222}
223
224/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100225 * When calling eval_expr_typval() many times we only need one funccall_T.
226 * Returns NULL when no funccall_T is to be used.
227 * When returning non-NULL remove_funccal() must be called later.
228 */
229 funccall_T *
230eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
231{
232 if (expr->v_type != VAR_PARTIAL)
233 return NULL;
234
235 partial_T *partial = expr->vval.v_partial;
236 if (partial == NULL)
237 return NULL;
238 if (partial->pt_func == NULL
239 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
240 return NULL;
241
242 return create_funccal(partial->pt_func, rettv);
243}
244
245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 * Evaluate an expression, which can be a function, partial or string.
247 * Pass arguments "argv[argc]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100248 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Return the result in "rettv" and OK or FAIL.
250 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200251 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100252eval_expr_typval(
253 typval_T *expr,
254 typval_T *argv,
255 int argc,
256 funccall_T *fc_arg,
257 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100258{
259 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200261 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100262
263 if (expr->v_type == VAR_FUNC)
264 {
265 s = expr->vval.v_string;
266 if (s == NULL || *s == NUL)
267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000269 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200270 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 return FAIL;
272 }
273 else if (expr->v_type == VAR_PARTIAL)
274 {
275 partial_T *partial = expr->vval.v_partial;
276
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200277 if (partial == NULL)
278 return FAIL;
279
Bram Moolenaar822ba242020-05-24 23:00:18 +0200280 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200281 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100283 funccall_T *fc = fc_arg != NULL ? fc_arg
284 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100285 int r;
286
287 if (fc == NULL)
288 return FAIL;
289
Bram Moolenaar69082912022-09-22 21:35:19 +0100290 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100291 r = call_def_function(partial->pt_func, argc, argv,
292 DEF_USE_PT_ARGV, partial, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100293 if (fc_arg == NULL)
294 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100295 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 return FAIL;
297 }
298 else
299 {
300 s = partial_name(partial);
301 if (s == NULL || *s == NUL)
302 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200303 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000304 funcexe.fe_evaluate = TRUE;
305 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
307 return FAIL;
308 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100309 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200310 else if (expr->v_type == VAR_INSTR)
311 {
312 return exe_typval_instr(expr, rettv);
313 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100314 else
315 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000316 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100317 if (s == NULL)
318 return FAIL;
319 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200320 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200322 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200324 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200325 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 return FAIL;
327 }
328 }
329 return OK;
330}
331
332/*
333 * Like eval_to_bool() but using a typval_T instead of a string.
334 * Works for string, funcref and partial.
335 */
336 int
337eval_expr_to_bool(typval_T *expr, int *error)
338{
339 typval_T rettv;
340 int res;
341
Bram Moolenaar82418262022-09-28 16:16:15 +0100342 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100343 {
344 *error = TRUE;
345 return FALSE;
346 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200347 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100348 clear_tv(&rettv);
349 return res;
350}
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352/*
353 * Top level evaluation function, returning a string. If "skip" is TRUE,
354 * only parsing to "nextcmd" is done, without reporting errors. Return
355 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
356 */
357 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100358eval_to_string_skip(
359 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200360 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100361 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362{
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200365 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaar37c83712020-06-30 21:18:36 +0200367 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 if (skip)
369 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200370 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 retval = NULL;
372 else
373 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100374 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000375 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377 if (skip)
378 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200379 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
381 return retval;
382}
383
384/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100385 * Initialize "evalarg" for use.
386 */
387 void
388init_evalarg(evalarg_T *evalarg)
389{
390 CLEAR_POINTER(evalarg);
391 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
392}
393
394/*
395 * If "evalarg->eval_tofree" is not NULL free it later.
396 * Caller is expected to overwrite "evalarg->eval_tofree" next.
397 */
398 static void
399free_eval_tofree_later(evalarg_T *evalarg)
400{
401 if (evalarg->eval_tofree != NULL)
402 {
403 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
404 ((char_u **)evalarg->eval_tofree_ga.ga_data)
405 [evalarg->eval_tofree_ga.ga_len++]
406 = evalarg->eval_tofree;
407 else
408 vim_free(evalarg->eval_tofree);
409 }
410}
411
412/*
413 * After using "evalarg" filled from "eap": free the memory.
414 */
415 void
416clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
417{
418 if (evalarg != NULL)
419 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100420 garray_T *etga = &evalarg->eval_tofree_ga;
421
422 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100423 {
424 if (eap != NULL)
425 {
426 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100427 // ":let" it has the variable names. But we may also need
428 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100429 vim_free(eap->cmdline_tofree);
430 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100431
432 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
433 {
434 // "nextcmd" points into the last line in eval_tofree_ga,
435 // need to keep it around.
436 --etga->ga_len;
437 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
Bram Moolenaar86fb3f82022-09-23 13:27:57 +0100438 vim_free(evalarg->eval_tofree);
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100439 }
440 else
441 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100442 }
443 else
444 vim_free(evalarg->eval_tofree);
445 evalarg->eval_tofree = NULL;
446 }
447
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100448 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100449 VIM_CLEAR(evalarg->eval_tofree_lambda);
450 }
451}
452
453/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000454 * Skip over an expression at "*pp".
455 * Return FAIL for an error, OK otherwise.
456 */
457 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200458skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000459{
Bram Moolenaar33570922005-01-25 22:26:29 +0000460 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000461
462 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200463 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000464}
465
466/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200467 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 * If in Vim9 script and line breaks are encountered, the lines are
469 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200470 * "arg" is advanced to just after the expression.
471 * "start" is set to the start of the expression, "end" to just after the end.
472 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200473 * Return FAIL for an error, OK otherwise.
474 */
475 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200476skip_expr_concatenate(
477 char_u **arg,
478 char_u **start,
479 char_u **end,
480 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200481{
482 typval_T rettv;
483 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200484 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200485 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200486 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200487 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200488 int evaluate = evalarg == NULL
489 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200490
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200491 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200492 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200493 {
494 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200495 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200496 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200497 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200498 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200499 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200500 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200501
502 // Don't evaluate the expression.
503 if (evalarg != NULL)
504 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200505 *arg = skipwhite(*arg);
506 res = eval1(arg, &rettv, evalarg);
507 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200508 if (evalarg != NULL)
509 evalarg->eval_flags = save_flags;
510
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200511 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200512 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200513 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200514 if (evalarg->eval_ga.ga_len == 1)
515 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200516 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200517 ga_clear(gap);
518 gap->ga_itemsize = 0;
519 }
520 else
521 {
522 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200523 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200525 // Line breaks encountered, concatenate all the lines.
526 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200527 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200528
529 // free the lines only when using getsourceline()
530 if (evalarg->eval_cookie != NULL)
531 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200532 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200533 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200534 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100535 // later. Also free "eval_tofree" later if needed.
536 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200537 evalarg->eval_tofree =
538 ((char_u **)gap->ga_data)[gap->ga_len - 1];
539 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200540 ga_clear_strings(gap);
541 }
542 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200543 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200545
546 // free lines that were explicitly marked for freeing
547 ga_clear_strings(freegap);
548 }
549
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200550 gap->ga_itemsize = 0;
551 if (p == NULL)
552 return FAIL;
553 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200554 vim_free(evalarg->eval_tofree_lambda);
555 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 // Compute "end" relative to the end.
557 *end = *start + STRLEN(*start) - endoff;
558 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200559 }
560
561 return res;
562}
563
564/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100565 * Convert "tv" to a string.
566 * When "convert" is TRUE convert a List into a sequence of lines and convert
567 * a Float to a String.
568 * Returns an allocated string (NULL when out of memory).
569 */
570 char_u *
571typval2string(typval_T *tv, int convert)
572{
573 garray_T ga;
574 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100575 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100576
577 if (convert && tv->v_type == VAR_LIST)
578 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000579 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100580 if (tv->vval.v_list != NULL)
581 {
582 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
583 if (tv->vval.v_list->lv_len > 0)
584 ga_append(&ga, NL);
585 }
586 ga_append(&ga, NUL);
587 retval = (char_u *)ga.ga_data;
588 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100589 else if (convert && tv->v_type == VAR_FLOAT)
590 {
591 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
592 retval = vim_strsave(numbuf);
593 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100594 else
595 retval = vim_strsave(tv_get_string(tv));
596 return retval;
597}
598
599/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200600 * Top level evaluation function, returning a string. Does not handle line
601 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000602 * When "convert" is TRUE convert a List into a sequence of lines and convert
603 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * Return pointer to allocated memory, or NULL for failure.
605 */
606 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100607eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100609 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100610 exarg_T *eap,
611 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
Bram Moolenaar33570922005-01-25 22:26:29 +0000613 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100615 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100616 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100619 if (use_simple_function)
620 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
621 else
622 r = eval0(arg, &tv, NULL, &evalarg);
623 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 retval = NULL;
625 else
626 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100627 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000628 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100630 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 return retval;
633}
634
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100635 char_u *
636eval_to_string(
637 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100638 int convert,
639 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100640{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100641 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100642}
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000645 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100646 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200647 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 */
649 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100650eval_to_string_safe(
651 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000652 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100653 int keep_script_version,
654 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200657 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200658 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200659 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
Bram Moolenaar9530b582022-01-22 13:39:08 +0000661 if (!keep_script_version)
662 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200663 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000664 if (use_sandbox)
665 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100666 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200667 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100668 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 if (use_sandbox)
670 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100671 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200672 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200673 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200674 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return retval;
676}
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678/*
679 * Top level evaluation function, returning a number.
680 * Evaluates "expr" silently.
681 * Returns -1 for an error.
682 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200683 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100684eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
Bram Moolenaar33570922005-01-25 22:26:29 +0000686 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200687 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000688 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100689 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 ++emsg_off;
692
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100693 if (use_simple_function)
694 r = may_call_simple_func(expr, &rettv);
695 if (r == NOTDONE)
696 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
697 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 retval = -1;
699 else
700 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100701 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000702 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704 --emsg_off;
705
706 return retval;
707}
708
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000709/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000710 * Top level evaluation function.
711 * Returns an allocated typval_T with the result.
712 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000713 */
714 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200715eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100717 return eval_expr_ext(arg, eap, FALSE);
718}
719
720 typval_T *
721eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
722{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200724 evalarg_T evalarg;
725
726 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000727
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200728 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100729 if (tv != NULL)
730 {
731 int r = NOTDONE;
732
733 if (use_simple_function)
734 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
735 if (r == NOTDONE)
736 r = eval0(arg, tv, eap, &evalarg);
737
738 if (r == FAIL)
739 VIM_CLEAR(tv);
740 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000741
Bram Moolenaar37c83712020-06-30 21:18:36 +0200742 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000743 return tv;
744}
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000747 * "*arg" points to what can be a function name in the form of "import.Name" or
748 * "Funcref". Return the name of the function. Set "tofree" to something that
749 * was allocated.
750 * If "verbose" is FALSE no errors are given.
751 * Return NULL for any failure.
752 */
753 static char_u *
754deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000755 char_u **arg,
756 char_u **tofree,
757 evalarg_T *evalarg,
758 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000759{
760 typval_T ref;
761 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100762 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000763
764 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100765 if (evalarg != NULL)
766 {
767 // need to evaluate this to get an import, like in "a.Func"
768 save_flags = evalarg->eval_flags;
769 evalarg->eval_flags |= EVAL_EVALUATE;
770 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100771 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100772 {
773 dictitem_T *v;
774
775 // If <SID>VarName was used it would not be found, try another way.
776 v = find_var_also_in_script(name, NULL, FALSE);
777 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100778 {
779 name = NULL;
780 goto theend;
781 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100782 copy_tv(&v->di_tv, &ref);
783 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000784 if (*skipwhite(*arg) != NUL)
785 {
786 if (verbose)
787 semsg(_(e_trailing_characters_str), *arg);
788 name = NULL;
789 }
790 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
791 {
792 name = ref.vval.v_string;
793 ref.vval.v_string = NULL;
794 *tofree = name;
795 }
796 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
797 {
798 if (ref.vval.v_partial->pt_argc > 0
799 || ref.vval.v_partial->pt_dict != NULL)
800 {
801 if (verbose)
802 emsg(_(e_cannot_use_partial_here));
803 name = NULL;
804 }
805 else
806 {
807 name = vim_strsave(partial_name(ref.vval.v_partial));
808 *tofree = name;
809 }
810 }
811 else
812 {
813 if (verbose)
814 semsg(_(e_not_callable_type_str), name);
815 name = NULL;
816 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100817
818theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000819 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100820 if (evalarg != NULL)
821 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000822 return name;
823}
824
825/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100826 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200827 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
828 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000829 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100832call_vim_function(
833 char_u *func,
834 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200835 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200836 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000838 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200839 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000840 char_u *arg;
841 char_u *name;
842 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000843 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200846 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000847 funcexe.fe_firstline = curwin->w_cursor.lnum;
848 funcexe.fe_lastline = curwin->w_cursor.lnum;
849 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000850
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000851 // The name might be "import.Func" or "Funcref". We don't know, we need to
852 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000853 // autoload script has errors. Guess that when there is a dot in the name
854 // showing errors is the right choice.
855 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000856 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000857 if (ignore_errors)
858 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000859 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000860 if (ignore_errors)
861 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 if (name == NULL)
863 name = func;
864
865 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
866
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000867 if (ret == FAIL)
868 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000869 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000870
871 return ret;
872}
873
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100874/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100875 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000876 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
877 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000878 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000879 */
880 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881call_func_retstr(
882 char_u *func,
883 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200884 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000885{
886 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000887 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000888
Bram Moolenaarded27a12018-08-01 19:06:03 +0200889 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000890 return NULL;
891
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100892 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return retval;
895}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000897/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100898 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000899 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000900 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100901 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000902 */
903 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100904call_func_retlist(
905 char_u *func,
906 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200907 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000908{
909 typval_T rettv;
910
Bram Moolenaarded27a12018-08-01 19:06:03 +0200911 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000912 return NULL;
913
914 if (rettv.v_type != VAR_LIST)
915 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100916 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
917 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000918 clear_tv(&rettv);
919 return NULL;
920 }
921
922 return rettv.vval.v_list;
923}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaare70dd112022-01-21 16:31:11 +0000925#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100927 * Evaluate "arg", which is 'foldexpr'.
928 * Note: caller must set "curwin" to match "arg".
929 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
930 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 */
932 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000933eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000935 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200937 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000939 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000940 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100941 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100943 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000944 current_sctx = wp->w_p_script_ctx[WV_FDE];
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000947 if (use_sandbox)
948 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100949 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100951
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100952 // Evaluate the expression. If the expression is "FuncName()" call the
953 // function directly.
954 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 retval = 0;
956 else
957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100958 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000959 if (tv.v_type == VAR_NUMBER)
960 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000961 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
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 string, check if there is a non-digit before
966 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000967 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (!VIM_ISDIGIT(*s) && *s != '-')
969 *cp = *s++;
970 retval = atol((char *)s);
971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000972 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000975 if (use_sandbox)
976 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100977 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200978 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000979 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200981 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983#endif
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 * Get an lval: variable, Dict item or List item that can be assigned a value
987 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
988 * "name.key", "name.key[expr]" etc.
989 * Indexing only works if "name" is an existing List or Dictionary.
990 * "name" points to the start of the name.
991 * If "rettv" is not NULL it points to the value to be assigned.
992 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
993 * wrong; must end in space or cmd separator.
994 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100995 * flags:
996 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100997 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100998 * GLV_NO_AUTOLOAD: do not use script autoloading
999 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001001 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001003 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001004 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005get_lval(
1006 char_u *name,
1007 typval_T *rettv,
1008 lval_T *lp,
1009 int unlet,
1010 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001011 int flags, // GLV_ values
1012 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 char_u *expr_start, *expr_end;
1016 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001017 dictitem_T *v;
1018 typval_T var1;
1019 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001023 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001024 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001025 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001026 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001028 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001029 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001031 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001033 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001035 lp->ll_name_end = find_name_end(name, NULL, NULL,
1036 FNE_INCL_BR | fne_flags);
1037 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 }
1039
Bram Moolenaara749a422022-02-12 19:52:25 +00001040 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001041 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001042 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1043 {
1044 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1045 return NULL;
1046 }
1047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001048 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001049 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 if (expr_start != NULL)
1052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001053 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001054 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 && *p != '[' && *p != '.')
1056 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001057 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
1059 }
1060
1061 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1062 if (lp->ll_exp_name == NULL)
1063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // Report an invalid expression in braces, unless the
1065 // expression evaluation has been cancelled due to an
1066 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (!aborting() && !quiet)
1068 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001069 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001070 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 return NULL;
1072 }
1073 }
1074 lp->ll_name = lp->ll_exp_name;
1075 }
1076 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 lp->ll_name = name;
1079
Bram Moolenaar4525a572022-02-13 11:57:33 +00001080 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001082 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001083 // However, "g:[key]" is indexing a dictionary.
1084 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001085 {
1086 --p;
1087 lp->ll_name_end = p;
1088 }
1089 if (*p == ':')
1090 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001091 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001092
Bram Moolenaar9510d222022-09-11 15:14:05 +01001093 if (is_scoped_variable(name))
1094 {
1095 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1096 return NULL;
1097 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001098 if (tp == p + 1 && !quiet)
1099 {
1100 semsg(_(e_white_space_required_after_str_str), ":", p);
1101 return NULL;
1102 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001103 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001104 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001105 semsg(_(e_using_type_not_in_script_context_str), p);
1106 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001107 }
1108
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001109 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001110 lp->ll_type = parse_type(&tp,
1111 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1112 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001113 if (lp->ll_type == NULL && !quiet)
1114 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001115 lp->ll_name_end = tp;
1116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 }
1118 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001119 if (lp->ll_name == NULL)
1120 return p;
1121
Bram Moolenaar62aec932022-01-29 21:45:34 +00001122 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001123 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001124 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001125
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001126 if (import != NULL)
1127 {
1128 ufunc_T *ufunc;
1129 type_T *type;
1130
Bram Moolenaar753885b2022-08-24 16:30:36 +01001131 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001132 lp->ll_sid = import->imp_sid;
1133 lp->ll_name = skipwhite(p + 1);
1134 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1135 lp->ll_name_end = p;
1136
1137 // check the item is exported
1138 cc = *p;
1139 *p = NUL;
1140 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001141 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001142 {
1143 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001144 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001145 }
1146 *p = cc;
1147 }
1148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001150 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001151 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return p;
1153
Bram Moolenaar4525a572022-02-13 11:57:33 +00001154 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001155 {
1156 // using local variable
1157 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001158 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001159 }
1160 else
1161 {
1162 cc = *p;
1163 *p = NUL;
1164 // When we would write to the variable pass &ht and prevent autoload.
1165 writing = !(flags & GLV_READ_ONLY);
1166 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001167 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001168 if (v == NULL && !quiet)
1169 semsg(_(e_undefined_variable_str), lp->ll_name);
1170 *p = cc;
1171 if (v == NULL)
1172 return NULL;
1173 lp->ll_tv = &v->di_tv;
1174 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001175
Bram Moolenaar4525a572022-02-13 11:57:33 +00001176 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001177 {
1178 if (!quiet)
1179 semsg(_(e_variable_already_declared), lp->ll_name);
1180 return NULL;
1181 }
1182
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 /*
1184 * Loop until no more [idx] or .key is following.
1185 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001186 var1.v_type = VAR_UNKNOWN;
1187 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001188 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001189 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001190 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001191
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001192 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1193 {
1194 if (!quiet)
1195 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1196 return NULL;
1197 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001198 if (lp->ll_tv->v_type != VAR_LIST
1199 && lp->ll_tv->v_type != VAR_DICT
1200 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001203 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001206
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001207 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001208 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001209 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001210 else if (lp->ll_tv->v_type == VAR_BLOB
1211 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001212 r = rettv_blob_alloc(lp->ll_tv);
1213 if (r == FAIL)
1214 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001215
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001219 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001221 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001222
Bram Moolenaar4525a572022-02-13 11:57:33 +00001223 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001224 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001225 && lp->ll_tv == &v->di_tv
1226 && ht != NULL && ht == get_script_local_ht())
1227 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001228 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001229
1230 // Vim9 script local variable: get the type
1231 if (sv != NULL)
1232 lp->ll_valtype = sv->sv_type;
1233 }
1234
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 len = -1;
1236 if (*p == '.')
1237 {
1238 key = p + 1;
1239 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1240 ;
1241 if (len == 0)
1242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001244 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 }
1247 p = key + len;
1248 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001249 else
1250 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001251 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001252 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 if (*p == ':')
1254 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255 else
1256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001258 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001260 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001262 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001263 clear_tv(&var1);
1264 return NULL;
1265 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001266 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001267 }
1268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001269 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 if (*p == ':')
1271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001273 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001275 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001276 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001278 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 if (rettv != NULL
1280 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001281 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001286 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001287 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001288 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001289 }
1290 p = skipwhite(p + 1);
1291 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001293 else
1294 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001296 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001297 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001298 {
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 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001302 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001303 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001304 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001305 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001306 clear_tv(&var2);
1307 return NULL;
1308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001312 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001314
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001318 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001319 clear_tv(&var1);
1320 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 }
1323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001324 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001325 ++p;
1326 }
1327
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001329 {
1330 if (len == -1)
1331 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001332 // "[key]": get key from "var1"
1333 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001334 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001338 }
1339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001340 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001341
1342 // a NULL dict is equivalent with an empty dict
1343 if (lp->ll_tv->vval.v_dict == NULL)
1344 {
1345 lp->ll_tv->vval.v_dict = dict_alloc();
1346 if (lp->ll_tv->vval.v_dict == NULL)
1347 {
1348 clear_tv(&var1);
1349 return NULL;
1350 }
1351 ++lp->ll_tv->vval.v_dict->dv_refcount;
1352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001354
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001355 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 // When assigning to a scope dictionary check that a function and
1358 // variable name is valid (only variable name unless it is l: or
1359 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001360 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001361 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001362 int prevval;
1363 int wrong;
1364
1365 if (len != -1)
1366 {
1367 prevval = key[len];
1368 key[len] = NUL;
1369 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001370 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001371 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001372 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1373 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001374 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001375 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001376 if (len != -1)
1377 key[len] = prevval;
1378 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001379 {
1380 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001381 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001382 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001383 }
1384
Bram Moolenaar10c65862020-10-08 21:16:42 +02001385 if (lp->ll_valtype != NULL)
1386 // use the type of the member
1387 lp->ll_valtype = lp->ll_valtype->tt_member;
1388
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001391 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001392 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001393 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001394 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001395 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001396 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001397 return NULL;
1398 }
1399
Bram Moolenaar31b81602019-02-10 22:14:27 +01001400 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001402 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001404 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001405 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001407 }
1408 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001410 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001412 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 p = NULL;
1415 break;
1416 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001418 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001419 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1420 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001421 {
1422 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001423 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001424 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001425
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001426 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001428 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001429 else if (lp->ll_tv->v_type == VAR_BLOB)
1430 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001431 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1432
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001433 /*
1434 * Get the number and item for the only or first index of the List.
1435 */
1436 if (empty1)
1437 lp->ll_n1 = 0;
1438 else
1439 // is number or string
1440 lp->ll_n1 = (long)tv_get_number(&var1);
1441 clear_tv(&var1);
1442
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001443 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001444 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001445 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001446 return NULL;
1447 }
1448 if (lp->ll_range && !lp->ll_empty2)
1449 {
1450 lp->ll_n2 = (long)tv_get_number(&var2);
1451 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001452 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1453 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001454 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 }
1456 lp->ll_blob = lp->ll_tv->vval.v_blob;
1457 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001458 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001460 else
1461 {
1462 /*
1463 * Get the number and item for the only or first index of the List.
1464 */
1465 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001467 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001468 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001469 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001470 clear_tv(&var1);
1471
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001474 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1475 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001476 if (lp->ll_li == NULL)
1477 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001478 clear_tv(&var2);
1479 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001480 }
1481
Bram Moolenaar10c65862020-10-08 21:16:42 +02001482 if (lp->ll_valtype != NULL)
1483 // use the type of the member
1484 lp->ll_valtype = lp->ll_valtype->tt_member;
1485
Bram Moolenaar8c711452005-01-14 21:53:12 +00001486 /*
1487 * May need to find the item or absolute index for the second
1488 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 * When no index given: "lp->ll_empty2" is TRUE.
1490 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001491 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001493 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001494 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001495 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001496 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001497 if (check_range_index_two(lp->ll_list,
1498 &lp->ll_n1, lp->ll_li,
1499 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001501 }
1502
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001505 }
1506
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001507 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 return p;
1510}
1511
1512/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001513 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517{
1518 vim_free(lp->ll_exp_name);
1519 vim_free(lp->ll_newkey);
1520}
1521
1522/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001523 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001524 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001525 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1526 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001527 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529set_var_lval(
1530 lval_T *lp,
1531 char_u *endp,
1532 typval_T *rettv,
1533 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001534 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1535 char_u *op,
1536 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001537{
1538 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001539 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001540
1541 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001542 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001543 cc = *endp;
1544 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001545 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1546 return;
1547
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001548 if (lp->ll_blob != NULL)
1549 {
1550 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001551
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001552 if (op != NULL && *op != '=')
1553 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001554 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001555 return;
1556 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001557 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001558 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001559
1560 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1561 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001562 if (lp->ll_empty2)
1563 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1564
Bram Moolenaar68452172021-04-12 21:21:02 +02001565 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1566 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001567 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001568 }
1569 else
1570 {
1571 val = (int)tv_get_number_chk(rettv, &error);
1572 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001573 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 }
1575 }
1576 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001578 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001580 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1581 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001582 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001583 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001584 *endp = cc;
1585 return;
1586 }
1587
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001589 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001590 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001591 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001592 {
1593 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001594 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1595 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001596 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001597 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001598 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001599 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001602 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001603 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001604 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1605 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001606 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001607 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001608 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001609 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001610 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001611 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001612 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001613 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001614 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001615 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001616 else if (lp->ll_range)
1617 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001618 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1619 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001620 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001621 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001622 return;
1623 }
1624
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001625 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1626 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001627 }
1628 else
1629 {
1630 /*
1631 * Assign to a List or Dictionary item.
1632 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001633 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1634 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001635 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001636 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001637 return;
1638 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001639
1640 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001641 && check_typval_arg_type(lp->ll_valtype, rettv,
1642 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001643 return;
1644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001645 if (lp->ll_newkey != NULL)
1646 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 if (op != NULL && *op != '=')
1648 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001649 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 return;
1651 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001652 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1653 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001654 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001656 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001657 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001658 if (di == NULL)
1659 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001660 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1661 {
1662 vim_free(di);
1663 return;
1664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001665 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001666 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 else if (op != NULL && *op != '=')
1668 {
1669 tv_op(lp->ll_tv, rettv, op);
1670 return;
1671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001673 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 /*
1676 * Assign the value to the variable or list item.
1677 */
1678 if (copy)
1679 copy_tv(rettv, lp->ll_tv);
1680 else
1681 {
1682 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001683 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001684 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 }
1686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687}
1688
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001690 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1691 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 * Returns OK or FAIL.
1693 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001697 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 char_u numbuf[NUMBUFLEN];
1699 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001700 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001702 // Can't do anything with a Funcref or Dict on the right.
1703 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001704 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001705 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1706 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 {
1708 switch (tv1->v_type)
1709 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001710 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001711 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 case VAR_DICT:
1714 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001715 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001716 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001717 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001718 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001719 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001720 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 break;
1722
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 case VAR_BLOB:
1724 if (*op != '+' || tv2->v_type != VAR_BLOB)
1725 break;
1726 // BLOB += BLOB
1727 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1728 {
1729 blob_T *b1 = tv1->vval.v_blob;
1730 blob_T *b2 = tv2->vval.v_blob;
1731 int i, len = blob_len(b2);
1732 for (i = 0; i < len; i++)
1733 ga_append(&b1->bv_ga, blob_get(b2, i));
1734 }
1735 return OK;
1736
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 case VAR_LIST:
1738 if (*op != '+' || tv2->v_type != VAR_LIST)
1739 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001740 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001741 if (tv2->vval.v_list != NULL)
1742 {
1743 if (tv1->vval.v_list == NULL)
1744 {
1745 tv1->vval.v_list = tv2->vval.v_list;
1746 ++tv1->vval.v_list->lv_refcount;
1747 }
1748 else
1749 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1750 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 return OK;
1752
1753 case VAR_NUMBER:
1754 case VAR_STRING:
1755 if (tv2->v_type == VAR_LIST)
1756 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001757 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001759 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001760 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001761 if (tv2->v_type == VAR_FLOAT)
1762 {
1763 float_T f = n;
1764
Bram Moolenaarff697e62019-02-12 22:28:33 +01001765 if (*op == '%')
1766 break;
1767 switch (*op)
1768 {
1769 case '+': f += tv2->vval.v_float; break;
1770 case '-': f -= tv2->vval.v_float; break;
1771 case '*': f *= tv2->vval.v_float; break;
1772 case '/': f /= tv2->vval.v_float; break;
1773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001774 clear_tv(tv1);
1775 tv1->v_type = VAR_FLOAT;
1776 tv1->vval.v_float = f;
1777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001779 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001780 switch (*op)
1781 {
1782 case '+': n += tv_get_number(tv2); break;
1783 case '-': n -= tv_get_number(tv2); break;
1784 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001785 case '/': n = num_divide(n, tv_get_number(tv2),
1786 &failed); break;
1787 case '%': n = num_modulus(n, tv_get_number(tv2),
1788 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001789 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001790 clear_tv(tv1);
1791 tv1->v_type = VAR_NUMBER;
1792 tv1->vval.v_number = n;
1793 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001794 }
1795 else
1796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001797 if (tv2->v_type == VAR_FLOAT)
1798 break;
1799
Bram Moolenaarff697e62019-02-12 22:28:33 +01001800 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001801 s = tv_get_string(tv1);
1802 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 clear_tv(tv1);
1804 tv1->v_type = VAR_STRING;
1805 tv1->vval.v_string = s;
1806 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001807 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001808
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001809 case VAR_FLOAT:
1810 {
1811 float_T f;
1812
Bram Moolenaarff697e62019-02-12 22:28:33 +01001813 if (*op == '%' || *op == '.'
1814 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001815 && tv2->v_type != VAR_NUMBER
1816 && tv2->v_type != VAR_STRING))
1817 break;
1818 if (tv2->v_type == VAR_FLOAT)
1819 f = tv2->vval.v_float;
1820 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001821 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001822 switch (*op)
1823 {
1824 case '+': tv1->vval.v_float += f; break;
1825 case '-': tv1->vval.v_float -= f; break;
1826 case '*': tv1->vval.v_float *= f; break;
1827 case '/': tv1->vval.v_float /= f; break;
1828 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001829 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001830 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001831 }
1832 }
1833
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001834 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 return FAIL;
1836}
1837
1838/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 * Evaluate the expression used in a ":for var in expr" command.
1840 * "arg" points to "var".
1841 * Set "*errp" to TRUE for an error, FALSE otherwise;
1842 * Return a pointer that holds the info. Null when there is an error.
1843 */
1844 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001845eval_for_line(
1846 char_u *arg,
1847 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001848 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001849 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850{
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001852 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001854 typval_T tv;
1855 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001856 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001858 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001860 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 if (fi == NULL)
1862 return NULL;
1863
Bram Moolenaar404557e2021-07-05 21:41:48 +02001864 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1865 &fi->fi_semicolon, FALSE);
1866 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 return fi;
1868
Bram Moolenaar404557e2021-07-05 21:41:48 +02001869 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001870 if (expr[0] != 'i' || expr[1] != 'n'
1871 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001873 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1874 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1875 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001876 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 return fi;
1878 }
1879
1880 if (skip)
1881 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001882 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1883 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 {
1885 *errp = FALSE;
1886 if (!skip)
1887 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001888 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001889 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001890 l = tv.vval.v_list;
1891 if (l == NULL)
1892 {
1893 // a null list is like an empty list: do nothing
1894 clear_tv(&tv);
1895 }
1896 else
1897 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001899 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001901 // No need to increment the refcount, it's already set for
1902 // the list being used in "tv".
1903 fi->fi_list = l;
1904 list_add_watch(l, &fi->fi_lw);
1905 fi->fi_lw.lw_item = l->lv_first;
1906 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001907 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001908 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001909 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001910 fi->fi_bi = 0;
1911 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001912 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001913 typval_T btv;
1914
1915 // Make a copy, so that the iteration still works when the
1916 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001918 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001919 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001920 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001921 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001922 else if (tv.v_type == VAR_STRING)
1923 {
1924 fi->fi_byte_idx = 0;
1925 fi->fi_string = tv.vval.v_string;
1926 tv.vval.v_string = NULL;
1927 if (fi->fi_string == NULL)
1928 fi->fi_string = vim_strsave((char_u *)"");
1929 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 else
1931 {
Sean Dewar80d73952021-08-04 19:25:54 +02001932 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001933 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 }
1935 }
1936 }
1937 if (skip)
1938 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001939 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940
1941 return fi;
1942}
1943
1944/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001945 * Used when looping over a :for line, skip the "in expr" part.
1946 */
1947 void
1948skip_for_lines(void *fi_void, evalarg_T *evalarg)
1949{
1950 forinfo_T *fi = (forinfo_T *)fi_void;
1951 int i;
1952
1953 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001954 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001955}
1956
1957/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 * Use the first item in a ":for" list. Advance to the next.
1959 * Assign the values to the variable (list). "arg" points to the first one.
1960 * Return TRUE when a valid item was found, FALSE when at end of list or
1961 * something wrong.
1962 */
1963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001964next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001966 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001968 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001969 ? (ASSIGN_FINAL
1970 // first round: error if variable exists
1971 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001972 | ASSIGN_NO_MEMBER_TYPE
1973 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001974 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001976 int skip_assign = in_vim9script() && arg[0] == '_'
1977 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001979 if (fi->fi_blob != NULL)
1980 {
1981 typval_T tv;
1982
1983 if (fi->fi_bi >= blob_len(fi->fi_blob))
1984 return FALSE;
1985 tv.v_type = VAR_NUMBER;
1986 tv.v_lock = VAR_FIXED;
1987 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1988 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001989 if (skip_assign)
1990 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001991 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001992 fi->fi_varcount, flag, NULL) == OK;
1993 }
1994
1995 if (fi->fi_string != NULL)
1996 {
1997 typval_T tv;
1998 int len;
1999
2000 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2001 if (len == 0)
2002 return FALSE;
2003 tv.v_type = VAR_STRING;
2004 tv.v_lock = VAR_FIXED;
2005 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2006 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002007 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002008 if (skip_assign)
2009 result = TRUE;
2010 else
2011 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002012 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002013 vim_free(tv.vval.v_string);
2014 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002015 }
2016
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 item = fi->fi_lw.lw_item;
2018 if (item == NULL)
2019 result = FALSE;
2020 else
2021 {
2022 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002023 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002024 if (skip_assign)
2025 result = TRUE;
2026 else
2027 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002028 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 }
2030 return result;
2031}
2032
2033/*
2034 * Free the structure used to store info used by ":for".
2035 */
2036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002037free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038{
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002041 if (fi == NULL)
2042 return;
2043 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002044 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002046 list_unref(fi->fi_list);
2047 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002048 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002049 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002050 else
2051 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 vim_free(fi);
2053}
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002056set_context_for_expression(
2057 expand_T *xp,
2058 char_u *arg,
2059 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002061 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002065 if (cmdidx == CMD_let || cmdidx == CMD_var
2066 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 {
2068 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002071 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073 {
2074 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002075 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002076 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 break;
2078 }
2079 return;
2080 }
2081 }
2082 else
2083 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2084 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 while ((xp->xp_pattern = vim_strpbrk(arg,
2086 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2087 {
2088 c = *xp->xp_pattern;
2089 if (c == '&')
2090 {
2091 c = xp->xp_pattern[1];
2092 if (c == '&')
2093 {
2094 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002095 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002100 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2101 xp->xp_pattern += 2;
2102
2103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105 else if (c == '$')
2106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 xp->xp_context = EXPAND_ENV_VARS;
2109 }
2110 else if (c == '=')
2111 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002112 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 xp->xp_context = EXPAND_EXPRESSION;
2114 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002115 else if (c == '#'
2116 && xp->xp_context == EXPAND_EXPRESSION)
2117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002118 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002119 break;
2120 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002121 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 && xp->xp_context == EXPAND_FUNCTIONS
2123 && vim_strchr(xp->xp_pattern, '(') == NULL)
2124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002125 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 break;
2127 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002128 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002130 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2133 if (c == '\\' && xp->xp_pattern[1] != NUL)
2134 ++xp->xp_pattern;
2135 xp->xp_context = EXPAND_NOTHING;
2136 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002137 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002139 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2141 /* skip */ ;
2142 xp->xp_context = EXPAND_NOTHING;
2143 }
2144 else if (c == '|')
2145 {
2146 if (xp->xp_pattern[1] == '|')
2147 {
2148 ++xp->xp_pattern;
2149 xp->xp_context = EXPAND_EXPRESSION;
2150 }
2151 else
2152 xp->xp_context = EXPAND_COMMANDS;
2153 }
2154 else
2155 xp->xp_context = EXPAND_EXPRESSION;
2156 }
2157 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002158 // Doesn't look like something valid, expand as an expression
2159 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002160 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 arg = xp->xp_pattern;
2162 if (*arg != NUL)
2163 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2164 /* skip */ ;
2165 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002166
2167 // ":exe one two" completes "two"
2168 if ((cmdidx == CMD_execute
2169 || cmdidx == CMD_echo
2170 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002171 || cmdidx == CMD_echomsg
2172 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002173 && xp->xp_context == EXPAND_EXPRESSION)
2174 {
2175 for (;;)
2176 {
2177 char_u *n = skiptowhite(arg);
2178
2179 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2180 break;
2181 arg = skipwhite(n);
2182 }
2183 }
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 xp->xp_pattern = arg;
2186}
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002189 * Return TRUE if "pat" matches "text".
2190 * Does not use 'cpo' and always uses 'magic'.
2191 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002192 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002193pattern_match(char_u *pat, char_u *text, int ic)
2194{
2195 int matches = FALSE;
2196 char_u *save_cpo;
2197 regmatch_T regmatch;
2198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002199 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002200 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002201 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002202 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2203 if (regmatch.regprog != NULL)
2204 {
2205 regmatch.rm_ic = ic;
2206 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2207 vim_regfree(regmatch.regprog);
2208 }
2209 p_cpo = save_cpo;
2210 return matches;
2211}
2212
2213/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002214 * Handle a name followed by "(". Both for just "name(arg)" and for
2215 * "expr->name(arg)".
2216 * Returns OK or FAIL.
2217 */
2218 static int
2219eval_func(
2220 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002221 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002222 char_u *name,
2223 int name_len,
2224 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002225 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002226 typval_T *basetv) // "expr" for "expr->name(arg)"
2227{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002228 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002229 char_u *s = name;
2230 int len = name_len;
2231 partial_T *partial;
2232 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002233 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002234 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002235
2236 if (!evaluate)
2237 check_vars(s, len);
2238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002239 // If "s" is the name of a variable of type VAR_FUNC
2240 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002241 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002242 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002244 // Need to make a copy, in case evaluating the arguments makes
2245 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002246 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002247 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002248 ret = FAIL;
2249 else
2250 {
2251 funcexe_T funcexe;
2252
2253 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002254 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002255 funcexe.fe_firstline = curwin->w_cursor.lnum;
2256 funcexe.fe_lastline = curwin->w_cursor.lnum;
2257 funcexe.fe_evaluate = evaluate;
2258 funcexe.fe_partial = partial;
2259 funcexe.fe_basetv = basetv;
2260 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002261 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002262 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002263 }
2264 vim_free(s);
2265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002266 // If evaluate is FALSE rettv->v_type was not set in
2267 // get_func_tv, but it's needed in handle_subscript() to parse
2268 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002269 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2270 {
2271 rettv->vval.v_string = NULL;
2272 rettv->v_type = VAR_FUNC;
2273 }
2274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002275 // Stop the expression evaluation when immediately
2276 // aborting on error, or when an interrupt occurred or
2277 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002278 if (evaluate && aborting())
2279 {
2280 if (ret == OK)
2281 clear_tv(rettv);
2282 ret = FAIL;
2283 }
2284 return ret;
2285}
2286
2287/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002288 * After a NL, skip over empty lines and comment-only lines.
2289 */
2290 static char_u *
2291newline_skip_comments(char_u *arg)
2292{
2293 char_u *p = arg + 1;
2294
2295 for (;;)
2296 {
2297 p = skipwhite(p);
2298
2299 if (*p == NUL)
2300 break;
2301 if (vim9_comment_start(p))
2302 {
2303 char_u *nl = vim_strchr(p, NL);
2304
2305 if (nl == NULL)
2306 break;
2307 p = nl;
2308 }
2309 if (*p != NL)
2310 break;
2311 ++p; // skip another NL
2312 }
2313 return p;
2314}
2315
2316/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002317 * Get the next line source line without advancing. But do skip over comment
2318 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002319 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002320 */
2321 static char_u *
2322getline_peek_skip_comments(evalarg_T *evalarg)
2323{
2324 for (;;)
2325 {
2326 char_u *next = getline_peek(evalarg->eval_getline,
2327 evalarg->eval_cookie);
2328 char_u *p;
2329
2330 if (next == NULL)
2331 break;
2332 p = skipwhite(next);
2333 if (*p != NUL && !vim9_comment_start(p))
2334 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002335 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002336 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002337 }
2338 return NULL;
2339}
2340
2341/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002342 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2343 * comment) and there is a next line, return the next line (skipping blanks)
2344 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002345 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2346 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002347 * "arg" must point somewhere inside a line, not at the start.
2348 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002349 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2351{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002352 char_u *p = skipwhite(arg);
2353
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002354 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002355 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002357 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2358 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002359 && (*p == NUL || *p == NL
2360 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002361 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002362 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002363
Bram Moolenaare442d592022-05-05 12:20:28 +01002364 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002365 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002366 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002367 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002368 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002369 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002370
Bram Moolenaar9d489562020-07-30 20:08:50 +02002371 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002372 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002373 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002374 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002375 }
2376 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002377 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002378}
2379
2380/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002381 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002382 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002383 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002384 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002385eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002386{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002387 garray_T *gap = &evalarg->eval_ga;
2388 char_u *line;
2389
Bram Moolenaar39be4982022-05-06 21:51:50 +01002390 if (arg != NULL)
2391 {
2392 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002393 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002394 // Truncate before a trailing comment, so that concatenating the lines
2395 // won't turn the rest into a comment.
2396 if (*skipwhite(arg) == '#')
2397 *arg = NUL;
2398 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002399
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002400 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002401 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2402 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002403 else
2404 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002405 if (line == NULL)
2406 return NULL;
2407
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002408 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002409 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2410 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002411 char_u *p = skipwhite(line);
2412
2413 // Going to concatenate the lines after parsing. For an empty or
2414 // comment line use an empty string.
2415 if (*p == NUL || vim9_comment_start(p))
2416 {
2417 vim_free(line);
2418 line = vim_strsave((char_u *)"");
2419 }
2420
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002421 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2422 ++gap->ga_len;
2423 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002424 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002425 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002426 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002427 evalarg->eval_tofree = line;
2428 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002429
2430 // Advanced to the next line, "arg" no longer points into the previous
2431 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002432 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002433 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002434}
2435
Bram Moolenaare6b53242020-07-01 17:28:33 +02002436/*
2437 * Call eval_next_non_blank() and get the next line if needed.
2438 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002439 char_u *
2440skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2441{
2442 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002443 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002444
Bram Moolenaar962d7212020-07-04 14:15:00 +02002445 if (evalarg == NULL)
2446 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002447 eval_next_non_blank(p, evalarg, &getnext);
2448 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002449 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002450 return p;
2451}
2452
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002453/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002454 * The "eval" functions have an "evalarg" argument: When NULL or
2455 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2456 * parsed but not executed. The functions may return OK, but the rettv will be
2457 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
2459
2460/*
2461 * Handle zero level expression.
2462 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002464 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 * Return OK or FAIL.
2467 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002469eval0(
2470 char_u *arg,
2471 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002472 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002475 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2476}
2477
2478/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002479 * If "arg" is a simple function call without arguments then call it and return
2480 * the result. Otherwise return NOTDONE.
2481 */
2482 int
2483may_call_simple_func(
2484 char_u *arg,
2485 typval_T *rettv)
2486{
2487 char_u *parens = (char_u *)strstr((char *)arg, "()");
2488 int r = NOTDONE;
2489
2490 // If the expression is "FuncName()" then we can skip a lot of overhead.
2491 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2492 {
2493 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2494
2495 if (to_name_end(p, TRUE) == parens)
2496 r = call_simple_func(arg, (int)(parens - arg), rettv);
2497 }
2498 return r;
2499}
2500
2501/*
2502 * Handle zero level expression with optimization for a simple function call.
2503 * Same arguments and return value as eval0().
2504 */
2505 int
2506eval0_simple_funccal(
2507 char_u *arg,
2508 typval_T *rettv,
2509 exarg_T *eap,
2510 evalarg_T *evalarg)
2511{
2512 int r = may_call_simple_func(arg, rettv);
2513
2514 if (r == NOTDONE)
2515 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2516 return r;
2517}
2518
2519/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002520 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2521 * expression and don't check what comes after the expression.
2522 */
2523 int
2524eval0_retarg(
2525 char_u *arg,
2526 typval_T *rettv,
2527 exarg_T *eap,
2528 evalarg_T *evalarg,
2529 char_u **retarg)
2530{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 int ret;
2532 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002533 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002534 int did_emsg_before = did_emsg;
2535 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002537 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002538 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
2540 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002541 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002542
Bram Moolenaar79481362022-06-27 20:15:10 +01002543 if (ret != FAIL)
2544 {
2545 expr_end = p;
2546 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002547
Bram Moolenaar79481362022-06-27 20:15:10 +01002548 // In Vim9 script a command block is not split at NL characters for
2549 // commands using an expression argument. Skip over a '#' comment to
2550 // check for a following NL. Require white space before the '#'.
2551 if (in_vim9script() && p > expr_end && retarg == NULL)
2552 while (*p == '#')
2553 {
2554 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002555
Bram Moolenaar79481362022-06-27 20:15:10 +01002556 if (nl == NULL)
2557 break;
2558 p = skipwhite(nl + 1);
2559 if (eap != NULL && *p != NUL)
2560 eap->nextcmd = p;
2561 check_for_end = FALSE;
2562 }
2563
2564 if (check_for_end)
2565 end_error = !ends_excmd2(arg, p);
2566 }
2567
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002568 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 /*
2573 * Report the invalid expression unless the expression evaluation has
2574 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002575 * exception, or we already gave a more specific error.
2576 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002578 if (!aborting()
2579 && did_emsg == did_emsg_before
2580 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002581 && (flags & EVAL_CONSTANT) == 0
2582 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002583 {
2584 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002585 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002586 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002587 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002588 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002589
2590 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002591 // a next command to avoid more errors, unless "|" is following, which
2592 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002593 if (eap != NULL && p != NULL
2594 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002595 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002598
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002599 if (retarg != NULL)
2600 *retarg = p;
2601 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002602 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 return ret;
2605}
2606
2607/*
2608 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002609 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002610 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 *
2612 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002613 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002615 * Note: "rettv.v_lock" is not set.
2616 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * Return OK or FAIL.
2618 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002619 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002620eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002622 char_u *p;
2623 int getnext;
2624
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002625 CLEAR_POINTER(rettv);
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * Get the first variable.
2629 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002630 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return FAIL;
2632
Bram Moolenaar793648f2020-06-26 21:28:25 +02002633 p = eval_next_non_blank(*arg, evalarg, &getnext);
2634 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002636 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002637 int result;
2638 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 evalarg_T *evalarg_used = evalarg;
2640 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002641 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002642 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002643 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644
2645 if (evalarg == NULL)
2646 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002647 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650 orig_flags = evalarg_used->eval_flags;
2651 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002652
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002654 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002655 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002656 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002657 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002658 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002659 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002660 clear_tv(rettv);
2661 return FAIL;
2662 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002663 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002664 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 int error = FALSE;
2670
Bram Moolenaar13106602020-10-04 16:06:05 +02002671 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002672 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002673 else if (vim9script)
2674 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002675 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002677 if (error || !op_falsy || !result)
2678 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 if (error)
2680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
2683 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002684 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002686 if (op_falsy)
2687 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002688 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002689 {
mityu4ac198c2021-05-28 17:52:40 +02002690 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002691 clear_tv(rettv);
2692 return FAIL;
2693 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002695 evalarg_used->eval_flags = (op_falsy ? !result : result)
2696 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2697 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002698 {
2699 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002701 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002702 if (!op_falsy || !result)
2703 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002705 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002707 /*
2708 * Check for the ":".
2709 */
2710 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2711 if (*p != ':')
2712 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002713 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002714 if (evaluate && result)
2715 clear_tv(rettv);
2716 evalarg_used->eval_flags = orig_flags;
2717 return FAIL;
2718 }
2719 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002720 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002721 else
2722 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002723 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002724 {
2725 error_white_both(p, 1);
2726 clear_tv(rettv);
2727 evalarg_used->eval_flags = orig_flags;
2728 return FAIL;
2729 }
2730 *arg = p;
2731 }
2732
2733 /*
2734 * Get the third variable. Recursive!
2735 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002736 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002737 {
mityu4ac198c2021-05-28 17:52:40 +02002738 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002739 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002740 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002741 return FAIL;
2742 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002743 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2744 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002746 if (eval1(arg, &var2, evalarg_used) == FAIL)
2747 {
2748 if (evaluate && result)
2749 clear_tv(rettv);
2750 evalarg_used->eval_flags = orig_flags;
2751 return FAIL;
2752 }
2753 if (evaluate && !result)
2754 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756
2757 if (evalarg == NULL)
2758 clear_evalarg(&local_evalarg, NULL);
2759 else
2760 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762
2763 return OK;
2764}
2765
2766/*
2767 * Handle first level expression:
2768 * expr2 || expr2 || expr2 logical OR
2769 *
2770 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002771 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 *
2773 * Return OK or FAIL.
2774 */
2775 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002778 char_u *p;
2779 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002782 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 return FAIL;
2786
2787 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002788 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002790 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002791 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002793 evalarg_T *evalarg_used = evalarg;
2794 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 int evaluate;
2796 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002797 long result = FALSE;
2798 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002799 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002800 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002801
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 if (evalarg == NULL)
2803 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002804 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002805 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002806 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002807 orig_flags = evalarg_used->eval_flags;
2808 evaluate = orig_flags & EVAL_EVALUATE;
2809 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002811 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002812 result = tv_get_bool_chk(rettv, &error);
2813 else if (tv_get_number_chk(rettv, &error) != 0)
2814 result = TRUE;
2815 clear_tv(rettv);
2816 if (error)
2817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
2820 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002821 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002823 while (p[0] == '|' && p[1] == '|')
2824 {
2825 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002826 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002827 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002828 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002829 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002830 {
2831 error_white_both(p, 2);
2832 clear_tv(rettv);
2833 return FAIL;
2834 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002835 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002836 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002837
2838 /*
2839 * Get the second variable.
2840 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002841 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002842 {
mityu4ac198c2021-05-28 17:52:40 +02002843 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002844 clear_tv(rettv);
2845 return FAIL;
2846 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002847 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2848 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002849 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002850 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002852
2853 /*
2854 * Compute the result.
2855 */
2856 if (evaluate && !result)
2857 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002858 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002859 result = tv_get_bool_chk(&var2, &error);
2860 else if (tv_get_number_chk(&var2, &error) != 0)
2861 result = TRUE;
2862 clear_tv(&var2);
2863 if (error)
2864 return FAIL;
2865 }
2866 if (evaluate)
2867 {
2868 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002869 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002870 rettv->v_type = VAR_BOOL;
2871 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 }
2873 else
2874 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002875 rettv->v_type = VAR_NUMBER;
2876 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002877 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002878 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002879
2880 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002882
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002883 if (evalarg == NULL)
2884 clear_evalarg(&local_evalarg, NULL);
2885 else
2886 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888
2889 return OK;
2890}
2891
2892/*
2893 * Handle second level expression:
2894 * expr3 && expr3 && expr3 logical AND
2895 *
2896 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002897 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 *
2899 * Return OK or FAIL.
2900 */
2901 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002904 char_u *p;
2905 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002908 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 return FAIL;
2912
2913 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002914 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002916 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002917 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002919 evalarg_T *evalarg_used = evalarg;
2920 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 int orig_flags;
2922 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002923 long result = TRUE;
2924 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002925 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002926 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002927
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002928 if (evalarg == NULL)
2929 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002930 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002931 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002933 orig_flags = evalarg_used->eval_flags;
2934 evaluate = orig_flags & EVAL_EVALUATE;
2935 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002937 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002938 result = tv_get_bool_chk(rettv, &error);
2939 else if (tv_get_number_chk(rettv, &error) == 0)
2940 result = FALSE;
2941 clear_tv(rettv);
2942 if (error)
2943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945
2946 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002947 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002949 while (p[0] == '&' && p[1] == '&')
2950 {
2951 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002952 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002953 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002954 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002955 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002956 {
2957 error_white_both(p, 2);
2958 clear_tv(rettv);
2959 return FAIL;
2960 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002961 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002962 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002963
2964 /*
2965 * Get the second variable.
2966 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002967 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002968 {
mityu4ac198c2021-05-28 17:52:40 +02002969 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002970 clear_tv(rettv);
2971 return FAIL;
2972 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002973 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2974 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002975 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002976 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002977 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002979
2980 /*
2981 * Compute the result.
2982 */
2983 if (evaluate && result)
2984 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002985 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002986 result = tv_get_bool_chk(&var2, &error);
2987 else if (tv_get_number_chk(&var2, &error) == 0)
2988 result = FALSE;
2989 clear_tv(&var2);
2990 if (error)
2991 return FAIL;
2992 }
2993 if (evaluate)
2994 {
2995 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002996 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002997 rettv->v_type = VAR_BOOL;
2998 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002999 }
3000 else
3001 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003002 rettv->v_type = VAR_NUMBER;
3003 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003004 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003005 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003006
3007 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003009
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003010 if (evalarg == NULL)
3011 clear_evalarg(&local_evalarg, NULL);
3012 else
3013 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015
3016 return OK;
3017}
3018
3019/*
3020 * Handle third level expression:
3021 * var1 == var2
3022 * var1 =~ var2
3023 * var1 != var2
3024 * var1 !~ var2
3025 * var1 > var2
3026 * var1 >= var2
3027 * var1 < var2
3028 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003029 * var1 is var2
3030 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 *
3032 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003033 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 *
3035 * Return OK or FAIL.
3036 */
3037 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003041 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003042 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003044 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003047 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 return FAIL;
3051
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003052 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003053
Bram Moolenaar696ba232020-07-29 21:20:41 +02003054 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003059 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003061 typval_T var2;
3062 int ic;
3063 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003064 int evaluate = evalarg == NULL
3065 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003066 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003067
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003068 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003069 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003070 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003071 p = *arg;
3072 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003073 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3074 {
mityu4ac198c2021-05-28 17:52:40 +02003075 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003076 clear_tv(rettv);
3077 return FAIL;
3078 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003079
Bram Moolenaar696ba232020-07-29 21:20:41 +02003080 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3081 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003082 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003083 clear_tv(rettv);
3084 return FAIL;
3085 }
3086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003087 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (p[len] == '?')
3089 {
3090 ic = TRUE;
3091 ++len;
3092 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 else if (p[len] == '#')
3095 {
3096 ic = FALSE;
3097 ++len;
3098 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003099 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003101 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103 /*
3104 * Get the second variable.
3105 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003106 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3107 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003108 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003109 clear_tv(rettv);
3110 return FAIL;
3111 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003112 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 return FAIL;
3117 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003118 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003119 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003120 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003121
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003122 // use the line of the comparison for messages
3123 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003124 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003125 {
3126 ret = FAIL;
3127 clear_tv(rettv);
3128 }
3129 else
3130 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003131 clear_tv(&var2);
3132 return ret;
3133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
3135
3136 return OK;
3137}
3138
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003139/*
3140 * Make a copy of blob "tv1" and append blob "tv2".
3141 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 void
3143eval_addblob(typval_T *tv1, typval_T *tv2)
3144{
3145 blob_T *b1 = tv1->vval.v_blob;
3146 blob_T *b2 = tv2->vval.v_blob;
3147 blob_T *b = blob_alloc();
3148 int i;
3149
3150 if (b != NULL)
3151 {
3152 for (i = 0; i < blob_len(b1); i++)
3153 ga_append(&b->bv_ga, blob_get(b1, i));
3154 for (i = 0; i < blob_len(b2); i++)
3155 ga_append(&b->bv_ga, blob_get(b2, i));
3156
3157 clear_tv(tv1);
3158 rettv_blob_set(tv1, b);
3159 }
3160}
3161
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003162/*
3163 * Make a copy of list "tv1" and append list "tv2".
3164 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 int
3166eval_addlist(typval_T *tv1, typval_T *tv2)
3167{
3168 typval_T var3;
3169
3170 // concatenate Lists
3171 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3172 {
3173 clear_tv(tv1);
3174 clear_tv(tv2);
3175 return FAIL;
3176 }
3177 clear_tv(tv1);
3178 *tv1 = var3;
3179 return OK;
3180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003183 * Handle the bitwise left/right shift operator expression:
3184 * var1 << var2
3185 * var1 >> var2
3186 *
3187 * "arg" must point to the first non-white of the expression.
3188 * "arg" is advanced to just after the recognized expression.
3189 *
3190 * Return OK or FAIL.
3191 */
3192 static int
3193eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3194{
3195 /*
3196 * Get the first expression.
3197 */
3198 if (eval6(arg, rettv, evalarg) == FAIL)
3199 return FAIL;
3200
3201 /*
3202 * Repeat computing, until no '<<' or '>>' is following.
3203 */
3204 for (;;)
3205 {
3206 char_u *p;
3207 int getnext;
3208 exprtype_T type;
3209 int evaluate;
3210 typval_T var2;
3211 int vim9script;
3212
3213 p = eval_next_non_blank(*arg, evalarg, &getnext);
3214 if (p[0] == '<' && p[1] == '<')
3215 type = EXPR_LSHIFT;
3216 else if (p[0] == '>' && p[1] == '>')
3217 type = EXPR_RSHIFT;
3218 else
3219 return OK;
3220
3221 // Handle a bitwise left or right shift operator
3222 if (rettv->v_type != VAR_NUMBER)
3223 {
3224 // left operand should be a number
3225 emsg(_(e_bitshift_ops_must_be_number));
3226 clear_tv(rettv);
3227 return FAIL;
3228 }
3229
3230 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3231 vim9script = in_vim9script();
3232 if (getnext)
3233 {
3234 *arg = eval_next_line(*arg, evalarg);
3235 p = *arg;
3236 }
3237 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3238 {
3239 error_white_both(*arg, 2);
3240 clear_tv(rettv);
3241 return FAIL;
3242 }
3243
3244 /*
3245 * Get the second variable.
3246 */
3247 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3248 {
3249 error_white_both(p, 2);
3250 clear_tv(rettv);
3251 return FAIL;
3252 }
3253 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3254 if (eval6(arg, &var2, evalarg) == FAIL)
3255 {
3256 clear_tv(rettv);
3257 return FAIL;
3258 }
3259
3260 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3261 {
3262 // right operand should be a positive number
3263 if (var2.v_type != VAR_NUMBER)
3264 emsg(_(e_bitshift_ops_must_be_number));
3265 else
3266 emsg(_(e_bitshift_ops_must_be_postive));
3267 clear_tv(rettv);
3268 clear_tv(&var2);
3269 return FAIL;
3270 }
3271
3272 if (evaluate)
3273 {
3274 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3275 // shifting more bits than we have always results in zero
3276 rettv->vval.v_number = 0;
3277 else if (type == EXPR_LSHIFT)
3278 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003279 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003280 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003281 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003282 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003283 }
3284
3285 clear_tv(&var2);
3286 }
3287
3288 return OK;
3289}
3290
3291/*
3292 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003293 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003295 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003296 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 *
3298 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003299 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 *
3301 * Return OK or FAIL.
3302 */
3303 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003304eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003307 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003309 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return FAIL;
3311
3312 /*
3313 * Repeat computing, until no '+', '-' or '.' is following.
3314 */
3315 for (;;)
3316 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003317 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003318 int getnext;
3319 char_u *p;
3320 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003321 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003322 int concat;
3323 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003324 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003325
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003326 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003327 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003328 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003329 p = eval_next_non_blank(*arg, evalarg, &getnext);
3330 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003331 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003332 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3333 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003335 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3336 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003337
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003338 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003339 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003340 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003341 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003342 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003343 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003344 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003345 {
mityu4ac198c2021-05-28 17:52:40 +02003346 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003347 clear_tv(rettv);
3348 return FAIL;
3349 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003350 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003351 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003352 if ((op != '+' || (rettv->v_type != VAR_LIST
3353 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003354 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003355 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003357 int error = FALSE;
3358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003359 // For "list + ...", an illegal use of the first operand as
3360 // a number cannot be determined before evaluating the 2nd
3361 // operand: if this is also a list, all is ok.
3362 // For "something . ...", "something - ..." or "non-list + ...",
3363 // we know that the first operand needs to be a string or number
3364 // without evaluating the 2nd operand. So check before to avoid
3365 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003366 if (op != '.')
3367 tv_get_number_chk(rettv, &error);
3368 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003369 {
3370 clear_tv(rettv);
3371 return FAIL;
3372 }
3373 }
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 /*
3376 * Get the second variable.
3377 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003378 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003379 {
mityu89dcb4d2021-05-28 13:50:17 +02003380 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003381 clear_tv(rettv);
3382 return FAIL;
3383 }
3384 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003385 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return FAIL;
3389 }
3390
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003391 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 /*
3394 * Compute the result.
3395 */
3396 if (op == '.')
3397 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003398 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3399 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003400 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003401
Bram Moolenaar2e086612020-08-25 22:37:48 +02003402 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003403 || var2.v_type == VAR_CHANNEL
3404 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003405 semsg(_(e_using_invalid_value_as_string_str),
3406 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003407 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003408 {
3409 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3410 var2.vval.v_float);
3411 s2 = buf2;
3412 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003413 else
3414 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003415 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 {
3417 clear_tv(rettv);
3418 clear_tv(&var2);
3419 return FAIL;
3420 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003422 clear_tv(rettv);
3423 rettv->v_type = VAR_STRING;
3424 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003426 else if (op == '+' && rettv->v_type == VAR_BLOB
3427 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003429 else if (op == '+' && rettv->v_type == VAR_LIST
3430 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003433 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 else
3436 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437 int error = FALSE;
3438 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003439 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003441 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003442 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003443 f1 = rettv->vval.v_float;
3444 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003445 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003446 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003448 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003449 if (error)
3450 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003451 // This can only happen for "list + non-list" or
3452 // "blob + non-blob". For "non-list + ..." or
3453 // "something - ...", we returned before evaluating the
3454 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003455 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003456 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003457 return FAIL;
3458 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003459 if (var2.v_type == VAR_FLOAT)
3460 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003461 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003462 if (var2.v_type == VAR_FLOAT)
3463 {
3464 f2 = var2.vval.v_float;
3465 n2 = 0;
3466 }
3467 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003468 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003470 if (error)
3471 {
3472 clear_tv(rettv);
3473 clear_tv(&var2);
3474 return FAIL;
3475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003476 if (rettv->v_type == VAR_FLOAT)
3477 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003479 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003482 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3483 {
3484 if (op == '+')
3485 f1 = f1 + f2;
3486 else
3487 f1 = f1 - f2;
3488 rettv->v_type = VAR_FLOAT;
3489 rettv->vval.v_float = f1;
3490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 {
3493 if (op == '+')
3494 n1 = n1 + n2;
3495 else
3496 n1 = n1 - n2;
3497 rettv->v_type = VAR_NUMBER;
3498 rettv->vval.v_number = n1;
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 }
3504 return OK;
3505}
3506
3507/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003508 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 * * number multiplication
3510 * / number division
3511 * % number modulo
3512 *
3513 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003514 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 *
3516 * Return OK or FAIL.
3517 */
3518 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003519eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520 char_u **arg,
3521 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003522 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003523 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003525 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003528 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003530 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 return FAIL;
3532
3533 /*
3534 * Repeat computing, until no '*', '/' or '%' is following.
3535 */
3536 for (;;)
3537 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003538 int evaluate;
3539 int getnext;
3540 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003541 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003542 int op;
3543 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003544 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003545 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003546
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003547 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003548 p = eval_next_non_blank(*arg, evalarg, &getnext);
3549 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003550 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003552
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003553 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003554 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003555 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003556 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003557 {
3558 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3559 {
mityu4ac198c2021-05-28 17:52:40 +02003560 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003561 clear_tv(rettv);
3562 return FAIL;
3563 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003564 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003567 f1 = 0;
3568 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003569 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003570 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 if (rettv->v_type == VAR_FLOAT)
3573 {
3574 f1 = rettv->vval.v_float;
3575 use_float = TRUE;
3576 n1 = 0;
3577 }
3578 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003579 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 if (error)
3582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 else
3585 n1 = 0;
3586
3587 /*
3588 * Get the second variable.
3589 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003590 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3591 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003592 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003593 clear_tv(rettv);
3594 return FAIL;
3595 }
3596 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003597 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 return FAIL;
3599
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003600 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003602 if (var2.v_type == VAR_FLOAT)
3603 {
3604 if (!use_float)
3605 {
3606 f1 = n1;
3607 use_float = TRUE;
3608 }
3609 f2 = var2.vval.v_float;
3610 n2 = 0;
3611 }
3612 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003614 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003615 clear_tv(&var2);
3616 if (error)
3617 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003618 if (use_float)
3619 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 /*
3623 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003624 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003626 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003628 if (op == '*')
3629 f1 = f1 * f2;
3630 else if (op == '/')
3631 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003632#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003633 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003634 if (f2 == 0.0)
3635 {
3636 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003637 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003638 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003639 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003640 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003641 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003642 }
3643 else
3644 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003645#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003646 // We rely on the floating point library to handle divide
3647 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003648 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003649#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003652 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003653 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003654 return FAIL;
3655 }
3656 rettv->v_type = VAR_FLOAT;
3657 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 else
3660 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003661 int failed = FALSE;
3662
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003663 if (op == '*')
3664 n1 = n1 * n2;
3665 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003666 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003668 n1 = num_modulus(n1, n2, &failed);
3669 if (failed)
3670 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003672 rettv->v_type = VAR_NUMBER;
3673 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 }
3677
3678 return OK;
3679}
3680
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003681/*
3682 * Handle a type cast before a base level expression.
3683 * "arg" must point to the first non-white of the expression.
3684 * "arg" is advanced to just after the recognized expression.
3685 * Return OK or FAIL.
3686 */
3687 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003688eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003689 char_u **arg,
3690 typval_T *rettv,
3691 evalarg_T *evalarg,
3692 int want_string) // after "." operator
3693{
3694 type_T *want_type = NULL;
3695 garray_T type_list; // list of pointers to allocated types
3696 int res;
3697 int evaluate = evalarg == NULL ? 0
3698 : (evalarg->eval_flags & EVAL_EVALUATE);
3699
3700 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003701 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3702 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003703 {
3704 ++*arg;
3705 ga_init2(&type_list, sizeof(type_T *), 10);
3706 want_type = parse_type(arg, &type_list, TRUE);
3707 if (want_type == NULL && (evaluate || **arg != '>'))
3708 {
3709 clear_type_list(&type_list);
3710 return FAIL;
3711 }
3712
3713 if (**arg != '>')
3714 {
3715 if (*skipwhite(*arg) == '>')
3716 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3717 else
3718 emsg(_(e_missing_gt));
3719 clear_type_list(&type_list);
3720 return FAIL;
3721 }
3722 ++*arg;
3723 *arg = skipwhite_and_linebreak(*arg, evalarg);
3724 }
3725
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003726 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003727
3728 if (want_type != NULL && evaluate)
3729 {
3730 if (res == OK)
3731 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003732 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3733 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003734
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003735 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003736 {
3737 if (want_type == &t_bool && actual != &t_bool
3738 && (actual->tt_flags & TTFLAG_BOOL_OK))
3739 {
3740 int n = tv2bool(rettv);
3741
3742 // can use "0" and "1" for boolean in some places
3743 clear_tv(rettv);
3744 rettv->v_type = VAR_BOOL;
3745 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3746 }
3747 else
3748 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003749 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003750
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003751 where.wt_variable = TRUE;
3752 res = check_type(want_type, actual, TRUE, where);
3753 }
3754 }
3755 }
3756 clear_type_list(&type_list);
3757 }
3758
3759 return res;
3760}
3761
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003762 int
3763eval_leader(char_u **arg, int vim9)
3764{
3765 char_u *s = *arg;
3766 char_u *p = *arg;
3767
3768 while (*p == '!' || *p == '-' || *p == '+')
3769 {
3770 char_u *n = skipwhite(p + 1);
3771
3772 // ++, --, -+ and +- are not accepted in Vim9 script
3773 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3774 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003775 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003776 return FAIL;
3777 }
3778 p = n;
3779 }
3780 *arg = p;
3781 return OK;
3782}
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003785 * Check for a predefined value "true", "false" and "null.*".
3786 * Return OK when recognized.
3787 */
3788 int
3789handle_predefined(char_u *s, int len, typval_T *rettv)
3790{
3791 switch (len)
3792 {
3793 case 4: if (STRNCMP(s, "true", 4) == 0)
3794 {
3795 rettv->v_type = VAR_BOOL;
3796 rettv->vval.v_number = VVAL_TRUE;
3797 return OK;
3798 }
3799 if (STRNCMP(s, "null", 4) == 0)
3800 {
3801 rettv->v_type = VAR_SPECIAL;
3802 rettv->vval.v_number = VVAL_NULL;
3803 return OK;
3804 }
3805 break;
3806 case 5: if (STRNCMP(s, "false", 5) == 0)
3807 {
3808 rettv->v_type = VAR_BOOL;
3809 rettv->vval.v_number = VVAL_FALSE;
3810 return OK;
3811 }
3812 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003813 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3814 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003815#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003816 rettv->v_type = VAR_JOB;
3817 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003818#else
3819 rettv->v_type = VAR_SPECIAL;
3820 rettv->vval.v_number = VVAL_NULL;
3821#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003822 return OK;
3823 }
3824 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003825 case 9:
3826 if (STRNCMP(s, "null_", 5) != 0)
3827 break;
3828 if (STRNCMP(s + 5, "list", 4) == 0)
3829 {
3830 rettv->v_type = VAR_LIST;
3831 rettv->vval.v_list = NULL;
3832 return OK;
3833 }
3834 if (STRNCMP(s + 5, "dict", 4) == 0)
3835 {
3836 rettv->v_type = VAR_DICT;
3837 rettv->vval.v_dict = NULL;
3838 return OK;
3839 }
3840 if (STRNCMP(s + 5, "blob", 4) == 0)
3841 {
3842 rettv->v_type = VAR_BLOB;
3843 rettv->vval.v_blob = NULL;
3844 return OK;
3845 }
3846 break;
3847 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3848 {
3849 rettv->v_type = VAR_STRING;
3850 rettv->vval.v_string = NULL;
3851 return OK;
3852 }
3853 break;
3854 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003855 if (STRNCMP(s, "null_channel", 12) == 0)
3856 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003857#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003858 rettv->v_type = VAR_CHANNEL;
3859 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003860#else
3861 rettv->v_type = VAR_SPECIAL;
3862 rettv->vval.v_number = VVAL_NULL;
3863#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003864 return OK;
3865 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003866 if (STRNCMP(s, "null_partial", 12) == 0)
3867 {
3868 rettv->v_type = VAR_PARTIAL;
3869 rettv->vval.v_partial = NULL;
3870 return OK;
3871 }
3872 break;
3873 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3874 {
3875 rettv->v_type = VAR_FUNC;
3876 rettv->vval.v_string = NULL;
3877 return OK;
3878 }
3879 break;
3880 }
3881 return FAIL;
3882}
3883
3884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * Handle sixth level expression:
3886 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003887 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003888 * "string" string constant
3889 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * &option-name option value
3891 * @r register contents
3892 * identifier variable value
3893 * function() function call
3894 * $VAR environment variable
3895 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003896 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003898 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003899 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 *
3901 * Also handle:
3902 * ! in front logical NOT
3903 * - in front unary minus
3904 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003905 * trailing [] subscript in String or List
3906 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003907 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 *
3909 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003910 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 *
3912 * Return OK or FAIL.
3913 */
3914 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003915eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003916 char_u **arg,
3917 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003918 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003921 int evaluate = evalarg != NULL
3922 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 int len;
3924 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003925 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 char_u *start_leader, *end_leader;
3927 int ret = OK;
3928 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003929 static int recurse = 0;
3930 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003934 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003939 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 */
3941 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003942 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 end_leader = *arg;
3945
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003946 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003947 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003948 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003949 ++*arg;
3950 return FAIL;
3951 }
3952
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003953 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003954 // and crash. With MSVC the stack is smaller.
3955 if (recurse ==
3956#ifdef _MSC_VER
3957 300
3958#else
3959 1000
3960#endif
3961 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003962 {
3963 semsg(_(e_expression_too_recursive_str), *arg);
3964 return FAIL;
3965 }
3966 ++recurse;
3967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 switch (**arg)
3969 {
3970 /*
3971 * Number constant.
3972 */
3973 case '0':
3974 case '1':
3975 case '2':
3976 case '3':
3977 case '4':
3978 case '5':
3979 case '6':
3980 case '7':
3981 case '8':
3982 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003983 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003984
3985 // Apply prefixed "-" and "+" now. Matters especially when
3986 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003987 if (ret == OK && evaluate && end_leader > start_leader
3988 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003989 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 break;
3991
3992 /*
3993 * String constant: "string".
3994 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003995 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 break;
3997
3998 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003999 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004001 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004002 break;
4003
4004 /*
4005 * List: [expr, expr]
4006 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004007 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 break;
4009
4010 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004011 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004012 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004013 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004014 {
4015 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4016 }
4017 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004018 {
4019 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004020 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004021 }
4022 else
4023 ret = NOTDONE;
4024 break;
4025
4026 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004027 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004028 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004029 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004030 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004031 ret = NOTDONE;
4032 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004033 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004034 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004035 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 break;
4037
4038 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004039 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004041 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 break;
4043
4044 /*
4045 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004046 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
LemonBoy2eaef102022-05-06 13:14:50 +01004048 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4049 ret = eval_interp_string(arg, rettv, evaluate);
4050 else
4051 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 break;
4053
4054 /*
4055 * Register contents: @r.
4056 */
4057 case '@': ++*arg;
4058 if (evaluate)
4059 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004060 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004061 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004062 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004063 emsg_invreg(**arg);
4064 else
4065 {
4066 rettv->v_type = VAR_STRING;
4067 rettv->vval.v_string = get_reg_contents(**arg,
4068 GREG_EXPR_SRC);
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 if (**arg != NUL)
4072 ++*arg;
4073 break;
4074
4075 /*
4076 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004077 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004079 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004080 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004081 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004082 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004083 if (ret == OK && evaluate)
4084 {
4085 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4086
Bram Moolenaara9931532021-06-12 15:58:16 +02004087 // Compile it here to get the return type. The return
4088 // type is optional, when it's missing use t_unknown.
4089 // This is recognized in compile_return().
4090 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4091 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004092 if (compile_def_function(ufunc, FALSE,
4093 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004094 {
4095 clear_tv(rettv);
4096 ret = FAIL;
4097 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004098 }
4099 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004100 if (ret == NOTDONE)
4101 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004102 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004103 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004104
Bram Moolenaar9215f012020-06-27 21:18:00 +02004105 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004106 if (**arg == ')')
4107 ++*arg;
4108 else if (ret == OK)
4109 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004110 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004111 clear_tv(rettv);
4112 ret = FAIL;
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 break;
4116
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120
4121 if (ret == NOTDONE)
4122 {
4123 /*
4124 * Must be a variable or function name.
4125 * Can also be a curly-braces kind of name: {expr}.
4126 */
4127 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004128 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004129 if (alias != NULL)
4130 s = alias;
4131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004132 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 ret = FAIL;
4134 else
4135 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004136 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4137
Bram Moolenaar4525a572022-02-13 11:57:33 +00004138 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004139 {
4140 emsg(_(e_cannot_use_underscore_here));
4141 ret = FAIL;
4142 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004143 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004144 && s[0] == 's' && s[1] == ':')
4145 {
4146 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4147 ret = FAIL;
4148 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004149 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004150 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004151 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004152 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004153 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004154 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004155 else if (flags & EVAL_CONSTANT)
4156 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004157 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004158 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004159 // get the value of "true", "false", etc. or a variable
4160 ret = FAIL;
4161 if (vim9script)
4162 ret = handle_predefined(s, len, rettv);
4163 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004164 {
4165 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004166 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004167 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004168 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004170 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004171 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004172 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004173 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004174 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004176 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004177 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004178 }
4179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4181 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004182 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004183 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185 /*
4186 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4187 */
4188 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004189 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004190
4191 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004192 return ret;
4193}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004195/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004196 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004197 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004198 * Adjusts "end_leaderp" until it is at "start_leader".
4199 */
4200 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004201eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004202 typval_T *rettv,
4203 int numeric_only,
4204 char_u *start_leader,
4205 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004206{
4207 char_u *end_leader = *end_leaderp;
4208 int ret = OK;
4209 int error = FALSE;
4210 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004211 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004212 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004213 float_T f = 0.0;
4214
4215 if (rettv->v_type == VAR_FLOAT)
4216 f = rettv->vval.v_float;
4217 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004218 {
4219 while (VIM_ISWHITE(end_leader[-1]))
4220 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004221 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004222 val = tv2bool(rettv);
4223 else
4224 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004225 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004226 if (error)
4227 {
4228 clear_tv(rettv);
4229 ret = FAIL;
4230 }
4231 else
4232 {
4233 while (end_leader > start_leader)
4234 {
4235 --end_leader;
4236 if (*end_leader == '!')
4237 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004238 if (numeric_only)
4239 {
4240 ++end_leader;
4241 break;
4242 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004243 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004244 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004245 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004246 {
4247 rettv->v_type = VAR_BOOL;
4248 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4249 }
4250 else
4251 f = !f;
4252 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004253 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004254 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004255 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004256 type = VAR_BOOL;
4257 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004258 }
4259 else if (*end_leader == '-')
4260 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004261 if (rettv->v_type == VAR_FLOAT)
4262 f = -f;
4263 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004264 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004265 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004266 type = VAR_NUMBER;
4267 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004268 }
4269 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004270 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004273 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 else
4276 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004277 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004278 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004279 rettv->v_type = type;
4280 else
4281 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004282 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004285 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 return ret;
4287}
4288
4289/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004290 * Call the function referred to in "rettv".
4291 */
4292 static int
4293call_func_rettv(
4294 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004295 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004296 typval_T *rettv,
4297 int evaluate,
4298 dict_T *selfdict,
4299 typval_T *basetv)
4300{
4301 partial_T *pt = NULL;
4302 funcexe_T funcexe;
4303 typval_T functv;
4304 char_u *s;
4305 int ret;
4306
4307 // need to copy the funcref so that we can clear rettv
4308 if (evaluate)
4309 {
4310 functv = *rettv;
4311 rettv->v_type = VAR_UNKNOWN;
4312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004313 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004314 if (functv.v_type == VAR_PARTIAL)
4315 {
4316 pt = functv.vval.v_partial;
4317 s = partial_name(pt);
4318 }
4319 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004320 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004321 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004322 if (s == NULL || *s == NUL)
4323 {
4324 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004325 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004326 goto theend;
4327 }
4328 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004329 }
4330 else
4331 s = (char_u *)"";
4332
Bram Moolenaara80faa82020-04-12 19:37:17 +02004333 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004334 funcexe.fe_firstline = curwin->w_cursor.lnum;
4335 funcexe.fe_lastline = curwin->w_cursor.lnum;
4336 funcexe.fe_evaluate = evaluate;
4337 funcexe.fe_partial = pt;
4338 funcexe.fe_selfdict = selfdict;
4339 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004340 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004341
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004342theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004343 // Clear the funcref afterwards, so that deleting it while
4344 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004345 if (evaluate)
4346 clear_tv(&functv);
4347
4348 return ret;
4349}
4350
4351/*
4352 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004353 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004354 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4355 */
4356 static int
4357eval_lambda(
4358 char_u **arg,
4359 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004360 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004361 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004362{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004363 int evaluate = evalarg != NULL
4364 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004365 typval_T base = *rettv;
4366 int ret;
4367
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004368 rettv->v_type = VAR_UNKNOWN;
4369
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004370 if (**arg == '{')
4371 {
4372 // ->{lambda}()
4373 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4374 }
4375 else
4376 {
4377 // ->(lambda)()
4378 ++*arg;
4379 ret = eval1(arg, rettv, evalarg);
4380 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004381 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004382 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004383 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004384 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004385 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004386 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4387 && rettv->v_type != VAR_PARTIAL)
4388 {
4389 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4390 return FAIL;
4391 }
4392 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004393 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004394 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004395 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004396
4397 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004398 {
4399 if (verbose)
4400 {
4401 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004402 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004403 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004404 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004405 }
4406 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004407 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004408 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004409 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004410 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004411
4412 // Clear the funcref afterwards, so that deleting it while
4413 // evaluating the arguments is possible (see test55).
4414 if (evaluate)
4415 clear_tv(&base);
4416
4417 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004418}
4419
4420/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004421 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004422 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004423 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4424 */
4425 static int
4426eval_method(
4427 char_u **arg,
4428 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004429 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004430 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004431{
4432 char_u *name;
4433 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004434 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004435 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004436 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004437 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004438 int evaluate = evalarg != NULL
4439 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004440
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004441 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004442
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004444 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004445 if (alias != NULL)
4446 name = alias;
4447
4448 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004449 {
4450 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004451 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004452 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004453 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004454 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004455 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004456 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004457
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004458 // If there is no "(" immediately following, but there is further on,
4459 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4460 // Does not handle anything where "(" is part of the expression.
4461 *arg = skipwhite(*arg);
4462
4463 if (**arg != '(' && alias == NULL
4464 && (paren = vim_strchr(*arg, '(')) != NULL)
4465 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004466 char_u *deref;
4467
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004468 *arg = name;
4469 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004470 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4471 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004472 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004473 *arg = name + len;
4474 ret = FAIL;
4475 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004476 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004477 {
4478 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004479 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004480 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004481 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004482 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004483
4484 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004485 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004486 *arg = skipwhite(*arg);
4487
4488 if (**arg != '(')
4489 {
4490 if (verbose)
4491 semsg(_(e_missing_parenthesis_str), name);
4492 ret = FAIL;
4493 }
4494 else if (VIM_ISWHITE((*arg)[-1]))
4495 {
4496 if (verbose)
4497 emsg(_(e_no_white_space_allowed_before_parenthesis));
4498 ret = FAIL;
4499 }
4500 else
4501 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004502 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004503 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004504 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004505
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004506 // Clear the funcref afterwards, so that deleting it while
4507 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004508 if (evaluate)
4509 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004510 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004511
Bram Moolenaarac92e252019-08-03 21:58:38 +02004512 return ret;
4513}
4514
4515/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004516 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4517 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004518 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4519 */
4520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004521eval_index(
4522 char_u **arg,
4523 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004524 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004527 int evaluate = evalarg != NULL
4528 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004530 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004531 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004532 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004533 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004534 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004536 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4537 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004539 init_tv(&var1);
4540 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004543 /*
4544 * dict.name
4545 */
4546 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004547 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004549 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004551 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 }
4553 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 /*
4556 * something[idx]
4557 *
4558 * Get the (first) variable from inside the [].
4559 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004560 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 if (**arg == ':')
4562 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004563 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004564 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004565 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004566 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004567 semsg(_(e_white_space_required_before_and_after_str_at_str),
4568 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004569 clear_tv(&var1);
4570 return FAIL;
4571 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004572 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004574 int error = FALSE;
4575
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004576 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004577 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004578 && var1.v_type == VAR_FLOAT)
4579 {
4580 var1.vval.v_string = typval_tostring(&var1, TRUE);
4581 var1.v_type = VAR_STRING;
4582 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004583
Bram Moolenaar4525a572022-02-13 11:57:33 +00004584 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004585 tv_get_number_chk(&var1, &error);
4586 else
4587 error = tv_get_string_chk(&var1) == NULL;
4588 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004589 {
4590 // not a number or string
4591 clear_tv(&var1);
4592 return FAIL;
4593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595
4596 /*
4597 * Get the second variable from inside the [:].
4598 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004599 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 if (**arg == ':')
4601 {
4602 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004603 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004604 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004605 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004606 semsg(_(e_white_space_required_before_and_after_str_at_str),
4607 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004608 if (!empty1)
4609 clear_tv(&var1);
4610 return FAIL;
4611 }
4612 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 if (**arg == ']')
4614 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004615 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004617 if (!empty1)
4618 clear_tv(&var1);
4619 return FAIL;
4620 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004624 if (!empty1)
4625 clear_tv(&var1);
4626 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 return FAIL;
4628 }
4629 }
4630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004632 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 if (**arg != ']')
4634 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004635 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004636 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 clear_tv(&var1);
4638 if (range)
4639 clear_tv(&var2);
4640 return FAIL;
4641 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004642 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004643 }
4644
4645 if (evaluate)
4646 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004647 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004648 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004649 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004650
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004651 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004653 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004654 clear_tv(&var2);
4655 return res;
4656 }
4657 return OK;
4658}
4659
4660/*
4661 * Check if "rettv" can have an [index] or [sli:ce]
4662 */
4663 int
4664check_can_index(typval_T *rettv, int evaluate, int verbose)
4665{
4666 switch (rettv->v_type)
4667 {
4668 case VAR_FUNC:
4669 case VAR_PARTIAL:
4670 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004671 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004672 return FAIL;
4673 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004675 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004676 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004677 case VAR_BOOL:
4678 case VAR_SPECIAL:
4679 case VAR_JOB:
4680 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004681 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004682 if (verbose)
4683 emsg(_(e_cannot_index_special_variable));
4684 return FAIL;
4685 case VAR_UNKNOWN:
4686 case VAR_ANY:
4687 case VAR_VOID:
4688 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004690 emsg(_(e_cannot_index_special_variable));
4691 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004693 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004695 case VAR_STRING:
4696 case VAR_LIST:
4697 case VAR_DICT:
4698 case VAR_BLOB:
4699 break;
4700 case VAR_NUMBER:
4701 if (in_vim9script())
4702 emsg(_(e_cannot_index_number));
4703 break;
4704 }
4705 return OK;
4706}
4707
4708/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004709 * slice() function
4710 */
4711 void
4712f_slice(typval_T *argvars, typval_T *rettv)
4713{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004714 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004715 && ((argvars[0].v_type != VAR_STRING
4716 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004717 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004718 && check_for_list_arg(argvars, 0) == FAIL)
4719 || check_for_number_arg(argvars, 1) == FAIL
4720 || check_for_opt_number_arg(argvars, 2) == FAIL))
4721 return;
4722
Bram Moolenaar6601b622021-01-13 21:47:15 +01004723 if (check_can_index(argvars, TRUE, FALSE) == OK)
4724 {
4725 copy_tv(argvars, rettv);
4726 eval_index_inner(rettv, TRUE, argvars + 1,
4727 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4728 TRUE, NULL, 0, FALSE);
4729 }
4730}
4731
4732/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004733 * Apply index or range to "rettv".
4734 * "var1" is the first index, NULL for [:expr].
4735 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004736 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4737 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004738 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4739 */
4740 int
4741eval_index_inner(
4742 typval_T *rettv,
4743 int is_range,
4744 typval_T *var1,
4745 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004746 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004747 char_u *key,
4748 int keylen,
4749 int verbose)
4750{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004751 varnumber_T n1, n2 = 0;
4752 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004753
4754 n1 = 0;
4755 if (var1 != NULL && rettv->v_type != VAR_DICT)
4756 n1 = tv_get_number(var1);
4757
4758 if (is_range)
4759 {
4760 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004762 if (verbose)
4763 emsg(_(e_cannot_slice_dictionary));
4764 return FAIL;
4765 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004766 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004767 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004768 else
4769 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 switch (rettv->v_type)
4773 {
4774 case VAR_UNKNOWN:
4775 case VAR_ANY:
4776 case VAR_VOID:
4777 case VAR_FUNC:
4778 case VAR_PARTIAL:
4779 case VAR_FLOAT:
4780 case VAR_BOOL:
4781 case VAR_SPECIAL:
4782 case VAR_JOB:
4783 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004784 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004785 break; // not evaluating, skipping over subscript
4786
4787 case VAR_NUMBER:
4788 case VAR_STRING:
4789 {
4790 char_u *s = tv_get_string(rettv);
4791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004793 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004794 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004795 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004796 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004797 else
4798 s = char_from_string(s, n1);
4799 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004800 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004802 // The resulting variable is a substring. If the indexes
4803 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 if (n1 < 0)
4805 {
4806 n1 = len + n1;
4807 if (n1 < 0)
4808 n1 = 0;
4809 }
4810 if (n2 < 0)
4811 n2 = len + n2;
4812 else if (n2 >= len)
4813 n2 = len;
4814 if (n1 >= len || n2 < 0 || n1 > n2)
4815 s = NULL;
4816 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004817 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 }
4819 else
4820 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // The resulting variable is a string of a single
4822 // character. If the index is too big or negative the
4823 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 if (n1 >= len || n1 < 0)
4825 s = NULL;
4826 else
4827 s = vim_strnsave(s + n1, 1);
4828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
4830 rettv->v_type = VAR_STRING;
4831 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004832 }
4833 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004835 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004836 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4837 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004838 break;
4839
4840 case VAR_LIST:
4841 if (var1 == NULL)
4842 n1 = 0;
4843 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004844 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004845 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004846 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004847 return FAIL;
4848 break;
4849
4850 case VAR_DICT:
4851 {
4852 dictitem_T *item;
4853 typval_T tmp;
4854
4855 if (key == NULL)
4856 {
4857 key = tv_get_string_chk(var1);
4858 if (key == NULL)
4859 return FAIL;
4860 }
4861
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004862 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004863
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004864 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004865 {
4866 if (verbose)
4867 {
4868 if (keylen > 0)
4869 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004870 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004871 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004872 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004873 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004874
4875 copy_tv(&item->di_tv, &tmp);
4876 clear_tv(rettv);
4877 *rettv = tmp;
4878 }
4879 break;
4880 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 return OK;
4882}
4883
4884/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004885 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004886 */
4887 char_u *
4888partial_name(partial_T *pt)
4889{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004890 if (pt != NULL)
4891 {
4892 if (pt->pt_name != NULL)
4893 return pt->pt_name;
4894 if (pt->pt_func != NULL)
4895 return pt->pt_func->uf_name;
4896 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004897 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004898}
4899
Bram Moolenaarddecc252016-04-06 22:59:37 +02004900 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004902{
4903 int i;
4904
4905 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004906 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004907 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004909 if (pt->pt_name != NULL)
4910 {
4911 func_unref(pt->pt_name);
4912 vim_free(pt->pt_name);
4913 }
4914 else
4915 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004916
Bram Moolenaar54656012021-06-09 20:50:46 +02004917 // "out_up" is no longer used, decrement refcount on partial that owns it.
4918 partial_unref(pt->pt_outer.out_up_partial);
4919
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004920 // Using pt_outer from another partial.
4921 partial_unref(pt->pt_outer_partial);
4922
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004923 // Decrease the reference count for the context of a closure. If down
4924 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004925 if (pt->pt_funcstack != NULL)
4926 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004927 --pt->pt_funcstack->fs_refcount;
4928 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004929 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004930 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004931 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4932 if (pt->pt_loopvars[i] != NULL)
4933 {
4934 --pt->pt_loopvars[i]->lvs_refcount;
4935 loopvars_check_refcount(pt->pt_loopvars[i]);
4936 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004937
Bram Moolenaarddecc252016-04-06 22:59:37 +02004938 vim_free(pt);
4939}
4940
4941/*
4942 * Unreference a closure: decrement the reference count and free it when it
4943 * becomes zero.
4944 */
4945 void
4946partial_unref(partial_T *pt)
4947{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004948 if (pt != NULL)
4949 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004950 int done = FALSE;
4951
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004952 if (--pt->pt_refcount <= 0)
4953 partial_free(pt);
4954
4955 // If the reference count goes down to one, the funcstack may be the
4956 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004957 else if (pt->pt_refcount == 1)
4958 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004959 // careful: if the funcstack is freed it may contain this partial
4960 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004961 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004962 done = funcstack_check_refcount(pt->pt_funcstack);
4963
Bram Moolenaarcc341812022-09-19 15:54:34 +01004964 if (!done)
4965 {
4966 int depth;
4967
4968 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4969 if (pt->pt_loopvars[depth] != NULL
4970 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4971 break;
4972 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004973 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004974 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004975}
4976
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004977/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004978 * Return the next (unique) copy ID.
4979 * Used for serializing nested structures.
4980 */
4981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004982get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004983{
4984 current_copyID += COPYID_INC;
4985 return current_copyID;
4986}
4987
4988/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004989 * Garbage collection for lists and dictionaries.
4990 *
4991 * We use reference counts to be able to free most items right away when they
4992 * are no longer used. But for composite items it's possible that it becomes
4993 * unused while the reference count is > 0: When there is a recursive
4994 * reference. Example:
4995 * :let l = [1, 2, 3]
4996 * :let d = {9: l}
4997 * :let l[1] = d
4998 *
4999 * Since this is quite unusual we handle this with garbage collection: every
5000 * once in a while find out which lists and dicts are not referenced from any
5001 * variable.
5002 *
5003 * Here is a good reference text about garbage collection (refers to Python
5004 * but it applies to all reference-counting mechanisms):
5005 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005006 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005007
5008/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005009 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005010 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005011 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005012 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005013 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005014garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005015{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005016 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005018 buf_T *buf;
5019 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005020 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005021 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005022
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005023 if (!testing)
5024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005026 want_garbage_collect = FALSE;
5027 may_garbage_collect = FALSE;
5028 garbage_collect_at_exit = FALSE;
5029 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005030
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005031 // The execution stack can grow big, limit the size.
5032 if (exestack.ga_maxlen - exestack.ga_len > 500)
5033 {
5034 size_t new_len;
5035 char_u *pp;
5036 int n;
5037
5038 // Keep 150% of the current size, with a minimum of the growth size.
5039 n = exestack.ga_len / 2;
5040 if (n < exestack.ga_growsize)
5041 n = exestack.ga_growsize;
5042
5043 // Don't make it bigger though.
5044 if (exestack.ga_len + n < exestack.ga_maxlen)
5045 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005046 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005047 pp = vim_realloc(exestack.ga_data, new_len);
5048 if (pp == NULL)
5049 return FAIL;
5050 exestack.ga_maxlen = exestack.ga_len + n;
5051 exestack.ga_data = pp;
5052 }
5053 }
5054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 // We advance by two because we add one for items referenced through
5056 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005057 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005058
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005059 /*
5060 * 1. Go through all accessible variables and mark all lists and dicts
5061 * with copyID.
5062 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // Don't free variables in the previous_funccal list unless they are only
5065 // referenced through previous_funccal. This must be first, because if
5066 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005067 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005070 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005073 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005074 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5075 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005077 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005079 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5080 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005081 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005082 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5083 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005084#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005085 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005086 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5087 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005088 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005089 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005090 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5091 NULL, NULL);
5092#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005095 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005096 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5097 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005099 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005102 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005105 abort = abort || set_ref_in_functions(copyID);
5106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005108 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005109
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005110 // funcstacks keep variables for closures
5111 abort = abort || set_ref_in_funcstacks(copyID);
5112
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005113 // loopvars keep variables for loop blocks
5114 abort = abort || set_ref_in_loopvars(copyID);
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005117 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005118
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005119 // callbacks in buffers
5120 abort = abort || set_ref_in_buffers(copyID);
5121
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005122 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5123 abort = abort || set_ref_in_insexpand_funcs(copyID);
5124
5125 // 'operatorfunc' callback
5126 abort = abort || set_ref_in_opfunc(copyID);
5127
5128 // 'tagfunc' callback
5129 abort = abort || set_ref_in_tagfunc(copyID);
5130
5131 // 'imactivatefunc' and 'imstatusfunc' callbacks
5132 abort = abort || set_ref_in_im_funcs(copyID);
5133
Bram Moolenaar1dced572012-04-05 16:54:08 +02005134#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005135 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005136#endif
5137
Bram Moolenaardb913952012-06-29 12:54:53 +02005138#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005139 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005140#endif
5141
5142#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005143 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005144#endif
5145
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005147 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005148 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005149#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005150#ifdef FEAT_NETBEANS_INTG
5151 abort = abort || set_ref_in_nb_channel(copyID);
5152#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005153
Bram Moolenaare3188e22016-05-31 21:13:04 +02005154#ifdef FEAT_TIMERS
5155 abort = abort || set_ref_in_timer(copyID);
5156#endif
5157
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005158#ifdef FEAT_QUICKFIX
5159 abort = abort || set_ref_in_quickfix(copyID);
5160#endif
5161
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005162#ifdef FEAT_TERMINAL
5163 abort = abort || set_ref_in_term(copyID);
5164#endif
5165
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005166#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005167 abort = abort || set_ref_in_popups(copyID);
5168#endif
5169
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005171 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005172 /*
5173 * 2. Free lists and dictionaries that are not referenced.
5174 */
5175 did_free = free_unref_items(copyID);
5176
5177 /*
5178 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005180 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005182 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005183 else if (p_verbose > 0)
5184 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005185 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005186 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005187
5188 return did_free;
5189}
5190
5191/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005192 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005193 */
5194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005195free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005196{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005197 int did_free = FALSE;
5198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // Let all "free" functions know that we are here. This means no
5200 // dictionaries, lists, channels or jobs are to be freed, because we will
5201 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005202 in_free_unref_items = TRUE;
5203
5204 /*
5205 * PASS 1: free the contents of the items. We don't free the items
5206 * themselves yet, so that it is possible to decrement refcount counters
5207 */
5208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005210 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005213 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005214
5215#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // Go through the list of jobs and free items without the copyID. This
5217 // must happen before doing channels, because jobs refer to channels, but
5218 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005219 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005222 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5223#endif
5224
5225 /*
5226 * PASS 2: free the items themselves.
5227 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005228 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005229 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005230
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Go through the list of jobs and free items without the copyID. This
5233 // must happen before doing channels, because jobs refer to channels, but
5234 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005235 free_unused_jobs(copyID, COPYID_MASK);
5236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005238 free_unused_channels(copyID, COPYID_MASK);
5239#endif
5240
5241 in_free_unref_items = FALSE;
5242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005243 return did_free;
5244}
5245
5246/*
5247 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 * "list_stack" is used to add lists to be marked. Can be NULL.
5249 *
5250 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005253set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254{
5255 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005256 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005258 hashtab_T *cur_ht;
5259 ht_stack_T *ht_stack = NULL;
5260 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005261
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005262 cur_ht = ht;
5263 for (;;)
5264 {
5265 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 // Mark each item in the hashtab. If the item contains a hashtab
5268 // it is added to ht_stack, if it contains a list it is added to
5269 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 todo = (int)cur_ht->ht_used;
5271 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5272 if (!HASHITEM_EMPTY(hi))
5273 {
5274 --todo;
5275 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5276 &ht_stack, list_stack);
5277 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005278 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005279
5280 if (ht_stack == NULL)
5281 break;
5282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005284 cur_ht = ht_stack->ht;
5285 tempitem = ht_stack;
5286 ht_stack = ht_stack->prev;
5287 free(tempitem);
5288 }
5289
5290 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291}
5292
Dominique Pelle748b3082022-01-08 12:41:16 +00005293#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5294 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005295/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005296 * Mark a dict and its items with "copyID".
5297 * Returns TRUE if setting references failed somehow.
5298 */
5299 int
5300set_ref_in_dict(dict_T *d, int copyID)
5301{
5302 if (d != NULL && d->dv_copyID != copyID)
5303 {
5304 d->dv_copyID = copyID;
5305 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5306 }
5307 return FALSE;
5308}
Dominique Pelle748b3082022-01-08 12:41:16 +00005309#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005310
5311/*
5312 * Mark a list and its items with "copyID".
5313 * Returns TRUE if setting references failed somehow.
5314 */
5315 int
5316set_ref_in_list(list_T *ll, int copyID)
5317{
5318 if (ll != NULL && ll->lv_copyID != copyID)
5319 {
5320 ll->lv_copyID = copyID;
5321 return set_ref_in_list_items(ll, copyID, NULL);
5322 }
5323 return FALSE;
5324}
5325
5326/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005328 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5329 *
5330 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005331 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005332 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005333set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005334{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 listitem_T *li;
5336 int abort = FALSE;
5337 list_T *cur_l;
5338 list_stack_T *list_stack = NULL;
5339 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005340
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005341 cur_l = l;
5342 for (;;)
5343 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005344 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 // Mark each item in the list. If the item contains a hashtab
5346 // it is added to ht_stack, if it contains a list it is added to
5347 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005348 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5349 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5350 ht_stack, &list_stack);
5351 if (list_stack == NULL)
5352 break;
5353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005355 cur_l = list_stack->list;
5356 tempitem = list_stack;
5357 list_stack = list_stack->prev;
5358 free(tempitem);
5359 }
5360
5361 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362}
5363
5364/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005365 * Mark the partial in callback 'cb' with "copyID".
5366 */
5367 int
5368set_ref_in_callback(callback_T *cb, int copyID)
5369{
5370 typval_T tv;
5371
5372 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5373 return FALSE;
5374
5375 tv.v_type = VAR_PARTIAL;
5376 tv.vval.v_partial = cb->cb_partial;
5377 return set_ref_in_item(&tv, copyID, NULL, NULL);
5378}
5379
5380/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005382 * "list_stack" is used to add lists to be marked. Can be NULL.
5383 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5384 *
5385 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005386 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005388set_ref_in_item(
5389 typval_T *tv,
5390 int copyID,
5391 ht_stack_T **ht_stack,
5392 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005393{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005394 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005395
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005396 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005397 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398 dict_T *dd = tv->vval.v_dict;
5399
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 if (dd != NULL && dd->dv_copyID != copyID)
5401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 dd->dv_copyID = copyID;
5404 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005405 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005406 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5407 }
5408 else
5409 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005410 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 if (newitem == NULL)
5413 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005414 else
5415 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 newitem->ht = &dd->dv_hashtab;
5417 newitem->prev = *ht_stack;
5418 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005419 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005420 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 }
5422 }
5423 else if (tv->v_type == VAR_LIST)
5424 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005425 list_T *ll = tv->vval.v_list;
5426
Bram Moolenaara03f2332016-02-06 18:09:59 +01005427 if (ll != NULL && ll->lv_copyID != copyID)
5428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005430 ll->lv_copyID = copyID;
5431 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005432 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005433 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005434 }
5435 else
5436 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005437 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5438
Bram Moolenaara03f2332016-02-06 18:09:59 +01005439 if (newitem == NULL)
5440 abort = TRUE;
5441 else
5442 {
5443 newitem->list = ll;
5444 newitem->prev = *list_stack;
5445 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005447 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005448 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005449 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005450 else if (tv->v_type == VAR_FUNC)
5451 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005452 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005453 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005454 else if (tv->v_type == VAR_PARTIAL)
5455 {
5456 partial_T *pt = tv->vval.v_partial;
5457 int i;
5458
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005459 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005460 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005461 // Didn't see this partial yet.
5462 pt->pt_copyID = copyID;
5463
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005464 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005465
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005466 if (pt->pt_dict != NULL)
5467 {
5468 typval_T dtv;
5469
5470 dtv.v_type = VAR_DICT;
5471 dtv.vval.v_dict = pt->pt_dict;
5472 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5473 }
5474
5475 for (i = 0; i < pt->pt_argc; ++i)
5476 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5477 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005478 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005479 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005480 }
5481 }
5482#ifdef FEAT_JOB_CHANNEL
5483 else if (tv->v_type == VAR_JOB)
5484 {
5485 job_T *job = tv->vval.v_job;
5486 typval_T dtv;
5487
5488 if (job != NULL && job->jv_copyID != copyID)
5489 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005490 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005491 if (job->jv_channel != NULL)
5492 {
5493 dtv.v_type = VAR_CHANNEL;
5494 dtv.vval.v_channel = job->jv_channel;
5495 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5496 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005497 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005498 {
5499 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005500 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005501 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5502 }
5503 }
5504 }
5505 else if (tv->v_type == VAR_CHANNEL)
5506 {
5507 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005508 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005509 typval_T dtv;
5510 jsonq_T *jq;
5511 cbq_T *cq;
5512
5513 if (ch != NULL && ch->ch_copyID != copyID)
5514 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005515 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005516 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005517 {
5518 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5519 jq = jq->jq_next)
5520 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5521 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5522 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005523 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005524 {
5525 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005526 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005527 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5528 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005529 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005530 {
5531 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005532 dtv.vval.v_partial =
5533 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005534 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5535 }
5536 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005537 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005538 {
5539 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005540 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005541 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5542 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005543 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005544 {
5545 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005546 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005547 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5548 }
5549 }
5550 }
5551#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005552 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005553}
5554
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 * Return a string with the string representation of a variable.
5557 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005558 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005559 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005560 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005561 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005562 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005563 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5564 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005565 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005567 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005568echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569 typval_T *tv,
5570 char_u **tofree,
5571 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005572 int copyID,
5573 int echo_style,
5574 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005575 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005577 static int recurse = 0;
5578 char_u *r = NULL;
5579
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005581 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005582 if (!did_echo_string_emsg)
5583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 // Only give this message once for a recursive call to avoid
5585 // flooding the user with errors. And stop iterating over lists
5586 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005587 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005588 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005589 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005590 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005591 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005592 }
5593 ++recurse;
5594
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 switch (tv->v_type)
5596 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005597 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005598 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005599 {
5600 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005601 r = tv->vval.v_string;
5602 if (r == NULL)
5603 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005604 }
5605 else
5606 {
5607 *tofree = string_quote(tv->vval.v_string, FALSE);
5608 r = *tofree;
5609 }
5610 break;
5611
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005613 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005614 char_u buf[MAX_FUNC_NAME_LEN];
5615
5616 if (echo_style)
5617 {
LemonBoya5d35902022-04-29 21:15:02 +01005618 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5619 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005620 buf, MAX_FUNC_NAME_LEN);
5621 if (r == buf)
5622 {
5623 r = vim_strsave(buf);
5624 *tofree = r;
5625 }
5626 else
5627 *tofree = NULL;
5628 }
5629 else
5630 {
5631 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5632 : make_ufunc_name_readable(
5633 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5634 TRUE);
5635 r = *tofree;
5636 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005639
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005640 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005641 {
5642 partial_T *pt = tv->vval.v_partial;
5643 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005644 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005645 garray_T ga;
5646 int i;
5647 char_u *tf;
5648
5649 ga_init2(&ga, 1, 100);
5650 ga_concat(&ga, (char_u *)"function(");
5651 if (fname != NULL)
5652 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005653 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005654 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005655 && vim_isupper(fname[1]))
5656 {
5657 ga_concat(&ga, (char_u *)"'g:");
5658 ga_concat(&ga, fname + 1);
5659 }
5660 else
5661 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005662 vim_free(fname);
5663 }
5664 if (pt != NULL && pt->pt_argc > 0)
5665 {
5666 ga_concat(&ga, (char_u *)", [");
5667 for (i = 0; i < pt->pt_argc; ++i)
5668 {
5669 if (i > 0)
5670 ga_concat(&ga, (char_u *)", ");
5671 ga_concat(&ga,
5672 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5673 vim_free(tf);
5674 }
5675 ga_concat(&ga, (char_u *)"]");
5676 }
5677 if (pt != NULL && pt->pt_dict != NULL)
5678 {
5679 typval_T dtv;
5680
5681 ga_concat(&ga, (char_u *)", ");
5682 dtv.v_type = VAR_DICT;
5683 dtv.vval.v_dict = pt->pt_dict;
5684 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5685 vim_free(tf);
5686 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005687 // terminate with ')' and a NUL
5688 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005689
5690 *tofree = ga.ga_data;
5691 r = *tofree;
5692 break;
5693 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005694
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005695 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005696 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005697 break;
5698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005700 if (tv->vval.v_list == NULL)
5701 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005702 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005703 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005704 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005705 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005706 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5707 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005708 {
5709 *tofree = NULL;
5710 r = (char_u *)"[...]";
5711 }
5712 else
5713 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005714 int old_copyID = tv->vval.v_list->lv_copyID;
5715
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005716 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005717 *tofree = list2string(tv, copyID, restore_copyID);
5718 if (restore_copyID)
5719 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005720 r = *tofree;
5721 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005722 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005723
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005725 if (tv->vval.v_dict == NULL)
5726 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005727 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005728 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005729 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005730 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005731 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5732 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005733 {
5734 *tofree = NULL;
5735 r = (char_u *)"{...}";
5736 }
5737 else
5738 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005739 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005740
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005741 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005742 *tofree = dict2string(tv, copyID, restore_copyID);
5743 if (restore_copyID)
5744 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005745 r = *tofree;
5746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005747 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005748
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005750 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005751 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005752 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005753 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005754 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005755 break;
5756
Bram Moolenaar835dc632016-02-07 14:27:38 +01005757 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005758 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005760 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005761 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5762 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005763 if (composite_val)
5764 {
5765 *tofree = string_quote(r, FALSE);
5766 r = *tofree;
5767 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005768#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005770
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005771 case VAR_INSTR:
5772 *tofree = NULL;
5773 r = (char_u *)"instructions";
5774 break;
5775
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005776 case VAR_FLOAT:
5777 *tofree = NULL;
5778 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5779 r = numbuf;
5780 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005781
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005782 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005783 case VAR_SPECIAL:
5784 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005785 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005786 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005788
Bram Moolenaar8502c702014-06-17 12:51:16 +02005789 if (--recurse == 0)
5790 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005791 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792}
5793
5794/*
5795 * Return a string with the string representation of a variable.
5796 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5797 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005798 * Does not put quotes around strings, as ":echo" displays values.
5799 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5800 * May return NULL.
5801 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005802 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005803echo_string(
5804 typval_T *tv,
5805 char_u **tofree,
5806 char_u *numbuf,
5807 int copyID)
5808{
5809 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5810}
5811
5812/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005813 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5814 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005815 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005816 */
5817 int
5818buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5819{
5820 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005821 char_u *t;
5822 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005823
5824 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5825 return -1;
5826
5827 if (lnum > buf->b_ml.ml_line_count)
5828 lnum = buf->b_ml.ml_line_count;
5829
5830 str = ml_get_buf(buf, lnum, FALSE);
5831 if (str == NULL)
5832 return -1;
5833
5834 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005835 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005836
Bram Moolenaar91458462021-01-13 20:08:38 +01005837 // count the number of characters
5838 t = str;
5839 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5840 t += mb_ptr2len(t);
5841
5842 // In insert mode, when the cursor is at the end of a non-empty line,
5843 // byteidx points to the NUL character immediately past the end of the
5844 // string. In this case, add one to the character count.
5845 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5846 count++;
5847
5848 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005849}
5850
5851/*
5852 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005853 * byte index. Works only for loaded buffers. Returns -1 on failure.
5854 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005855 */
5856 int
5857buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5858{
5859 char_u *str;
5860 char_u *t;
5861
5862 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5863 return -1;
5864
5865 if (lnum > buf->b_ml.ml_line_count)
5866 lnum = buf->b_ml.ml_line_count;
5867
5868 str = ml_get_buf(buf, lnum, FALSE);
5869 if (str == NULL)
5870 return -1;
5871
5872 // Convert the character offset to a byte offset
5873 t = str;
5874 while (*t != NUL && --charidx > 0)
5875 t += mb_ptr2len(t);
5876
Bram Moolenaar91458462021-01-13 20:08:38 +01005877 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005878}
5879
5880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005882 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885var2fpos(
5886 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005888 int *fnum, // set to fnum for '0, 'A, etc.
5889 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005891 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005893 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005896 if (varp->v_type == VAR_LIST)
5897 {
5898 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005899 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005900 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005901 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005902
5903 l = varp->vval.v_list;
5904 if (l == NULL)
5905 return NULL;
5906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005907 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005908 pos.lnum = list_find_nr(l, 0L, &error);
5909 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005911 if (charcol)
5912 len = (long)mb_charlen(ml_get(pos.lnum));
5913 else
5914 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005915
Bram Moolenaarec65d772020-08-20 22:29:12 +02005916 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005918 li = list_find(l, 1L);
5919 if (li != NULL && li->li_tv.v_type == VAR_STRING
5920 && li->li_tv.vval.v_string != NULL
5921 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005922 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005923 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005924 }
5925 else
5926 {
5927 pos.col = list_find_nr(l, 1L, &error);
5928 if (error)
5929 return NULL;
5930 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005933 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005935 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005938 pos.coladd = list_find_nr(l, 2L, &error);
5939 if (error)
5940 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005941
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005942 return &pos;
5943 }
5944
Bram Moolenaarc5809432021-03-27 21:23:30 +01005945 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5946 return NULL;
5947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005948 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005949 if (name == NULL)
5950 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005951
5952 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005953 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005954 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005955 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005956 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005957 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005958 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005959 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005960 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005961 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005962 pos = VIsual;
5963 else
5964 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005965 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005966 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005967 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005969 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005970 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5972 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005973 pos = *pp;
5974 }
5975 if (pos.lnum != 0)
5976 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005977 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005978 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5979 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005981
Bram Moolenaara5525202006-03-02 22:52:09 +00005982 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005983
Bram Moolenaar477933c2007-07-17 14:32:23 +00005984 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005985 {
5986 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005987 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005988 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005989 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005990 // In silent Ex mode topline is zero, but that's not a valid line
5991 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005992 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005993 return &pos;
5994 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005995 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005996 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005997 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005998 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005999 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006000 return &pos;
6001 }
6002 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006003 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006005 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {
6007 pos.lnum = curbuf->b_ml.ml_line_count;
6008 pos.col = 0;
6009 }
6010 else
6011 {
6012 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006013 if (charcol)
6014 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6015 else
6016 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 }
6018 return &pos;
6019 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006020 if (in_vim9script())
6021 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 return NULL;
6023}
6024
6025/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006026 * Convert list in "arg" into a position and optional file number.
6027 * When "fnump" is NULL there is no file number, only 3 items.
6028 * Note that the column is passed on as-is, the caller may want to decrement
6029 * it to use 1 for the first column.
6030 * Return FAIL when conversion is not possible, doesn't check the position for
6031 * validity.
6032 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034list2fpos(
6035 typval_T *arg,
6036 pos_T *posp,
6037 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006038 colnr_T *curswantp,
6039 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006040{
6041 list_T *l = arg->vval.v_list;
6042 long i = 0;
6043 long n;
6044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006045 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6046 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006047 if (arg->v_type != VAR_LIST
6048 || l == NULL
6049 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006050 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006051 return FAIL;
6052
6053 if (fnump != NULL)
6054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006055 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006056 if (n < 0)
6057 return FAIL;
6058 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006059 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006060 *fnump = n;
6061 }
6062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006064 if (n < 0)
6065 return FAIL;
6066 posp->lnum = n;
6067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006069 if (n < 0)
6070 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006071 // If character position is specified, then convert to byte position
6072 if (charcol)
6073 {
6074 buf_T *buf;
6075
6076 // Get the text for the specified line in a loaded buffer
6077 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6078 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6079 return FAIL;
6080
Bram Moolenaar91458462021-01-13 20:08:38 +01006081 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006082 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006083 posp->col = n;
6084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006086 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006087 posp->coladd = 0;
6088 else
6089 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006090
Bram Moolenaar493c1782014-05-28 14:34:46 +02006091 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006092 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006093
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006094 return OK;
6095}
6096
6097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 * Get the length of an environment variable name.
6099 * Advance "arg" to the first character after the name.
6100 * Return 0 for error.
6101 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char_u *p;
6106 int len;
6107
6108 for (p = *arg; vim_isIDc(*p); ++p)
6109 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006110 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 return 0;
6112
6113 len = (int)(p - *arg);
6114 *arg = p;
6115 return len;
6116}
6117
6118/*
6119 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006120 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 * Return 0 if something is wrong.
6122 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006123 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 char_u *p;
6127 int len;
6128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006129 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006131 {
6132 if (*p == ':')
6133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // "s:" is start of "s:var", but "n:" is not and can be used in
6135 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006136 len = (int)(p - *arg);
6137 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6138 || len > 1)
6139 break;
6140 }
6141 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006142 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 return 0;
6144
6145 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006146 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 return len;
6149}
6150
6151/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006152 * Get the length of the name of a variable or function.
6153 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006155 * Return -1 if curly braces expansion failed.
6156 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 * If the name contains 'magic' {}'s, expand them and return the
6158 * expanded name in an allocated string via 'alias' - caller must free.
6159 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161get_name_len(
6162 char_u **arg,
6163 char_u **alias,
6164 int evaluate,
6165 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
6167 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 char_u *p;
6169 char_u *expr_start;
6170 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006172 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6175 && (*arg)[2] == (int)KE_SNR)
6176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006177 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 *arg += 3;
6179 return get_id_len(arg) + 3;
6180 }
6181 len = eval_fname_script(*arg);
6182 if (len > 0)
6183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006184 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 *arg += len;
6186 }
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006189 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006191 p = find_name_end(*arg, &expr_start, &expr_end,
6192 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (expr_start != NULL)
6194 {
6195 char_u *temp_string;
6196
6197 if (!evaluate)
6198 {
6199 len += (int)(p - *arg);
6200 *arg = skipwhite(p);
6201 return len;
6202 }
6203
6204 /*
6205 * Include any <SID> etc in the expanded string:
6206 * Thus the -len here.
6207 */
6208 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6209 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006210 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 *alias = temp_string;
6212 *arg = skipwhite(p);
6213 return (int)STRLEN(temp_string);
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006217 // Only give an error when there is something, otherwise it will be
6218 // reported at a higher level.
6219 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006220 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
6222 return len;
6223}
6224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006225/*
6226 * Find the end of a variable or function name, taking care of magic braces.
6227 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6228 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006229 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006230 * Return a pointer to just after the name. Equal to "arg" if there is no
6231 * valid name.
6232 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234find_name_end(
6235 char_u *arg,
6236 char_u **expr_start,
6237 char_u **expr_end,
6238 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006240 int mb_nest = 0;
6241 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006243 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006244 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006248 *expr_start = NULL;
6249 *expr_end = NULL;
6250 }
6251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006252 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006253 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6254 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006255 return arg;
6256
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006257 for (p = arg; *p != NUL
6258 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006259 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006260 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006261 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006262 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006263 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006264 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006265 if (*p == '\'')
6266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006267 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006268 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006269 ;
6270 if (*p == NUL)
6271 break;
6272 }
6273 else if (*p == '"')
6274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006276 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006277 if (*p == '\\' && p[1] != NUL)
6278 ++p;
6279 if (*p == NUL)
6280 break;
6281 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006282 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 // "s:" is start of "s:var", but "n:" is not and can be used in
6285 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006286 len = (int)(p - arg);
6287 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006288 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006289 break;
6290 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006292 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 if (*p == '[')
6295 ++br_nest;
6296 else if (*p == ']')
6297 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006299
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006300 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006302 if (*p == '{')
6303 {
6304 mb_nest++;
6305 if (expr_start != NULL && *expr_start == NULL)
6306 *expr_start = p;
6307 }
6308 else if (*p == '}')
6309 {
6310 mb_nest--;
6311 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6312 *expr_end = p;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316
6317 return p;
6318}
6319
6320/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006321 * Expands out the 'magic' {}'s in a variable/function name.
6322 * Note that this can call itself recursively, to deal with
6323 * constructs like foo{bar}{baz}{bam}
6324 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6325 * "in_start" ^
6326 * "expr_start" ^
6327 * "expr_end" ^
6328 * "in_end" ^
6329 *
6330 * Returns a new allocated string, which the caller must free.
6331 * Returns NULL for failure.
6332 */
6333 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006334make_expanded_name(
6335 char_u *in_start,
6336 char_u *expr_start,
6337 char_u *expr_end,
6338 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006339{
6340 char_u c1;
6341 char_u *retval = NULL;
6342 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006343
6344 if (expr_end == NULL || in_end == NULL)
6345 return NULL;
6346 *expr_start = NUL;
6347 *expr_end = NUL;
6348 c1 = *in_end;
6349 *in_end = NUL;
6350
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006351 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006352 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006353 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006354 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6355 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006356 if (retval != NULL)
6357 {
6358 STRCPY(retval, in_start);
6359 STRCAT(retval, temp_result);
6360 STRCAT(retval, expr_end + 1);
6361 }
6362 }
6363 vim_free(temp_result);
6364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006366 *expr_start = '{';
6367 *expr_end = '}';
6368
6369 if (retval != NULL)
6370 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006371 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006372 if (expr_start != NULL)
6373 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006374 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006375 temp_result = make_expanded_name(retval, expr_start,
6376 expr_end, temp_result);
6377 vim_free(retval);
6378 retval = temp_result;
6379 }
6380 }
6381
6382 return retval;
6383}
6384
6385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006392 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006393}
6394
6395/*
6396 * Return TRUE if character "c" can be used as the first character in a
6397 * variable or function name (excluding '{' and '}').
6398 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006400eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006401{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006402 return ASCII_ISALPHA(c) || c == '_';
6403}
6404
6405/*
6406 * Return TRUE if character "c" can be used as the first character of a
6407 * dictionary key.
6408 */
6409 int
6410eval_isdictc(int c)
6411{
6412 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413}
6414
6415/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006416 * Handle:
6417 * - expr[expr], expr[expr:expr] subscript
6418 * - ".name" lookup
6419 * - function call with Funcref variable: func(expr)
6420 * - method call: var->method()
6421 *
6422 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006423 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006424 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426handle_subscript(
6427 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006428 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006430 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006431 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006432{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006433 int evaluate = evalarg != NULL
6434 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006435 int ret = OK;
6436 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006437 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006438 int getnext;
6439 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006440
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006441 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006442 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006443 // When at the end of the line and ".name" or "->{" or "->X" follows in
6444 // the next line then consume the line break.
6445 p = eval_next_non_blank(*arg, evalarg, &getnext);
6446 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006447 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006448 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6449 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6450 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006451 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006452 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006453 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006454 check_white = FALSE;
6455 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006456
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006457 if (rettv->v_type == VAR_ANY)
6458 {
6459 char_u *exp_name;
6460 int cc;
6461 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006462 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006463 type_T *type;
6464
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006465 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006466 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006467 if (**arg != '.')
6468 {
6469 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006470 semsg(_(e_expected_dot_after_name_str),
6471 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006472 ret = FAIL;
6473 break;
6474 }
6475 ++*arg;
6476 if (IS_WHITE_OR_NUL(**arg))
6477 {
6478 if (verbose)
6479 emsg(_(e_no_white_space_allowed_after_dot));
6480 ret = FAIL;
6481 break;
6482 }
6483
6484 // isolate the name
6485 exp_name = *arg;
6486 while (eval_isnamec(**arg))
6487 ++*arg;
6488 cc = **arg;
6489 **arg = NUL;
6490
6491 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006492 evalarg == NULL ? NULL : evalarg->eval_cctx,
6493 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006494 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006495
6496 if (idx < 0 && ufunc == NULL)
6497 {
6498 ret = FAIL;
6499 break;
6500 }
6501 if (idx >= 0)
6502 {
6503 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6504 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6505
6506 copy_tv(sv->sv_tv, rettv);
6507 }
6508 else
6509 {
6510 rettv->v_type = VAR_FUNC;
6511 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6512 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006513 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006514 }
6515
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006516 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6517 || rettv->v_type == VAR_PARTIAL))
6518 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006519 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006520 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6521 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006522
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006523 // Stop the expression evaluation when immediately aborting on
6524 // error, or when an interrupt occurred or an exception was thrown
6525 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006526 if (aborting())
6527 {
6528 if (ret == OK)
6529 clear_tv(rettv);
6530 ret = FAIL;
6531 }
6532 dict_unref(selfdict);
6533 selfdict = NULL;
6534 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006535 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006536 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006537 if (in_vim9script())
6538 *arg = skipwhite(p + 2);
6539 else
6540 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006541 if (ret == OK)
6542 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006543 if (VIM_ISWHITE(**arg))
6544 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006545 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006546 ret = FAIL;
6547 }
6548 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006549 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006550 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006551 else
6552 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006553 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006554 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006555 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006556 // "." is ".name" lookup when we found a dict or when evaluating and
6557 // scriptversion is at least 2, where string concatenation is "..".
6558 else if (**arg == '['
6559 || (**arg == '.' && (rettv->v_type == VAR_DICT
6560 || (!evaluate
6561 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006562 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006563 {
6564 dict_unref(selfdict);
6565 if (rettv->v_type == VAR_DICT)
6566 {
6567 selfdict = rettv->vval.v_dict;
6568 if (selfdict != NULL)
6569 ++selfdict->dv_refcount;
6570 }
6571 else
6572 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006573 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006574 {
6575 clear_tv(rettv);
6576 ret = FAIL;
6577 }
6578 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006579 else
6580 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006581 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6584 // Don't do this when "Func" is already a partial that was bound
6585 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006586 if (selfdict != NULL
6587 && (rettv->v_type == VAR_FUNC
6588 || (rettv->v_type == VAR_PARTIAL
6589 && (rettv->vval.v_partial->pt_auto
6590 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006591 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006593 dict_unref(selfdict);
6594 return ret;
6595}
6596
6597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006598 * Make a copy of an item.
6599 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006600 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6602 * reference to an already copied list/dict can be used.
6603 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006606item_copy(
6607 typval_T *from,
6608 typval_T *to,
6609 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006610 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612{
6613 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006617 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006618 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 }
6621 ++recurse;
6622
6623 switch (from->v_type)
6624 {
6625 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006626 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 case VAR_STRING:
6628 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006629 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006630 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006631 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006632 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006633 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006634 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006635 copy_tv(from, to);
6636 break;
6637 case VAR_LIST:
6638 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006639 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 if (from->vval.v_list == NULL)
6641 to->vval.v_list = NULL;
6642 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006645 to->vval.v_list = from->vval.v_list->lv_copylist;
6646 ++to->vval.v_list->lv_refcount;
6647 }
6648 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006649 to->vval.v_list = list_copy(from->vval.v_list,
6650 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006651 if (to->vval.v_list == NULL)
6652 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006653 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006654 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006656 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 case VAR_DICT:
6658 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006659 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 if (from->vval.v_dict == NULL)
6661 to->vval.v_dict = NULL;
6662 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006664 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006665 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6666 ++to->vval.v_dict->dv_refcount;
6667 }
6668 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006669 to->vval.v_dict = dict_copy(from->vval.v_dict,
6670 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006671 if (to->vval.v_dict == NULL)
6672 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006674 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006675 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006677 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 }
6680 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682}
6683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684 void
6685echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6686{
6687 char_u *tofree;
6688 char_u numbuf[NUMBUFLEN];
6689 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6690
6691 if (*atstart)
6692 {
6693 *atstart = FALSE;
6694 // Call msg_start() after eval1(), evaluating the expression
6695 // may cause a message to appear.
6696 if (with_space)
6697 {
6698 // Mark the saved text as finishing the line, so that what
6699 // follows is displayed on a new line when scrolling back
6700 // at the more prompt.
6701 msg_sb_eol();
6702 msg_start();
6703 }
6704 }
6705 else if (with_space)
6706 msg_puts_attr(" ", echo_attr);
6707
6708 if (p != NULL)
6709 for ( ; *p != NUL && !got_int; ++p)
6710 {
6711 if (*p == '\n' || *p == '\r' || *p == TAB)
6712 {
6713 if (*p != TAB && *needclr)
6714 {
6715 // remove any text still there from the command
6716 msg_clr_eos();
6717 *needclr = FALSE;
6718 }
6719 msg_putchar_attr(*p, echo_attr);
6720 }
6721 else
6722 {
6723 if (has_mbyte)
6724 {
6725 int i = (*mb_ptr2len)(p);
6726
6727 (void)msg_outtrans_len_attr(p, i, echo_attr);
6728 p += i - 1;
6729 }
6730 else
6731 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6732 }
6733 }
6734 vim_free(tofree);
6735}
6736
Bram Moolenaare9a41262005-01-15 22:18:47 +00006737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 * ":echo expr1 ..." print each argument separated with a space, add a
6739 * newline at the end.
6740 * ":echon expr1 ..." print each argument plain.
6741 */
6742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006743ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006747 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 int needclr = TRUE;
6749 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006750 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006751 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006752 evalarg_T evalarg;
6753
Bram Moolenaare707c882020-07-01 13:07:37 +02006754 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if (eap->skip)
6757 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006758 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006760 // If eval1() causes an error message the text from the command may
6761 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006762 need_clr_eos = needclr;
6763
Bram Moolenaar68db9962021-05-09 23:19:22 +02006764 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006765 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
6767 /*
6768 * Report the invalid expression unless the expression evaluation
6769 * has been cancelled due to an aborting error, an interrupt, or an
6770 * exception.
6771 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006772 if (!aborting() && did_emsg == did_emsg_before
6773 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006774 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006775 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 break;
6777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006778 need_clr_eos = FALSE;
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006781 {
6782 if (rettv.v_type == VAR_VOID)
6783 {
6784 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6785 break;
6786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006787 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006788 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006789
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 arg = skipwhite(arg);
6792 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006793 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006794 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 if (eap->skip)
6797 --emsg_skip;
6798 else
6799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006800 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (needclr)
6802 msg_clr_eos();
6803 if (eap->cmdidx == CMD_echo)
6804 msg_end();
6805 }
6806}
6807
6808/*
6809 * ":echohl {name}".
6810 */
6811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006814 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006818 * Returns the :echo attribute
6819 */
6820 int
6821get_echo_attr(void)
6822{
6823 return echo_attr;
6824}
6825
6826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 * ":execute expr1 ..." execute the result of an expression.
6828 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006829 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006831 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 * Each gets spaces around each argument and a newline at the end for
6833 * echo commands
6834 */
6835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006836ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int ret = OK;
6841 char_u *p;
6842 garray_T ga;
6843 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006844 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
6846 ga_init2(&ga, 1, 80);
6847
6848 if (eap->skip)
6849 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006850 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006852 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006853 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 if (!eap->skip)
6857 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006858 char_u buf[NUMBUFLEN];
6859
6860 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006861 {
6862 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6863 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006864 semsg(_(e_using_invalid_value_as_string_str),
6865 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006866 p = NULL;
6867 }
6868 else
6869 p = tv_get_string_buf(&rettv, buf);
6870 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006871 else
6872 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006873 if (p == NULL)
6874 {
6875 clear_tv(&rettv);
6876 ret = FAIL;
6877 break;
6878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 len = (int)STRLEN(p);
6880 if (ga_grow(&ga, len + 2) == FAIL)
6881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 ret = FAIL;
6884 break;
6885 }
6886 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 ga.ga_len += len;
6890 }
6891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 arg = skipwhite(arg);
6894 }
6895
6896 if (ret != FAIL && ga.ga_data != NULL)
6897 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006898 // use the first line of continuation lines for messages
6899 SOURCING_LNUM = start_lnum;
6900
Bram Moolenaar37fef162022-08-29 18:16:32 +01006901 if (eap->cmdidx == CMD_echomsg
6902 || eap->cmdidx == CMD_echowindow
6903 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006905 // Mark the already saved text as finishing the line, so that what
6906 // follows is displayed on a new line when scrolling back at the
6907 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006908 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006909 }
6910
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006911 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006912 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006913 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006914 out_flush();
6915 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006916 else if (eap->cmdidx == CMD_echowindow)
6917 {
6918#ifdef HAS_MESSAGE_WINDOW
6919 start_echowindow();
6920#endif
6921 msg_attr(ga.ga_data, echo_attr);
6922#ifdef HAS_MESSAGE_WINDOW
6923 end_echowindow();
6924#endif
6925 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006926 else if (eap->cmdidx == CMD_echoconsole)
6927 {
6928 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6929 ui_write((char_u *)"\r\n", 2, TRUE);
6930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 else if (eap->cmdidx == CMD_echoerr)
6932 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006933 int save_did_emsg = did_emsg;
6934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006935 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006936 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (!force_abort)
6938 did_emsg = save_did_emsg;
6939 }
6940 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006941 {
6942 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6943
6944 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6945 sticky_cmdmod_flags = cmdmod.cmod_flags
6946 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 do_cmdline((char_u *)ga.ga_data,
6948 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006949 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
6953 ga_clear(&ga);
6954
6955 if (eap->skip)
6956 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006957 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958}
6959
6960/*
6961 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6962 * "arg" points to the "&" or '+' when called, to "option" when returning.
6963 * Returns NULL when no option name found. Otherwise pointer to the char
6964 * after the option name.
6965 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006966 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006967find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 char_u *p = *arg;
6970
6971 ++p;
6972 if (*p == 'g' && p[1] == ':')
6973 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006974 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 p += 2;
6976 }
6977 else if (*p == 'l' && p[1] == ':')
6978 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006979 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 p += 2;
6981 }
6982 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006983 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 if (!ASCII_ISALPHA(*p))
6986 return NULL;
6987 *arg = p;
6988
6989 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006990 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 else
6992 while (ASCII_ISALPHA(*p))
6993 ++p;
6994 return p;
6995}
6996
6997/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006998 * Display script name where an item was last set.
6999 * Should only be invoked when 'verbose' is non-zero.
7000 */
7001 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007002last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007003{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007004 char_u *p;
7005
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007006 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007007 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007008 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007009 if (p != NULL)
7010 {
7011 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007012 msg_puts(_("\n\tLast set from "));
7013 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007014 if (script_ctx.sc_lnum > 0)
7015 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007016 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007017 msg_outnum((long)script_ctx.sc_lnum);
7018 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007019 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007020 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007021 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007022 }
7023}
7024
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007025#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
7027/*
7028 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007029 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 * "flags" can be "g" to do a global substitute.
7031 * Returns an allocated string, NULL for error.
7032 */
7033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034do_string_sub(
7035 char_u *str,
7036 char_u *pat,
7037 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007038 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 int sublen;
7042 regmatch_T regmatch;
7043 int i;
7044 int do_all;
7045 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007046 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 garray_T ga;
7048 char_u *ret;
7049 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007050 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007052 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007054 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 ga_init2(&ga, 1, 200);
7057
7058 do_all = (flags[0] == 'g');
7059
7060 regmatch.rm_ic = p_ic;
7061 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7062 if (regmatch.regprog != NULL)
7063 {
7064 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007065 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007068 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007069 if (regmatch.startp[0] == regmatch.endp[0])
7070 {
7071 if (zero_width == regmatch.startp[0])
7072 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007073 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007074 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007075 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7076 (size_t)i);
7077 ga.ga_len += i;
7078 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007079 continue;
7080 }
7081 zero_width = regmatch.startp[0];
7082 }
7083
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 /*
7085 * Get some space for a temporary buffer to do the substitution
7086 * into. It will contain:
7087 * - The text up to where the match is.
7088 * - The substituted text.
7089 * - The text after the match.
7090 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007091 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007092 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7094 {
7095 ga_clear(&ga);
7096 break;
7097 }
7098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007099 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 i = (int)(regmatch.startp[0] - tail);
7101 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007103 (void)vim_regsub(&regmatch, sub, expr,
7104 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7105 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007107 tail = regmatch.endp[0];
7108 if (*tail == NUL)
7109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (!do_all)
7111 break;
7112 }
7113
7114 if (ga.ga_data != NULL)
7115 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7116
Bram Moolenaar473de612013-06-08 18:19:48 +02007117 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119
7120 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7121 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007122 if (p_cpo == empty_option)
7123 p_cpo = save_cpo;
7124 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007126 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007127 // If it's still empty it was changed and restored, need to restore in
7128 // the complicated way.
7129 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007130 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007131 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133
7134 return ret;
7135}