blob: 05b6584424ce216c5eecedda9cc178a736dd0030 [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 Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static 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 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100106#if defined(EBCDIC) || defined(PROTO)
107/*
108 * Compare struct fst by function name.
109 */
110 static int
111compare_func_name(const void *s1, const void *s2)
112{
113 struct fst *p1 = (struct fst *)s1;
114 struct fst *p2 = (struct fst *)s2;
115
116 return STRCMP(p1->f_name, p2->f_name);
117}
118
119/*
120 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100121 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100122 * On machines using EBCDIC we have to sort it.
123 */
124 static void
125sortFunctions(void)
126{
127 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
128
129 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
130}
131#endif
132
Bram Moolenaar33570922005-01-25 22:26:29 +0000133/*
134 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000135 */
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000141
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142#ifdef EBCDIC
143 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100144 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200145 */
146 sortFunctions();
147#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000148}
149
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150#if defined(EXITFREE) || defined(PROTO)
151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100152eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200154 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100155 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200156 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000157
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200158 // autoloaded script names
159 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000160
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200161 // unreferenced lists and dicts
162 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200163
164 // functions not garbage collected
165 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000166}
167#endif
168
Bram Moolenaare6b53242020-07-01 17:28:33 +0200169 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200170fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
171{
172 CLEAR_FIELD(*evalarg);
173 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
174 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
175 {
176 evalarg->eval_getline = eap->getline;
177 evalarg->eval_cookie = eap->cookie;
178 }
179}
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Top level evaluation function, returning a boolean.
183 * Sets "error" to TRUE if there was an error.
184 * Return TRUE or FALSE.
185 */
186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100187eval_to_bool(
188 char_u *arg,
189 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200190 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100191 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaar33570922005-01-25 22:26:29 +0000193 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 evalarg_T evalarg;
196
Bram Moolenaar37c83712020-06-30 21:18:36 +0200197 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 if (skip)
200 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 else
204 {
205 *error = FALSE;
206 if (!skip)
207 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200209 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200210 else
211 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000212 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
214 }
215 if (skip)
216 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220}
221
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222/*
223 * Call eval1() and give an error message if not done at a lower level.
224 */
225 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200226eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100228 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100229 int ret;
230 int did_emsg_before = did_emsg;
231 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200234 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
235
236 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100237 if (ret == FAIL)
238 {
239 // Report the invalid expression unless the expression evaluation has
240 // been cancelled due to an aborting error, an interrupt, or an
241 // exception, or we already gave a more specific error.
242 // Also check called_emsg for when using assert_fails().
243 if (!aborting() && did_emsg == did_emsg_before
244 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100245 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200247 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100248 return ret;
249}
250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200252 * Return whether a typval is a valid expression to pass to eval_expr_typval()
253 * or eval_expr_to_bool(). An empty string returns FALSE;
254 */
255 int
256eval_expr_valid_arg(typval_T *tv)
257{
258 return tv->v_type != VAR_UNKNOWN
259 && (tv->v_type != VAR_STRING
260 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
261}
262
263/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 * Evaluate an expression, which can be a function, partial or string.
265 * Pass arguments "argv[argc]".
266 * Return the result in "rettv" and OK or FAIL.
267 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200268 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100269eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
270{
271 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200273 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100274
275 if (expr->v_type == VAR_FUNC)
276 {
277 s = expr->vval.v_string;
278 if (s == NULL || *s == NUL)
279 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200280 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200281 funcexe.evaluate = TRUE;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 return FAIL;
284 }
285 else if (expr->v_type == VAR_PARTIAL)
286 {
287 partial_T *partial = expr->vval.v_partial;
288
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200289 if (partial == NULL)
290 return FAIL;
291
Bram Moolenaar822ba242020-05-24 23:00:18 +0200292 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200293 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200295 if (call_def_function(partial->pt_func, argc, argv,
296 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 return FAIL;
298 }
299 else
300 {
301 s = partial_name(partial);
302 if (s == NULL || *s == NUL)
303 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200304 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 funcexe.evaluate = TRUE;
306 funcexe.partial = partial;
307 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
308 return FAIL;
309 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100310 }
311 else
312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100313 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100314 if (s == NULL)
315 return FAIL;
316 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200317 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200319 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200321 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100322 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 return FAIL;
324 }
325 }
326 return OK;
327}
328
329/*
330 * Like eval_to_bool() but using a typval_T instead of a string.
331 * Works for string, funcref and partial.
332 */
333 int
334eval_expr_to_bool(typval_T *expr, int *error)
335{
336 typval_T rettv;
337 int res;
338
339 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
340 {
341 *error = TRUE;
342 return FALSE;
343 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200344 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100345 clear_tv(&rettv);
346 return res;
347}
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349/*
350 * Top level evaluation function, returning a string. If "skip" is TRUE,
351 * only parsing to "nextcmd" is done, without reporting errors. Return
352 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
353 */
354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_to_string_skip(
356 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200357 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100358 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200362 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar37c83712020-06-30 21:18:36 +0200364 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (skip)
366 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200367 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 retval = NULL;
369 else
370 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100371 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
374 if (skip)
375 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378 return retval;
379}
380
381/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000382 * Skip over an expression at "*pp".
383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200386skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387{
Bram Moolenaar33570922005-01-25 22:26:29 +0000388 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000389
390 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200391 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000392}
393
394/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 * Skip over an expression at "*pp".
396 * If in Vim9 script and line breaks are encountered, the lines are
397 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200398 * "arg" is advanced to just after the expression.
399 * "start" is set to the start of the expression, "end" to just after the end.
400 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 * Return FAIL for an error, OK otherwise.
402 */
403 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200404skip_expr_concatenate(
405 char_u **arg,
406 char_u **start,
407 char_u **end,
408 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409{
410 typval_T rettv;
411 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200412 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200413 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200415 int evaluate = evalarg == NULL
416 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
421 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 ++gap->ga_len;
425 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200426 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427
428 // Don't evaluate the expression.
429 if (evalarg != NULL)
430 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 *arg = skipwhite(*arg);
432 res = eval1(arg, &rettv, evalarg);
433 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434 if (evalarg != NULL)
435 evalarg->eval_flags = save_flags;
436
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200437 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200439 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 if (evalarg->eval_ga.ga_len == 1)
441 {
442 // just one line, no need to concatenate
443 ga_clear(gap);
444 gap->ga_itemsize = 0;
445 }
446 else
447 {
448 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200449 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 // Line breaks encountered, concatenate all the lines.
452 *((char_u **)gap->ga_data) = *start;
453 p = ga_concat_strings(gap, "");
454
455 // free the lines only when using getsourceline()
456 if (evalarg->eval_cookie != NULL)
457 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 // Do not free the last line, "arg" points into it, free it
461 // later.
462 vim_free(evalarg->eval_tofree);
463 evalarg->eval_tofree =
464 ((char_u **)gap->ga_data)[gap->ga_len - 1];
465 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 ga_clear_strings(gap);
467 }
468 else
469 ga_clear(gap);
470 gap->ga_itemsize = 0;
471 if (p == NULL)
472 return FAIL;
473 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200474 vim_free(evalarg->eval_tofree_lambda);
475 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200476 // Compute "end" relative to the end.
477 *end = *start + STRLEN(*start) - endoff;
478 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 }
480
481 return res;
482}
483
484/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100485 * Convert "tv" to a string.
486 * When "convert" is TRUE convert a List into a sequence of lines and convert
487 * a Float to a String.
488 * Returns an allocated string (NULL when out of memory).
489 */
490 char_u *
491typval2string(typval_T *tv, int convert)
492{
493 garray_T ga;
494 char_u *retval;
495#ifdef FEAT_FLOAT
496 char_u numbuf[NUMBUFLEN];
497#endif
498
499 if (convert && tv->v_type == VAR_LIST)
500 {
501 ga_init2(&ga, (int)sizeof(char), 80);
502 if (tv->vval.v_list != NULL)
503 {
504 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
505 if (tv->vval.v_list->lv_len > 0)
506 ga_append(&ga, NL);
507 }
508 ga_append(&ga, NUL);
509 retval = (char_u *)ga.ga_data;
510 }
511#ifdef FEAT_FLOAT
512 else if (convert && tv->v_type == VAR_FLOAT)
513 {
514 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
515 retval = vim_strsave(numbuf);
516 }
517#endif
518 else
519 retval = vim_strsave(tv_get_string(tv));
520 return retval;
521}
522
523/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200524 * Top level evaluation function, returning a string. Does not handle line
525 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Return pointer to allocated memory, or NULL for failure.
529 */
530 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100532 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100533 int convert,
534 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar33570922005-01-25 22:26:29 +0000536 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
541 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 retval = NULL;
543 else
544 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100545 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000546 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100548 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 return retval;
551}
552
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100553 char_u *
554eval_to_string(
555 char_u *arg,
556 int convert)
557{
558 return eval_to_string_eap(arg, convert, NULL);
559}
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200563 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200564 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 */
566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567eval_to_string_safe(
568 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100569 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200579 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000581 if (use_sandbox)
582 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200583 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200584 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200585 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 return retval;
587}
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
590 * Top level evaluation function, returning a number.
591 * Evaluates "expr" silently.
592 * Returns -1 for an error.
593 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200594 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100595eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
Bram Moolenaar33570922005-01-25 22:26:29 +0000597 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200598 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000599 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 ++emsg_off;
602
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200603 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 retval = -1;
605 else
606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100607 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000608 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 --emsg_off;
611
612 return retval;
613}
614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000616 * Top level evaluation function.
617 * Returns an allocated typval_T with the result.
618 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619 */
620 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200621eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000622{
623 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 evalarg_T evalarg;
625
626 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000627
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200628 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200629 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100630 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631
Bram Moolenaar37c83712020-06-30 21:18:36 +0200632 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000633 return tv;
634}
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100637 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
639 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000640 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643call_vim_function(
644 char_u *func,
645 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200646 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200647 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200650 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200654 funcexe.firstline = curwin->w_cursor.lnum;
655 funcexe.lastline = curwin->w_cursor.lnum;
656 funcexe.evaluate = TRUE;
657 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 if (ret == FAIL)
659 clear_tv(rettv);
660
661 return ret;
662}
663
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100665 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200667 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
668 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200670 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671call_func_retnr(
672 char_u *func,
673 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200674 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100675{
676 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100680 return -1;
681
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100683 clear_tv(&rettv);
684 return retval;
685}
686
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000687/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100688 * Call Vim script function like call_func_retnr() and drop the result.
689 * Returns FAIL when calling the function fails.
690 */
691 int
692call_func_noret(
693 char_u *func,
694 int argc,
695 typval_T *argv)
696{
697 typval_T rettv;
698
699 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
700 return FAIL;
701 clear_tv(&rettv);
702 return OK;
703}
704
705/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100706 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100707 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000708 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709 */
710 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711call_func_retstr(
712 char_u *func,
713 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200714 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715{
716 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
Bram Moolenaarded27a12018-08-01 19:06:03 +0200719 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720 return NULL;
721
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100722 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return retval;
725}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000726
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000727/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100728 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100729 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000730 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 */
732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100733call_func_retlist(
734 char_u *func,
735 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200736 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000737{
738 typval_T rettv;
739
Bram Moolenaarded27a12018-08-01 19:06:03 +0200740 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 return NULL;
742
743 if (rettv.v_type != VAR_LIST)
744 {
745 clear_tv(&rettv);
746 return NULL;
747 }
748
749 return rettv.vval.v_list;
750}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifdef FEAT_FOLDING
753/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100754 * Evaluate "arg", which is 'foldexpr'.
755 * Note: caller must set "curwin" to match "arg".
756 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
757 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200763 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000765 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
766 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000769 if (use_sandbox)
770 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200771 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200773 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 retval = 0;
775 else
776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 if (tv.v_type == VAR_NUMBER)
779 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000780 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = 0;
782 else
783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // If the result is a string, check if there is a non-digit before
785 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000786 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (!VIM_ISDIGIT(*s) && *s != '-')
788 *cp = *s++;
789 retval = atol((char *)s);
790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000794 if (use_sandbox)
795 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200796 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200797 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200799 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 * Get an lval: variable, Dict item or List item that can be assigned a value
805 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
806 * "name.key", "name.key[expr]" etc.
807 * Indexing only works if "name" is an existing List or Dictionary.
808 * "name" points to the start of the name.
809 * If "rettv" is not NULL it points to the value to be assigned.
810 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
811 * wrong; must end in space or cmd separator.
812 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100813 * flags:
814 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100815 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100816 * GLV_NO_AUTOLOAD: do not use script autoloading
817 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000819 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823get_lval(
824 char_u *name,
825 typval_T *rettv,
826 lval_T *lp,
827 int unlet,
828 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100829 int flags, // GLV_ values
830 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 char_u *expr_start, *expr_end;
834 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 dictitem_T *v;
836 typval_T var1;
837 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100842 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100843 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100844 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200847 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100851 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200853 lp->ll_name_end = find_name_end(name, NULL, NULL,
854 FNE_INCL_BR | fne_flags);
855 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 }
857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100858 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000859 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 if (expr_start != NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100864 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 && *p != '[' && *p != '.')
866 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200867 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870
871 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
872 if (lp->ll_exp_name == NULL)
873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Report an invalid expression in braces, unless the
875 // expression evaluation has been cancelled due to an
876 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (!aborting() && !quiet)
878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000879 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
882 }
883 }
884 lp->ll_name = lp->ll_exp_name;
885 }
886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 lp->ll_name = name;
889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 // "a: type" is declaring variable "a" with a type, not "a:".
893 if (p == name + 2 && p[-1] == ':')
894 {
895 --p;
896 lp->ll_name_end = p;
897 }
898 if (*p == ':')
899 {
900 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
901 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200903 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100904 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
905 if (lp->ll_type == NULL && !quiet)
906 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200907 lp->ll_name_end = tp;
908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 }
910 }
911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
914 return p;
915
916 cc = *p;
917 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100918 // When we would write to the variable pass &ht and prevent autoload.
919 writing = !(flags & GLV_READ_ONLY);
920 v = find_var(lp->ll_name, writing ? &ht : NULL,
921 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200923 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 if (v == NULL)
926 return NULL;
927
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100928 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
929 {
930 if (!quiet)
931 semsg(_(e_variable_already_declared), lp->ll_name);
932 return NULL;
933 }
934
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 /*
936 * Loop until no more [idx] or .key is following.
937 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100939 var1.v_type = VAR_UNKNOWN;
940 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200944 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100945 && !(lp->ll_tv->v_type == VAR_BLOB
946 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
963 svar_T *sv = find_typval_in_script(lp->ll_tv);
964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
1178 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 || lp->ll_n1 > bloblen
1180 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 {
1182 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001183 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 return NULL;
1186 }
1187 if (lp->ll_range && !lp->ll_empty2)
1188 {
1189 lp->ll_n2 = (long)tv_get_number(&var2);
1190 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001191 if (lp->ll_n2 < 0
1192 || lp->ll_n2 >= bloblen
1193 || lp->ll_n2 < lp->ll_n1)
1194 {
1195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001197 return NULL;
1198 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001199 }
1200 lp->ll_blob = lp->ll_tv->vval.v_blob;
1201 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001202 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 else
1205 {
1206 /*
1207 * Get the number and item for the only or first index of the List.
1208 */
1209 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001213 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001214 clear_tv(&var1);
1215
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001218 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 if (lp->ll_li == NULL)
1220 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001221 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001222 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar10c65862020-10-08 21:16:42 +02001227 if (lp->ll_valtype != NULL)
1228 // use the type of the member
1229 lp->ll_valtype = lp->ll_valtype->tt_member;
1230
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 /*
1232 * May need to find the item or absolute index for the second
1233 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * When no index given: "lp->ll_empty2" is TRUE.
1235 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001239 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 {
1247 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (lp->ll_n1 < 0)
1256 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1257 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 {
1259 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001262 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001263 }
1264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 }
1268
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return p;
1272}
1273
1274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279{
1280 vim_free(lp->ll_exp_name);
1281 vim_free(lp->ll_newkey);
1282}
1283
1284/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001285 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001287 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1288 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291set_var_lval(
1292 lval_T *lp,
1293 char_u *endp,
1294 typval_T *rettv,
1295 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001296 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1297 char_u *op,
1298 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299{
1300 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 listitem_T *ri;
1302 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303
1304 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001306 cc = *endp;
1307 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 if (lp->ll_blob != NULL)
1309 {
1310 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (op != NULL && *op != '=')
1313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001314 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 return;
1316 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001317 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001318 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319
1320 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1321 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001324 if (lp->ll_empty2)
1325 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1326
1327 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001329 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 return;
1331 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001332 if (lp->ll_empty2)
1333 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001335 ir = 0;
1336 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1337 blob_set(lp->ll_blob, il,
1338 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339 }
1340 else
1341 {
1342 val = (int)tv_get_number_chk(rettv, &error);
1343 if (!error)
1344 {
1345 garray_T *gap = &lp->ll_blob->bv_ga;
1346
1347 // Allow for appending a byte. Setting a byte beyond
1348 // the end is an error otherwise.
1349 if (lp->ll_n1 < gap->ga_len
1350 || (lp->ll_n1 == gap->ga_len
1351 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1352 {
1353 blob_set(lp->ll_blob, lp->ll_n1, val);
1354 if (lp->ll_n1 == gap->ga_len)
1355 ++gap->ga_len;
1356 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001357 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001358 }
1359 }
1360 }
1361 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001365 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001366 {
1367 emsg(_(e_cannot_mod));
1368 *endp = cc;
1369 return;
1370 }
1371
Bram Moolenaarff697e62019-02-12 22:28:33 +01001372 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001374 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001375 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001376 {
1377 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001378 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1379 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001380 && tv_op(&tv, rettv, op) == OK)
1381 set_var(lp->ll_name, &tv, FALSE);
1382 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001385 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001386 {
1387 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001388 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001389 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001390 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1391 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001392 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001393 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001395 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001396 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001397 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001398 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 else if (lp->ll_range)
1400 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001401 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001402 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001403
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001404 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001405 {
1406 emsg(_("E996: Cannot lock a range"));
1407 return;
1408 }
1409
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001410 /*
1411 * Check whether any of the list items is locked
1412 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001413 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001414 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001415 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001416 return;
1417 ri = ri->li_next;
1418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1419 break;
1420 ll_li = ll_li->li_next;
1421 ++ll_n1;
1422 }
1423
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 /*
1425 * Assign the List values to the list items.
1426 */
1427 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 if (op != NULL && *op != '=')
1430 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1431 else
1432 {
1433 clear_tv(&lp->ll_li->li_tv);
1434 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 ri = ri->li_next;
1437 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1438 break;
1439 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001441 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001442 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 ri = NULL;
1445 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001446 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 lp->ll_li = lp->ll_li->li_next;
1449 ++lp->ll_n1;
1450 }
1451 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001452 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 else if (lp->ll_empty2
1454 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001456 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 }
1458 else
1459 {
1460 /*
1461 * Assign to a List or Dictionary item.
1462 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001463 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001464 {
1465 emsg(_("E996: Cannot lock a list or dict"));
1466 return;
1467 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001468
1469 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001470 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001471 return;
1472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 if (lp->ll_newkey != NULL)
1474 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001475 if (op != NULL && *op != '=')
1476 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001477 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 return;
1479 }
1480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001481 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001482 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 if (di == NULL)
1484 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001485 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1486 {
1487 vim_free(di);
1488 return;
1489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001491 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 else if (op != NULL && *op != '=')
1493 {
1494 tv_op(lp->ll_tv, rettv, op);
1495 return;
1496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001499
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 /*
1501 * Assign the value to the variable or list item.
1502 */
1503 if (copy)
1504 copy_tv(rettv, lp->ll_tv);
1505 else
1506 {
1507 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001508 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510 }
1511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512}
1513
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001515 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1516 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001517 * Returns OK or FAIL.
1518 */
1519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001522 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 char_u numbuf[NUMBUFLEN];
1524 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001525 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001527 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001528 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001529 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530 {
1531 switch (tv1->v_type)
1532 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001533 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001534 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 case VAR_DICT:
1537 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001538 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001539 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001540 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001541 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001542 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 break;
1544
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001545 case VAR_BLOB:
1546 if (*op != '+' || tv2->v_type != VAR_BLOB)
1547 break;
1548 // BLOB += BLOB
1549 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1550 {
1551 blob_T *b1 = tv1->vval.v_blob;
1552 blob_T *b2 = tv2->vval.v_blob;
1553 int i, len = blob_len(b2);
1554 for (i = 0; i < len; i++)
1555 ga_append(&b1->bv_ga, blob_get(b2, i));
1556 }
1557 return OK;
1558
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 case VAR_LIST:
1560 if (*op != '+' || tv2->v_type != VAR_LIST)
1561 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001563 if (tv2->vval.v_list != NULL)
1564 {
1565 if (tv1->vval.v_list == NULL)
1566 {
1567 tv1->vval.v_list = tv2->vval.v_list;
1568 ++tv1->vval.v_list->lv_refcount;
1569 }
1570 else
1571 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 return OK;
1574
1575 case VAR_NUMBER:
1576 case VAR_STRING:
1577 if (tv2->v_type == VAR_LIST)
1578 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583#ifdef FEAT_FLOAT
1584 if (tv2->v_type == VAR_FLOAT)
1585 {
1586 float_T f = n;
1587
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 if (*op == '%')
1589 break;
1590 switch (*op)
1591 {
1592 case '+': f += tv2->vval.v_float; break;
1593 case '-': f -= tv2->vval.v_float; break;
1594 case '*': f *= tv2->vval.v_float; break;
1595 case '/': f /= tv2->vval.v_float; break;
1596 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_FLOAT;
1599 tv1->vval.v_float = f;
1600 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001601 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602#endif
1603 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001604 switch (*op)
1605 {
1606 case '+': n += tv_get_number(tv2); break;
1607 case '-': n -= tv_get_number(tv2); break;
1608 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001609 case '/': n = num_divide(n, tv_get_number(tv2),
1610 &failed); break;
1611 case '%': n = num_modulus(n, tv_get_number(tv2),
1612 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001614 clear_tv(tv1);
1615 tv1->v_type = VAR_NUMBER;
1616 tv1->vval.v_number = n;
1617 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 }
1619 else
1620 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 if (tv2->v_type == VAR_FLOAT)
1622 break;
1623
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001625 s = tv_get_string(tv1);
1626 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 clear_tv(tv1);
1628 tv1->v_type = VAR_STRING;
1629 tv1->vval.v_string = s;
1630 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001631 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001634#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001635 {
1636 float_T f;
1637
Bram Moolenaarff697e62019-02-12 22:28:33 +01001638 if (*op == '%' || *op == '.'
1639 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 && tv2->v_type != VAR_NUMBER
1641 && tv2->v_type != VAR_STRING))
1642 break;
1643 if (tv2->v_type == VAR_FLOAT)
1644 f = tv2->vval.v_float;
1645 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001646 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 switch (*op)
1648 {
1649 case '+': tv1->vval.v_float += f; break;
1650 case '-': tv1->vval.v_float -= f; break;
1651 case '*': tv1->vval.v_float *= f; break;
1652 case '/': tv1->vval.v_float /= f; break;
1653 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001654 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001655#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001656 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 }
1658 }
1659
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001660 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 return FAIL;
1662}
1663
1664/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 * Evaluate the expression used in a ":for var in expr" command.
1666 * "arg" points to "var".
1667 * Set "*errp" to TRUE for an error, FALSE otherwise;
1668 * Return a pointer that holds the info. Null when there is an error.
1669 */
1670 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671eval_for_line(
1672 char_u *arg,
1673 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001674 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001675 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676{
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 typval_T tv;
1680 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001681 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001683 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001685 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 if (fi == NULL)
1687 return NULL;
1688
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001689 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (expr == NULL)
1691 return fi;
1692
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001693 expr = skipwhite_and_linebreak(expr, evalarg);
1694 if (expr[0] != 'i' || expr[1] != 'n'
1695 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 return fi;
1699 }
1700
1701 if (skip)
1702 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001703 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1704 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 {
1706 *errp = FALSE;
1707 if (!skip)
1708 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001710 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711 l = tv.vval.v_list;
1712 if (l == NULL)
1713 {
1714 // a null list is like an empty list: do nothing
1715 clear_tv(&tv);
1716 }
1717 else
1718 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001720 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 // No need to increment the refcount, it's already set for
1723 // the list being used in "tv".
1724 fi->fi_list = l;
1725 list_add_watch(l, &fi->fi_lw);
1726 fi->fi_lw.lw_item = l->lv_first;
1727 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001728 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001730 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001731 fi->fi_bi = 0;
1732 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001734 typval_T btv;
1735
1736 // Make a copy, so that the iteration still works when the
1737 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001739 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001741 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001742 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001743 else if (tv.v_type == VAR_STRING)
1744 {
1745 fi->fi_byte_idx = 0;
1746 fi->fi_string = tv.vval.v_string;
1747 tv.vval.v_string = NULL;
1748 if (fi->fi_string == NULL)
1749 fi->fi_string = vim_strsave((char_u *)"");
1750 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 else
1752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001753 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 }
1756 }
1757 }
1758 if (skip)
1759 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001760 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761
1762 return fi;
1763}
1764
1765/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001766 * Used when looping over a :for line, skip the "in expr" part.
1767 */
1768 void
1769skip_for_lines(void *fi_void, evalarg_T *evalarg)
1770{
1771 forinfo_T *fi = (forinfo_T *)fi_void;
1772 int i;
1773
1774 for (i = 0; i < fi->fi_break_count; ++i)
1775 eval_next_line(evalarg);
1776}
1777
1778/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 * Use the first item in a ":for" list. Advance to the next.
1780 * Assign the values to the variable (list). "arg" points to the first one.
1781 * Return TRUE when a valid item was found, FALSE when at end of list or
1782 * something wrong.
1783 */
1784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001787 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001789 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001792 if (fi->fi_blob != NULL)
1793 {
1794 typval_T tv;
1795
1796 if (fi->fi_bi >= blob_len(fi->fi_blob))
1797 return FALSE;
1798 tv.v_type = VAR_NUMBER;
1799 tv.v_lock = VAR_FIXED;
1800 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1801 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001802 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001803 fi->fi_varcount, flag, NULL) == OK;
1804 }
1805
1806 if (fi->fi_string != NULL)
1807 {
1808 typval_T tv;
1809 int len;
1810
1811 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1812 if (len == 0)
1813 return FALSE;
1814 tv.v_type = VAR_STRING;
1815 tv.v_lock = VAR_FIXED;
1816 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1817 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001818 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001819 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001820 vim_free(tv.vval.v_string);
1821 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 }
1823
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 item = fi->fi_lw.lw_item;
1825 if (item == NULL)
1826 result = FALSE;
1827 else
1828 {
1829 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001830 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001831 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 }
1833 return result;
1834}
1835
1836/*
1837 * Free the structure used to store info used by ":for".
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001844 if (fi == NULL)
1845 return;
1846 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001847 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001849 list_unref(fi->fi_list);
1850 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001851 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001852 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001853 else
1854 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 vim_free(fi);
1856}
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001859set_context_for_expression(
1860 expand_T *xp,
1861 char_u *arg,
1862 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001864 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001868 if (cmdidx == CMD_let || cmdidx == CMD_var
1869 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 {
1871 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001872 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001875 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 {
1877 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001878 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001879 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 break;
1881 }
1882 return;
1883 }
1884 }
1885 else
1886 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1887 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 while ((xp->xp_pattern = vim_strpbrk(arg,
1889 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1890 {
1891 c = *xp->xp_pattern;
1892 if (c == '&')
1893 {
1894 c = xp->xp_pattern[1];
1895 if (c == '&')
1896 {
1897 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001898 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001903 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1904 xp->xp_pattern += 2;
1905
1906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 else if (c == '$')
1909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 xp->xp_context = EXPAND_ENV_VARS;
1912 }
1913 else if (c == '=')
1914 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001915 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 xp->xp_context = EXPAND_EXPRESSION;
1917 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001918 else if (c == '#'
1919 && xp->xp_context == EXPAND_EXPRESSION)
1920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001922 break;
1923 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001924 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 && xp->xp_context == EXPAND_FUNCTIONS
1926 && vim_strchr(xp->xp_pattern, '(') == NULL)
1927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 break;
1930 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001931 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001933 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1936 if (c == '\\' && xp->xp_pattern[1] != NUL)
1937 ++xp->xp_pattern;
1938 xp->xp_context = EXPAND_NOTHING;
1939 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001940 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001942 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1944 /* skip */ ;
1945 xp->xp_context = EXPAND_NOTHING;
1946 }
1947 else if (c == '|')
1948 {
1949 if (xp->xp_pattern[1] == '|')
1950 {
1951 ++xp->xp_pattern;
1952 xp->xp_context = EXPAND_EXPRESSION;
1953 }
1954 else
1955 xp->xp_context = EXPAND_COMMANDS;
1956 }
1957 else
1958 xp->xp_context = EXPAND_EXPRESSION;
1959 }
1960 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001961 // Doesn't look like something valid, expand as an expression
1962 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 arg = xp->xp_pattern;
1965 if (*arg != NUL)
1966 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1967 /* skip */ ;
1968 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001969
1970 // ":exe one two" completes "two"
1971 if ((cmdidx == CMD_execute
1972 || cmdidx == CMD_echo
1973 || cmdidx == CMD_echon
1974 || cmdidx == CMD_echomsg)
1975 && xp->xp_context == EXPAND_EXPRESSION)
1976 {
1977 for (;;)
1978 {
1979 char_u *n = skiptowhite(arg);
1980
1981 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1982 break;
1983 arg = skipwhite(n);
1984 }
1985 }
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 xp->xp_pattern = arg;
1988}
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001991 * Return TRUE if "pat" matches "text".
1992 * Does not use 'cpo' and always uses 'magic'.
1993 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001994 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001995pattern_match(char_u *pat, char_u *text, int ic)
1996{
1997 int matches = FALSE;
1998 char_u *save_cpo;
1999 regmatch_T regmatch;
2000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002002 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002003 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002004 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2005 if (regmatch.regprog != NULL)
2006 {
2007 regmatch.rm_ic = ic;
2008 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2009 vim_regfree(regmatch.regprog);
2010 }
2011 p_cpo = save_cpo;
2012 return matches;
2013}
2014
2015/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 * Handle a name followed by "(". Both for just "name(arg)" and for
2017 * "expr->name(arg)".
2018 * Returns OK or FAIL.
2019 */
2020 static int
2021eval_func(
2022 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002023 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 char_u *name,
2025 int name_len,
2026 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002027 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002028 typval_T *basetv) // "expr" for "expr->name(arg)"
2029{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002030 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031 char_u *s = name;
2032 int len = name_len;
2033 partial_T *partial;
2034 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002035 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036
2037 if (!evaluate)
2038 check_vars(s, len);
2039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // If "s" is the name of a variable of type VAR_FUNC
2041 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002042 s = deref_func_name(s, &len, &partial,
2043 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002045 // Need to make a copy, in case evaluating the arguments makes
2046 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002048 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002049 ret = FAIL;
2050 else
2051 {
2052 funcexe_T funcexe;
2053
2054 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002055 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 funcexe.firstline = curwin->w_cursor.lnum;
2057 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002058 funcexe.evaluate = evaluate;
2059 funcexe.partial = partial;
2060 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002061 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002062 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 }
2064 vim_free(s);
2065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002066 // If evaluate is FALSE rettv->v_type was not set in
2067 // get_func_tv, but it's needed in handle_subscript() to parse
2068 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2070 {
2071 rettv->vval.v_string = NULL;
2072 rettv->v_type = VAR_FUNC;
2073 }
2074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002075 // Stop the expression evaluation when immediately
2076 // aborting on error, or when an interrupt occurred or
2077 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002078 if (evaluate && aborting())
2079 {
2080 if (ret == OK)
2081 clear_tv(rettv);
2082 ret = FAIL;
2083 }
2084 return ret;
2085}
2086
2087/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002088 * Get the next line source line without advancing. But do skip over comment
2089 * lines.
2090 */
2091 static char_u *
2092getline_peek_skip_comments(evalarg_T *evalarg)
2093{
2094 for (;;)
2095 {
2096 char_u *next = getline_peek(evalarg->eval_getline,
2097 evalarg->eval_cookie);
2098 char_u *p;
2099
2100 if (next == NULL)
2101 break;
2102 p = skipwhite(next);
2103 if (*p != NUL && !vim9_comment_start(p))
2104 return next;
2105 (void)eval_next_line(evalarg);
2106 }
2107 return NULL;
2108}
2109
2110/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002111 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2112 * comment) and there is a next line, return the next line (skipping blanks)
2113 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2115 * "arg" must point somewhere inside a line, not at the start.
2116 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002117 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2119{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 char_u *p = skipwhite(arg);
2121
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002122 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002123 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002125 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002126 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002128 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129
2130 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002131 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002132 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002133 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136 {
2137 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002138 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 }
2140 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002141 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002142}
2143
2144/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002145 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002146 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002147 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002148eval_next_line(evalarg_T *evalarg)
2149{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002150 garray_T *gap = &evalarg->eval_ga;
2151 char_u *line;
2152
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002153 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002154 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2155 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002156 else
2157 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002158 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002159 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2160 {
2161 // Going to concatenate the lines after parsing.
2162 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2163 ++gap->ga_len;
2164 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002165 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002166 {
2167 vim_free(evalarg->eval_tofree);
2168 evalarg->eval_tofree = line;
2169 }
2170 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002171}
2172
Bram Moolenaare6b53242020-07-01 17:28:33 +02002173/*
2174 * Call eval_next_non_blank() and get the next line if needed.
2175 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002176 char_u *
2177skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2178{
2179 int getnext;
2180 char_u *p = skipwhite(arg);
2181
Bram Moolenaar962d7212020-07-04 14:15:00 +02002182 if (evalarg == NULL)
2183 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002184 eval_next_non_blank(p, evalarg, &getnext);
2185 if (getnext)
2186 return eval_next_line(evalarg);
2187 return p;
2188}
2189
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002191 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002192 */
2193 void
2194clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2195{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002196 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002197 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002198 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002199 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002200 if (eap != NULL)
2201 {
2202 // We may need to keep the original command line, e.g. for
2203 // ":let" it has the variable names. But we may also need the
2204 // new one, "nextcmd" points into it. Keep both.
2205 vim_free(eap->cmdline_tofree);
2206 eap->cmdline_tofree = *eap->cmdlinep;
2207 *eap->cmdlinep = evalarg->eval_tofree;
2208 }
2209 else
2210 vim_free(evalarg->eval_tofree);
2211 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002212 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002213
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002214 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2215 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002216 }
2217}
2218
2219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2223 */
2224
2225/*
2226 * Handle zero level expression.
2227 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002229 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 * Return OK or FAIL.
2232 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234eval0(
2235 char_u *arg,
2236 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002237 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 int ret;
2241 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002242 int did_emsg_before = did_emsg;
2243 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002244 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002248 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002250 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /*
2255 * Report the invalid expression unless the expression evaluation has
2256 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002257 * exception, or we already gave a more specific error.
2258 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002260 if (!aborting()
2261 && did_emsg == did_emsg_before
2262 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002263 && (flags & EVAL_CONSTANT) == 0
2264 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002265 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002266
2267 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002268 // a next command to avoid more errors, unless "|" is following, which
2269 // could only be a command separator.
2270 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2271 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002274
2275 if (eap != NULL)
2276 eap->nextcmd = check_nextcmd(p);
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return ret;
2279}
2280
2281/*
2282 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002283 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002284 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 *
2286 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002287 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002289 * Note: "rettv.v_lock" is not set.
2290 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * Return OK or FAIL.
2292 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002293 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002296 char_u *p;
2297 int getnext;
2298
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002299 CLEAR_POINTER(rettv);
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 /*
2302 * Get the first variable.
2303 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return FAIL;
2306
Bram Moolenaar793648f2020-06-26 21:28:25 +02002307 p = eval_next_non_blank(*arg, evalarg, &getnext);
2308 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002310 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 int result;
2312 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313 evalarg_T *evalarg_used = evalarg;
2314 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002316 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002317 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318
2319 if (evalarg == NULL)
2320 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002321 CLEAR_FIELD(local_evalarg);
2322 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002323 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 orig_flags = evalarg_used->eval_flags;
2325 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002327 if (getnext)
2328 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002329 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002330 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002331 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002332 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002333 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002334 clear_tv(rettv);
2335 return FAIL;
2336 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002337 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002338 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 int error = FALSE;
2344
Bram Moolenaar13106602020-10-04 16:06:05 +02002345 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002346 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002347 else if (vim9script)
2348 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002349 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 if (error || !op_falsy || !result)
2352 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 if (error)
2354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002358 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002360 if (op_falsy)
2361 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002362 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002363 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002364 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002365 clear_tv(rettv);
2366 return FAIL;
2367 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002368 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002369 evalarg_used->eval_flags = (op_falsy ? !result : result)
2370 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2371 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002372 {
2373 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002375 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002376 if (!op_falsy || !result)
2377 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002379 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002381 /*
2382 * Check for the ":".
2383 */
2384 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2385 if (*p != ':')
2386 {
2387 emsg(_(e_missing_colon));
2388 if (evaluate && result)
2389 clear_tv(rettv);
2390 evalarg_used->eval_flags = orig_flags;
2391 return FAIL;
2392 }
2393 if (getnext)
2394 *arg = eval_next_line(evalarg_used);
2395 else
2396 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002397 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002398 {
2399 error_white_both(p, 1);
2400 clear_tv(rettv);
2401 evalarg_used->eval_flags = orig_flags;
2402 return FAIL;
2403 }
2404 *arg = p;
2405 }
2406
2407 /*
2408 * Get the third variable. Recursive!
2409 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002410 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002411 {
2412 error_white_both(p, 1);
2413 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002414 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002415 return FAIL;
2416 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002417 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2418 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 if (eval1(arg, &var2, evalarg_used) == FAIL)
2421 {
2422 if (evaluate && result)
2423 clear_tv(rettv);
2424 evalarg_used->eval_flags = orig_flags;
2425 return FAIL;
2426 }
2427 if (evaluate && !result)
2428 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430
2431 if (evalarg == NULL)
2432 clear_evalarg(&local_evalarg, NULL);
2433 else
2434 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436
2437 return OK;
2438}
2439
2440/*
2441 * Handle first level expression:
2442 * expr2 || expr2 || expr2 logical OR
2443 *
2444 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 *
2447 * Return OK or FAIL.
2448 */
2449 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002452 char_u *p;
2453 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 /*
2456 * Get the first variable.
2457 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002458 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return FAIL;
2460
2461 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002464 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 evalarg_T *evalarg_used = evalarg;
2468 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 int evaluate;
2470 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 long result = FALSE;
2472 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002473 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002474 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002475
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 if (evalarg == NULL)
2477 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 CLEAR_FIELD(local_evalarg);
2479 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 orig_flags = evalarg_used->eval_flags;
2482 evaluate = orig_flags & EVAL_EVALUATE;
2483 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002485 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002486 result = tv_get_bool_chk(rettv, &error);
2487 else if (tv_get_number_chk(rettv, &error) != 0)
2488 result = TRUE;
2489 clear_tv(rettv);
2490 if (error)
2491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493
2494 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 while (p[0] == '|' && p[1] == '|')
2498 {
2499 if (getnext)
2500 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002501 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002502 {
2503 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2504 {
2505 error_white_both(p, 2);
2506 clear_tv(rettv);
2507 return FAIL;
2508 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002509 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002510 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511
2512 /*
2513 * Get the second variable.
2514 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002515 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2516 {
2517 error_white_both(p, 2);
2518 clear_tv(rettv);
2519 return FAIL;
2520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2522 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002525 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526
2527 /*
2528 * Compute the result.
2529 */
2530 if (evaluate && !result)
2531 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002532 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 result = tv_get_bool_chk(&var2, &error);
2534 else if (tv_get_number_chk(&var2, &error) != 0)
2535 result = TRUE;
2536 clear_tv(&var2);
2537 if (error)
2538 return FAIL;
2539 }
2540 if (evaluate)
2541 {
2542 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002543 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002544 rettv->v_type = VAR_BOOL;
2545 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002546 }
2547 else
2548 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002549 rettv->v_type = VAR_NUMBER;
2550 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002551 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553
2554 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002556
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 if (evalarg == NULL)
2558 clear_evalarg(&local_evalarg, NULL);
2559 else
2560 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
2563 return OK;
2564}
2565
2566/*
2567 * Handle second level expression:
2568 * expr3 && expr3 && expr3 logical AND
2569 *
2570 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002571 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 *
2573 * Return OK or FAIL.
2574 */
2575 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002576eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002578 char_u *p;
2579 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /*
2582 * Get the first variable.
2583 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return FAIL;
2586
2587 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 evalarg_T *evalarg_used = evalarg;
2594 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 int orig_flags;
2596 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597 long result = TRUE;
2598 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002599 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002600 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002601
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 if (evalarg == NULL)
2603 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 CLEAR_FIELD(local_evalarg);
2605 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002607 orig_flags = evalarg_used->eval_flags;
2608 evaluate = orig_flags & EVAL_EVALUATE;
2609 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002611 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 result = tv_get_bool_chk(rettv, &error);
2613 else if (tv_get_number_chk(rettv, &error) == 0)
2614 result = FALSE;
2615 clear_tv(rettv);
2616 if (error)
2617 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619
2620 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 while (p[0] == '&' && p[1] == '&')
2624 {
2625 if (getnext)
2626 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002627 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002628 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 {
2631 error_white_both(p, 2);
2632 clear_tv(rettv);
2633 return FAIL;
2634 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002635 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002636 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637
2638 /*
2639 * Get the second variable.
2640 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002641 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2642 {
2643 error_white_both(p, 2);
2644 clear_tv(rettv);
2645 return FAIL;
2646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2648 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002649 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002650 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002652 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653
2654 /*
2655 * Compute the result.
2656 */
2657 if (evaluate && result)
2658 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 result = tv_get_bool_chk(&var2, &error);
2661 else if (tv_get_number_chk(&var2, &error) == 0)
2662 result = FALSE;
2663 clear_tv(&var2);
2664 if (error)
2665 return FAIL;
2666 }
2667 if (evaluate)
2668 {
2669 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002671 rettv->v_type = VAR_BOOL;
2672 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002673 }
2674 else
2675 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002676 rettv->v_type = VAR_NUMBER;
2677 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002678 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002680
2681 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002683
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 if (evalarg == NULL)
2685 clear_evalarg(&local_evalarg, NULL);
2686 else
2687 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 return OK;
2691}
2692
2693/*
2694 * Handle third level expression:
2695 * var1 == var2
2696 * var1 =~ var2
2697 * var1 != var2
2698 * var1 !~ var2
2699 * var1 > var2
2700 * var1 >= var2
2701 * var1 < var2
2702 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002703 * var1 is var2
2704 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *
2706 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002707 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 *
2709 * Return OK or FAIL.
2710 */
2711 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002716 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002718 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Get the first variable.
2722 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002726 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002727 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002732 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002734 typval_T var2;
2735 int ic;
2736 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002737 int evaluate = evalarg == NULL
2738 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002739
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002740 if (getnext)
2741 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2743 {
2744 error_white_both(p, len);
2745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002748
Bram Moolenaar696ba232020-07-29 21:20:41 +02002749 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2750 {
2751 semsg(_(e_invexpr2), p);
2752 clear_tv(rettv);
2753 return FAIL;
2754 }
2755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002756 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (p[len] == '?')
2758 {
2759 ic = TRUE;
2760 ++len;
2761 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002762 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else if (p[len] == '#')
2764 {
2765 ic = FALSE;
2766 ++len;
2767 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002768 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002770 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 /*
2773 * Get the second variable.
2774 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002775 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2776 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002777 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 clear_tv(rettv);
2779 return FAIL;
2780 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002781 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002784 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 return FAIL;
2786 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002787 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002788 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002789 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002790
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002791 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002792 {
2793 ret = FAIL;
2794 clear_tv(rettv);
2795 }
2796 else
2797 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002798 clear_tv(&var2);
2799 return ret;
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
2803 return OK;
2804}
2805
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002806/*
2807 * Make a copy of blob "tv1" and append blob "tv2".
2808 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 void
2810eval_addblob(typval_T *tv1, typval_T *tv2)
2811{
2812 blob_T *b1 = tv1->vval.v_blob;
2813 blob_T *b2 = tv2->vval.v_blob;
2814 blob_T *b = blob_alloc();
2815 int i;
2816
2817 if (b != NULL)
2818 {
2819 for (i = 0; i < blob_len(b1); i++)
2820 ga_append(&b->bv_ga, blob_get(b1, i));
2821 for (i = 0; i < blob_len(b2); i++)
2822 ga_append(&b->bv_ga, blob_get(b2, i));
2823
2824 clear_tv(tv1);
2825 rettv_blob_set(tv1, b);
2826 }
2827}
2828
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002829/*
2830 * Make a copy of list "tv1" and append list "tv2".
2831 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 int
2833eval_addlist(typval_T *tv1, typval_T *tv2)
2834{
2835 typval_T var3;
2836
2837 // concatenate Lists
2838 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2839 {
2840 clear_tv(tv1);
2841 clear_tv(tv2);
2842 return FAIL;
2843 }
2844 clear_tv(tv1);
2845 *tv1 = var3;
2846 return OK;
2847}
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * Handle fourth level expression:
2851 * + number addition
2852 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002853 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002854 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 *
2856 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002857 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *
2859 * Return OK or FAIL.
2860 */
2861 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /*
2865 * Get the first variable.
2866 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return FAIL;
2869
2870 /*
2871 * Repeat computing, until no '+', '-' or '.' is following.
2872 */
2873 for (;;)
2874 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002875 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002876 int getnext;
2877 char_u *p;
2878 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002879 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 int concat;
2881 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002882 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002884 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002885 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 p = eval_next_non_blank(*arg, evalarg, &getnext);
2887 op = *p;
2888 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002889 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2890 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002892
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002893 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002894 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002896 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002897 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002898 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002899 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002900 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002901 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002902 clear_tv(rettv);
2903 return FAIL;
2904 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002905 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002906 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002907 if ((op != '+' || (rettv->v_type != VAR_LIST
2908 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002909#ifdef FEAT_FLOAT
2910 && (op == '.' || rettv->v_type != VAR_FLOAT)
2911#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002912 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002914 int error = FALSE;
2915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002916 // For "list + ...", an illegal use of the first operand as
2917 // a number cannot be determined before evaluating the 2nd
2918 // operand: if this is also a list, all is ok.
2919 // For "something . ...", "something - ..." or "non-list + ...",
2920 // we know that the first operand needs to be a string or number
2921 // without evaluating the 2nd operand. So check before to avoid
2922 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002923 if (op != '.')
2924 tv_get_number_chk(rettv, &error);
2925 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002926 {
2927 clear_tv(rettv);
2928 return FAIL;
2929 }
2930 }
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 /*
2933 * Get the second variable.
2934 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002935 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002936 {
2937 error_white_both(p, oplen);
2938 clear_tv(rettv);
2939 return FAIL;
2940 }
2941 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002942 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return FAIL;
2946 }
2947
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 /*
2951 * Compute the result.
2952 */
2953 if (op == '.')
2954 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002955 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2956 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002957 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958
Bram Moolenaar2e086612020-08-25 22:37:48 +02002959 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002960 || var2.v_type == VAR_CHANNEL
2961 || var2.v_type == VAR_JOB))
2962 emsg(_(e_inval_string));
2963#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002964 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002965 {
2966 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2967 var2.vval.v_float);
2968 s2 = buf2;
2969 }
2970#endif
2971 else
2972 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002973 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002974 {
2975 clear_tv(rettv);
2976 clear_tv(&var2);
2977 return FAIL;
2978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 clear_tv(rettv);
2981 rettv->v_type = VAR_STRING;
2982 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002984 else if (op == '+' && rettv->v_type == VAR_BLOB
2985 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002987 else if (op == '+' && rettv->v_type == VAR_LIST
2988 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002991 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 else
2994 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 int error = FALSE;
2996 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002998 float_T f1 = 0, f2 = 0;
2999
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 f1 = rettv->vval.v_float;
3003 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 else
3006#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003008 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 if (error)
3010 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003011 // This can only happen for "list + non-list" or
3012 // "blob + non-blob". For "non-list + ..." or
3013 // "something - ...", we returned before evaluating the
3014 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003015 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003016 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017 return FAIL;
3018 }
3019#ifdef FEAT_FLOAT
3020 if (var2.v_type == VAR_FLOAT)
3021 f1 = n1;
3022#endif
3023 }
3024#ifdef FEAT_FLOAT
3025 if (var2.v_type == VAR_FLOAT)
3026 {
3027 f2 = var2.vval.v_float;
3028 n2 = 0;
3029 }
3030 else
3031#endif
3032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003033 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034 if (error)
3035 {
3036 clear_tv(rettv);
3037 clear_tv(&var2);
3038 return FAIL;
3039 }
3040#ifdef FEAT_FLOAT
3041 if (rettv->v_type == VAR_FLOAT)
3042 f2 = n2;
3043#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046
3047#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003048 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3050 {
3051 if (op == '+')
3052 f1 = f1 + f2;
3053 else
3054 f1 = f1 - f2;
3055 rettv->v_type = VAR_FLOAT;
3056 rettv->vval.v_float = f1;
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059#endif
3060 {
3061 if (op == '+')
3062 n1 = n1 + n2;
3063 else
3064 n1 = n1 - n2;
3065 rettv->v_type = VAR_NUMBER;
3066 rettv->vval.v_number = n1;
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071 }
3072 return OK;
3073}
3074
3075/*
3076 * Handle fifth level expression:
3077 * * number multiplication
3078 * / number division
3079 * % number modulo
3080 *
3081 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003082 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 *
3084 * Return OK or FAIL.
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087eval6(
3088 char_u **arg,
3089 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003090 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003091 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003093#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003094 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * Get the first variable.
3099 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003100 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return FAIL;
3102
3103 /*
3104 * Repeat computing, until no '*', '/' or '%' is following.
3105 */
3106 for (;;)
3107 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108 int evaluate;
3109 int getnext;
3110 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003111 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003112 int op;
3113 varnumber_T n1, n2;
3114#ifdef FEAT_FLOAT
3115 float_T f1, f2;
3116#endif
3117 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003118
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003119 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003120 p = eval_next_non_blank(*arg, evalarg, &getnext);
3121 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003122 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003124
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003125 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003126 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003127 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003128 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003129 {
3130 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3131 {
3132 error_white_both(p, 1);
3133 clear_tv(rettv);
3134 return FAIL;
3135 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003136 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003139#ifdef FEAT_FLOAT
3140 f1 = 0;
3141 f2 = 0;
3142#endif
3143 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146#ifdef FEAT_FLOAT
3147 if (rettv->v_type == VAR_FLOAT)
3148 {
3149 f1 = rettv->vval.v_float;
3150 use_float = TRUE;
3151 n1 = 0;
3152 }
3153 else
3154#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003155 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003156 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003157 if (error)
3158 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 else
3161 n1 = 0;
3162
3163 /*
3164 * Get the second variable.
3165 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003166 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3167 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003168 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003169 clear_tv(rettv);
3170 return FAIL;
3171 }
3172 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 return FAIL;
3175
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003176 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178#ifdef FEAT_FLOAT
3179 if (var2.v_type == VAR_FLOAT)
3180 {
3181 if (!use_float)
3182 {
3183 f1 = n1;
3184 use_float = TRUE;
3185 }
3186 f2 = var2.vval.v_float;
3187 n2 = 0;
3188 }
3189 else
3190#endif
3191 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003192 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193 clear_tv(&var2);
3194 if (error)
3195 return FAIL;
3196#ifdef FEAT_FLOAT
3197 if (use_float)
3198 f2 = n2;
3199#endif
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 /*
3203 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206#ifdef FEAT_FLOAT
3207 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209 if (op == '*')
3210 f1 = f1 * f2;
3211 else if (op == '/')
3212 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003213# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003214 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003215 if (f2 == 0.0)
3216 {
3217 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003218 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003219 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003220 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003221 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003222 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003223 }
3224 else
3225 f1 = f1 / f2;
3226# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003227 // We rely on the floating point library to handle divide
3228 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003229 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003230# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003235 return FAIL;
3236 }
3237 rettv->v_type = VAR_FLOAT;
3238 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003243 int failed = FALSE;
3244
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 if (op == '*')
3246 n1 = n1 * n2;
3247 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003248 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003250 n1 = num_modulus(n1, n2, &failed);
3251 if (failed)
3252 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003253
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003254 rettv->v_type = VAR_NUMBER;
3255 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 }
3259
3260 return OK;
3261}
3262
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003263 int
3264eval_leader(char_u **arg, int vim9)
3265{
3266 char_u *s = *arg;
3267 char_u *p = *arg;
3268
3269 while (*p == '!' || *p == '-' || *p == '+')
3270 {
3271 char_u *n = skipwhite(p + 1);
3272
3273 // ++, --, -+ and +- are not accepted in Vim9 script
3274 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3275 {
3276 semsg(_(e_invexpr2), s);
3277 return FAIL;
3278 }
3279 p = n;
3280 }
3281 *arg = p;
3282 return OK;
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285/*
3286 * Handle sixth level expression:
3287 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003288 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003289 * "string" string constant
3290 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 * &option-name option value
3292 * @r register contents
3293 * identifier variable value
3294 * function() function call
3295 * $VAR environment variable
3296 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003297 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003299 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003300 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 *
3302 * Also handle:
3303 * ! in front logical NOT
3304 * - in front unary minus
3305 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003306 * trailing [] subscript in String or List
3307 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003308 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 *
3310 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003311 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 *
3313 * Return OK or FAIL.
3314 */
3315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003316eval7(
3317 char_u **arg,
3318 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003319 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003322 int evaluate = evalarg != NULL
3323 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int len;
3325 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 char_u *start_leader, *end_leader;
3327 int ret = OK;
3328 char_u *alias;
3329
3330 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003332 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003337 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003340 if (eval_leader(arg, in_vim9script()) == FAIL)
3341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 end_leader = *arg;
3343
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003344 if (**arg == '.' && (!isdigit(*(*arg + 1))
3345#ifdef FEAT_FLOAT
3346 || current_sctx.sc_version < 2
3347#endif
3348 ))
3349 {
3350 semsg(_(e_invexpr2), *arg);
3351 ++*arg;
3352 return FAIL;
3353 }
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 switch (**arg)
3356 {
3357 /*
3358 * Number constant.
3359 */
3360 case '0':
3361 case '1':
3362 case '2':
3363 case '3':
3364 case '4':
3365 case '5':
3366 case '6':
3367 case '7':
3368 case '8':
3369 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003370 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003371
3372 // Apply prefixed "-" and "+" now. Matters especially when
3373 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003374 if (ret == OK && evaluate && end_leader > start_leader
3375 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003376 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 break;
3378
3379 /*
3380 * String constant: "string".
3381 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 break;
3384
3385 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003386 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003388 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003389 break;
3390
3391 /*
3392 * List: [expr, expr]
3393 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003394 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 break;
3396
3397 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003398 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003399 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003400 case '#': if (in_vim9script())
3401 {
3402 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3403 }
3404 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003405 {
3406 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003407 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003408 }
3409 else
3410 ret = NOTDONE;
3411 break;
3412
3413 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003414 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003415 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003417 case '{': if (in_vim9script())
3418 ret = NOTDONE;
3419 else
3420 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003421 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003422 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 break;
3424
3425 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003426 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003428 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 break;
3430
3431 /*
3432 * Environment variable: $VAR.
3433 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
3436
3437 /*
3438 * Register contents: @r.
3439 */
3440 case '@': ++*arg;
3441 if (evaluate)
3442 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003443 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3444 semsg(_(e_syntax_error_at_str), *arg);
3445 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3446 emsg_invreg(**arg);
3447 else
3448 {
3449 rettv->v_type = VAR_STRING;
3450 rettv->vval.v_string = get_reg_contents(**arg,
3451 GREG_EXPR_SRC);
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 if (**arg != NUL)
3455 ++*arg;
3456 break;
3457
3458 /*
3459 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003460 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003462 case '(': ret = NOTDONE;
3463 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003464 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003465 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003466 if (ret == OK && evaluate)
3467 {
3468 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3469
3470 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003471 if (compile_def_function(ufunc,
3472 TRUE, PROFILING(ufunc), NULL) == FAIL)
3473 {
3474 clear_tv(rettv);
3475 ret = FAIL;
3476 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003477 }
3478 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003479 if (ret == NOTDONE)
3480 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003481 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003482 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003483
Bram Moolenaar9215f012020-06-27 21:18:00 +02003484 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003485 if (**arg == ')')
3486 ++*arg;
3487 else if (ret == OK)
3488 {
3489 emsg(_(e_missing_close));
3490 clear_tv(rettv);
3491 ret = FAIL;
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 break;
3495
Bram Moolenaar8c711452005-01-14 21:53:12 +00003496 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 break;
3498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003499
3500 if (ret == NOTDONE)
3501 {
3502 /*
3503 * Must be a variable or function name.
3504 * Can also be a curly-braces kind of name: {expr}.
3505 */
3506 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003507 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 if (alias != NULL)
3509 s = alias;
3510
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003511 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003512 ret = FAIL;
3513 else
3514 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003515 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3516
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003517 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3518 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003519 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003520 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003521 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003522 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003523 else if (flags & EVAL_CONSTANT)
3524 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003526 {
3527 // get the value of "true", "false" or a variable
3528 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3529 {
3530 rettv->v_type = VAR_BOOL;
3531 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003532 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003533 }
3534 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003535 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003536 {
3537 rettv->v_type = VAR_BOOL;
3538 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003539 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003540 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003541 else if (len == 4 && in_vim9script()
3542 && STRNCMP(s, "null", 4) == 0)
3543 {
3544 rettv->v_type = VAR_SPECIAL;
3545 rettv->vval.v_number = VVAL_NULL;
3546 ret = OK;
3547 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003548 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003549 ret = eval_variable(s, len, rettv, NULL,
3550 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003552 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003553 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003554 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003555 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003556 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003557 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003558 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003559 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560 }
3561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003562 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3563 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003564 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003565 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567 /*
3568 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3569 */
3570 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003571 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003572 return ret;
3573}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003574
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003575/*
3576 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003577 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003578 * Adjusts "end_leaderp" until it is at "start_leader".
3579 */
3580 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003581eval7_leader(
3582 typval_T *rettv,
3583 int numeric_only,
3584 char_u *start_leader,
3585 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003586{
3587 char_u *end_leader = *end_leaderp;
3588 int ret = OK;
3589 int error = FALSE;
3590 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003591 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003592#ifdef FEAT_FLOAT
3593 float_T f = 0.0;
3594
3595 if (rettv->v_type == VAR_FLOAT)
3596 f = rettv->vval.v_float;
3597 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003598#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003599 {
3600 while (VIM_ISWHITE(end_leader[-1]))
3601 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003602 if (in_vim9script() && end_leader[-1] == '!')
3603 val = tv2bool(rettv);
3604 else
3605 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003606 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003607 if (error)
3608 {
3609 clear_tv(rettv);
3610 ret = FAIL;
3611 }
3612 else
3613 {
3614 while (end_leader > start_leader)
3615 {
3616 --end_leader;
3617 if (*end_leader == '!')
3618 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003619 if (numeric_only)
3620 {
3621 ++end_leader;
3622 break;
3623 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003624#ifdef FEAT_FLOAT
3625 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003626 {
3627 if (in_vim9script())
3628 {
3629 rettv->v_type = VAR_BOOL;
3630 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3631 }
3632 else
3633 f = !f;
3634 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003635 else
3636#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003637 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003638 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003639 type = VAR_BOOL;
3640 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003641 }
3642 else if (*end_leader == '-')
3643 {
3644#ifdef FEAT_FLOAT
3645 if (rettv->v_type == VAR_FLOAT)
3646 f = -f;
3647 else
3648#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003649 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003650 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003651 type = VAR_NUMBER;
3652 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003653 }
3654 }
3655#ifdef FEAT_FLOAT
3656 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003658 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003659 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003661 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003662#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003665 if (in_vim9script())
3666 rettv->v_type = type;
3667 else
3668 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003669 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003672 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return ret;
3674}
3675
3676/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003677 * Call the function referred to in "rettv".
3678 */
3679 static int
3680call_func_rettv(
3681 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003682 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003683 typval_T *rettv,
3684 int evaluate,
3685 dict_T *selfdict,
3686 typval_T *basetv)
3687{
3688 partial_T *pt = NULL;
3689 funcexe_T funcexe;
3690 typval_T functv;
3691 char_u *s;
3692 int ret;
3693
3694 // need to copy the funcref so that we can clear rettv
3695 if (evaluate)
3696 {
3697 functv = *rettv;
3698 rettv->v_type = VAR_UNKNOWN;
3699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003700 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003701 if (functv.v_type == VAR_PARTIAL)
3702 {
3703 pt = functv.vval.v_partial;
3704 s = partial_name(pt);
3705 }
3706 else
3707 s = functv.vval.v_string;
3708 }
3709 else
3710 s = (char_u *)"";
3711
Bram Moolenaara80faa82020-04-12 19:37:17 +02003712 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003713 funcexe.firstline = curwin->w_cursor.lnum;
3714 funcexe.lastline = curwin->w_cursor.lnum;
3715 funcexe.evaluate = evaluate;
3716 funcexe.partial = pt;
3717 funcexe.selfdict = selfdict;
3718 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003719 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003721 // Clear the funcref afterwards, so that deleting it while
3722 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003723 if (evaluate)
3724 clear_tv(&functv);
3725
3726 return ret;
3727}
3728
3729/*
3730 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003731 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003732 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3733 */
3734 static int
3735eval_lambda(
3736 char_u **arg,
3737 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003738 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003740{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003741 int evaluate = evalarg != NULL
3742 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003743 typval_T base = *rettv;
3744 int ret;
3745
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003746 rettv->v_type = VAR_UNKNOWN;
3747
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003748 if (**arg == '{')
3749 {
3750 // ->{lambda}()
3751 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3752 }
3753 else
3754 {
3755 // ->(lambda)()
3756 ++*arg;
3757 ret = eval1(arg, rettv, evalarg);
3758 *arg = skipwhite_and_linebreak(*arg, evalarg);
3759 if (**arg != ')')
3760 {
3761 emsg(_(e_missing_close));
3762 ret = FAIL;
3763 }
3764 ++*arg;
3765 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003766 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003767 return FAIL;
3768 else if (**arg != '(')
3769 {
3770 if (verbose)
3771 {
3772 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003773 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003774 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003776 }
3777 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003778 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003779 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003780 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003781 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003782
3783 // Clear the funcref afterwards, so that deleting it while
3784 // evaluating the arguments is possible (see test55).
3785 if (evaluate)
3786 clear_tv(&base);
3787
3788 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003789}
3790
3791/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003792 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003793 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003794 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3795 */
3796 static int
3797eval_method(
3798 char_u **arg,
3799 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003800 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003801 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003802{
3803 char_u *name;
3804 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003805 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003806 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003807 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003808 int evaluate = evalarg != NULL
3809 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003810
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003811 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003812
Bram Moolenaarac92e252019-08-03 21:58:38 +02003813 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003814 len = get_name_len(arg, &alias, evaluate, TRUE);
3815 if (alias != NULL)
3816 name = alias;
3817
3818 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003819 {
3820 if (verbose)
3821 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003822 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003823 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003824 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003825 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003826 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003827 if (**arg != '(')
3828 {
3829 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003831 ret = FAIL;
3832 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003833 else if (VIM_ISWHITE((*arg)[-1]))
3834 {
3835 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003836 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003837 ret = FAIL;
3838 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003839 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003840 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003841 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003842 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003843
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003844 // Clear the funcref afterwards, so that deleting it while
3845 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003846 if (evaluate)
3847 clear_tv(&base);
3848
Bram Moolenaarac92e252019-08-03 21:58:38 +02003849 return ret;
3850}
3851
3852/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003853 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3854 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003855 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3856 */
3857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858eval_index(
3859 char_u **arg,
3860 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003861 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003862 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003863{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003864 int evaluate = evalarg != NULL
3865 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003866 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003868 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003869 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003870 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003871 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003872
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003873 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3874 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003875
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003876 init_tv(&var1);
3877 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003880 /*
3881 * dict.name
3882 */
3883 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003884 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003886 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003887 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003888 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003889 }
3890 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003892 /*
3893 * something[idx]
3894 *
3895 * Get the (first) variable from inside the [].
3896 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003897 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 if (**arg == ':')
3899 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003900 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003901 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003902 else if (vim9 && **arg == ':')
3903 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003904 semsg(_(e_white_space_required_before_and_after_str_at_str),
3905 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003906 clear_tv(&var1);
3907 return FAIL;
3908 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003909 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003910 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003911#ifdef FEAT_FLOAT
3912 // allow for indexing with float
3913 if (vim9 && rettv->v_type == VAR_DICT
3914 && var1.v_type == VAR_FLOAT)
3915 {
3916 var1.vval.v_string = typval_tostring(&var1, TRUE);
3917 var1.v_type = VAR_STRING;
3918 }
3919#endif
3920 if (tv_get_string_chk(&var1) == NULL)
3921 {
3922 // not a number or string
3923 clear_tv(&var1);
3924 return FAIL;
3925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003927
3928 /*
3929 * Get the second variable from inside the [:].
3930 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003931 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003932 if (**arg == ':')
3933 {
3934 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003935 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003936 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003937 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003938 semsg(_(e_white_space_required_before_and_after_str_at_str),
3939 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003940 if (!empty1)
3941 clear_tv(&var1);
3942 return FAIL;
3943 }
3944 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 if (**arg == ']')
3946 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003947 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003949 if (!empty1)
3950 clear_tv(&var1);
3951 return FAIL;
3952 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003953 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003955 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003956 if (!empty1)
3957 clear_tv(&var1);
3958 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 return FAIL;
3960 }
3961 }
3962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003964 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003965 if (**arg != ']')
3966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003967 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 clear_tv(&var1);
3970 if (range)
3971 clear_tv(&var2);
3972 return FAIL;
3973 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003974 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 }
3976
3977 if (evaluate)
3978 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003979 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003980 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003981 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003982
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003983 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003985 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003986 clear_tv(&var2);
3987 return res;
3988 }
3989 return OK;
3990}
3991
3992/*
3993 * Check if "rettv" can have an [index] or [sli:ce]
3994 */
3995 int
3996check_can_index(typval_T *rettv, int evaluate, int verbose)
3997{
3998 switch (rettv->v_type)
3999 {
4000 case VAR_FUNC:
4001 case VAR_PARTIAL:
4002 if (verbose)
4003 emsg(_("E695: Cannot index a Funcref"));
4004 return FAIL;
4005 case VAR_FLOAT:
4006#ifdef FEAT_FLOAT
4007 if (verbose)
4008 emsg(_(e_float_as_string));
4009 return FAIL;
4010#endif
4011 case VAR_BOOL:
4012 case VAR_SPECIAL:
4013 case VAR_JOB:
4014 case VAR_CHANNEL:
4015 if (verbose)
4016 emsg(_(e_cannot_index_special_variable));
4017 return FAIL;
4018 case VAR_UNKNOWN:
4019 case VAR_ANY:
4020 case VAR_VOID:
4021 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004023 emsg(_(e_cannot_index_special_variable));
4024 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004025 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004026 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004027
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004028 case VAR_STRING:
4029 case VAR_LIST:
4030 case VAR_DICT:
4031 case VAR_BLOB:
4032 break;
4033 case VAR_NUMBER:
4034 if (in_vim9script())
4035 emsg(_(e_cannot_index_number));
4036 break;
4037 }
4038 return OK;
4039}
4040
4041/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004042 * slice() function
4043 */
4044 void
4045f_slice(typval_T *argvars, typval_T *rettv)
4046{
4047 if (check_can_index(argvars, TRUE, FALSE) == OK)
4048 {
4049 copy_tv(argvars, rettv);
4050 eval_index_inner(rettv, TRUE, argvars + 1,
4051 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4052 TRUE, NULL, 0, FALSE);
4053 }
4054}
4055
4056/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004057 * Apply index or range to "rettv".
4058 * "var1" is the first index, NULL for [:expr].
4059 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004060 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4061 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004062 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4063 */
4064 int
4065eval_index_inner(
4066 typval_T *rettv,
4067 int is_range,
4068 typval_T *var1,
4069 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004070 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004071 char_u *key,
4072 int keylen,
4073 int verbose)
4074{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004075 varnumber_T n1, n2 = 0;
4076 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004077
4078 n1 = 0;
4079 if (var1 != NULL && rettv->v_type != VAR_DICT)
4080 n1 = tv_get_number(var1);
4081
4082 if (is_range)
4083 {
4084 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004086 if (verbose)
4087 emsg(_(e_cannot_slice_dictionary));
4088 return FAIL;
4089 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004090 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004091 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004092 else
4093 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004094 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004095
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004096 switch (rettv->v_type)
4097 {
4098 case VAR_UNKNOWN:
4099 case VAR_ANY:
4100 case VAR_VOID:
4101 case VAR_FUNC:
4102 case VAR_PARTIAL:
4103 case VAR_FLOAT:
4104 case VAR_BOOL:
4105 case VAR_SPECIAL:
4106 case VAR_JOB:
4107 case VAR_CHANNEL:
4108 break; // not evaluating, skipping over subscript
4109
4110 case VAR_NUMBER:
4111 case VAR_STRING:
4112 {
4113 char_u *s = tv_get_string(rettv);
4114
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004116 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004117 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004118 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004119 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004120 else
4121 s = char_from_string(s, n1);
4122 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // The resulting variable is a substring. If the indexes
4126 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004127 if (n1 < 0)
4128 {
4129 n1 = len + n1;
4130 if (n1 < 0)
4131 n1 = 0;
4132 }
4133 if (n2 < 0)
4134 n2 = len + n2;
4135 else if (n2 >= len)
4136 n2 = len;
4137 if (n1 >= len || n2 < 0 || n1 > n2)
4138 s = NULL;
4139 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004140 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 }
4142 else
4143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // The resulting variable is a string of a single
4145 // character. If the index is too big or negative the
4146 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004147 if (n1 >= len || n1 < 0)
4148 s = NULL;
4149 else
4150 s = vim_strnsave(s + n1, 1);
4151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(rettv);
4153 rettv->v_type = VAR_STRING;
4154 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 }
4156 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004157
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 case VAR_BLOB:
4159 len = blob_len(rettv->vval.v_blob);
4160 if (is_range)
4161 {
4162 // The resulting variable is a sub-blob. If the indexes
4163 // are out of range the result is empty.
4164 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004165 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004166 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004167 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 n1 = 0;
4169 }
4170 if (n2 < 0)
4171 n2 = len + n2;
4172 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004173 n2 = len - (exclusive ? 0 : 1);
4174 if (exclusive)
4175 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004176 if (n1 >= len || n2 < 0 || n1 > n2)
4177 {
4178 clear_tv(rettv);
4179 rettv->v_type = VAR_BLOB;
4180 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004181 }
4182 else
4183 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004184 blob_T *blob = blob_alloc();
4185 long i;
4186
4187 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004188 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004190 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004191 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004192 return FAIL;
4193 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004194 blob->bv_ga.ga_len = n2 - n1 + 1;
4195 for (i = n1; i <= n2; i++)
4196 blob_set(blob, i - n1,
4197 blob_get(rettv->vval.v_blob, i));
4198
4199 clear_tv(rettv);
4200 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004202 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004203 }
4204 else
4205 {
4206 // The resulting variable is a byte value.
4207 // If the index is too big or negative that is an error.
4208 if (n1 < 0)
4209 n1 = len + n1;
4210 if (n1 < len && n1 >= 0)
4211 {
4212 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004214 clear_tv(rettv);
4215 rettv->v_type = VAR_NUMBER;
4216 rettv->vval.v_number = v;
4217 }
4218 else
4219 semsg(_(e_blobidx), n1);
4220 }
4221 break;
4222
4223 case VAR_LIST:
4224 if (var1 == NULL)
4225 n1 = 0;
4226 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004227 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004228 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004229 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 return FAIL;
4231 break;
4232
4233 case VAR_DICT:
4234 {
4235 dictitem_T *item;
4236 typval_T tmp;
4237
4238 if (key == NULL)
4239 {
4240 key = tv_get_string_chk(var1);
4241 if (key == NULL)
4242 return FAIL;
4243 }
4244
4245 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4246
4247 if (item == NULL && verbose)
4248 semsg(_(e_dictkey), key);
4249 if (item == NULL)
4250 return FAIL;
4251
4252 copy_tv(&item->di_tv, &tmp);
4253 clear_tv(rettv);
4254 *rettv = tmp;
4255 }
4256 break;
4257 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004258 return OK;
4259}
4260
4261/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004262 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004263 */
4264 char_u *
4265partial_name(partial_T *pt)
4266{
4267 if (pt->pt_name != NULL)
4268 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004269 if (pt->pt_func != NULL)
4270 return pt->pt_func->uf_name;
4271 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004272}
4273
Bram Moolenaarddecc252016-04-06 22:59:37 +02004274 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004275partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004276{
4277 int i;
4278
4279 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004280 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004281 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004282 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004283 if (pt->pt_name != NULL)
4284 {
4285 func_unref(pt->pt_name);
4286 vim_free(pt->pt_name);
4287 }
4288 else
4289 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004290
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004291 // Decrease the reference count for the context of a closure. If down
4292 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004293 if (pt->pt_funcstack != NULL)
4294 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004295 --pt->pt_funcstack->fs_refcount;
4296 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004297 }
4298
Bram Moolenaarddecc252016-04-06 22:59:37 +02004299 vim_free(pt);
4300}
4301
4302/*
4303 * Unreference a closure: decrement the reference count and free it when it
4304 * becomes zero.
4305 */
4306 void
4307partial_unref(partial_T *pt)
4308{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004309 if (pt != NULL)
4310 {
4311 if (--pt->pt_refcount <= 0)
4312 partial_free(pt);
4313
4314 // If the reference count goes down to one, the funcstack may be the
4315 // only reference and can be freed if no other partials reference it.
4316 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4317 funcstack_check_refcount(pt->pt_funcstack);
4318 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004319}
4320
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004321/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004322 * Return the next (unique) copy ID.
4323 * Used for serializing nested structures.
4324 */
4325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004326get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004327{
4328 current_copyID += COPYID_INC;
4329 return current_copyID;
4330}
4331
4332/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004333 * Garbage collection for lists and dictionaries.
4334 *
4335 * We use reference counts to be able to free most items right away when they
4336 * are no longer used. But for composite items it's possible that it becomes
4337 * unused while the reference count is > 0: When there is a recursive
4338 * reference. Example:
4339 * :let l = [1, 2, 3]
4340 * :let d = {9: l}
4341 * :let l[1] = d
4342 *
4343 * Since this is quite unusual we handle this with garbage collection: every
4344 * once in a while find out which lists and dicts are not referenced from any
4345 * variable.
4346 *
4347 * Here is a good reference text about garbage collection (refers to Python
4348 * but it applies to all reference-counting mechanisms):
4349 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004350 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004351
4352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004353 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004354 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004355 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004356 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004357 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004358garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004359{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004360 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004361 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004362 buf_T *buf;
4363 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004364 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004365 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004366
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004367 if (!testing)
4368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004369 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004370 want_garbage_collect = FALSE;
4371 may_garbage_collect = FALSE;
4372 garbage_collect_at_exit = FALSE;
4373 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004374
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004375 // The execution stack can grow big, limit the size.
4376 if (exestack.ga_maxlen - exestack.ga_len > 500)
4377 {
4378 size_t new_len;
4379 char_u *pp;
4380 int n;
4381
4382 // Keep 150% of the current size, with a minimum of the growth size.
4383 n = exestack.ga_len / 2;
4384 if (n < exestack.ga_growsize)
4385 n = exestack.ga_growsize;
4386
4387 // Don't make it bigger though.
4388 if (exestack.ga_len + n < exestack.ga_maxlen)
4389 {
4390 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4391 pp = vim_realloc(exestack.ga_data, new_len);
4392 if (pp == NULL)
4393 return FAIL;
4394 exestack.ga_maxlen = exestack.ga_len + n;
4395 exestack.ga_data = pp;
4396 }
4397 }
4398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004399 // We advance by two because we add one for items referenced through
4400 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004401 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004402
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004403 /*
4404 * 1. Go through all accessible variables and mark all lists and dicts
4405 * with copyID.
4406 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 // Don't free variables in the previous_funccal list unless they are only
4409 // referenced through previous_funccal. This must be first, because if
4410 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004413 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004414 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004416 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004417 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004418 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4419 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004421 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004422 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004423 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4424 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004425 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4427 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004428#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004429 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004430 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4431 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004432 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004433 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004434 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4435 NULL, NULL);
4436#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004438 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004439 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004440 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4441 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004443 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004445 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004449 abort = abort || set_ref_in_functions(copyID);
4450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004451 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004455 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004456
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004457 // callbacks in buffers
4458 abort = abort || set_ref_in_buffers(copyID);
4459
Bram Moolenaar1dced572012-04-05 16:54:08 +02004460#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004462#endif
4463
Bram Moolenaardb913952012-06-29 12:54:53 +02004464#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004466#endif
4467
4468#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004469 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004470#endif
4471
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004473 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004474 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004475#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004476#ifdef FEAT_NETBEANS_INTG
4477 abort = abort || set_ref_in_nb_channel(copyID);
4478#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004479
Bram Moolenaare3188e22016-05-31 21:13:04 +02004480#ifdef FEAT_TIMERS
4481 abort = abort || set_ref_in_timer(copyID);
4482#endif
4483
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004484#ifdef FEAT_QUICKFIX
4485 abort = abort || set_ref_in_quickfix(copyID);
4486#endif
4487
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004488#ifdef FEAT_TERMINAL
4489 abort = abort || set_ref_in_term(copyID);
4490#endif
4491
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004492#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004493 abort = abort || set_ref_in_popups(copyID);
4494#endif
4495
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004496 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004497 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004498 /*
4499 * 2. Free lists and dictionaries that are not referenced.
4500 */
4501 did_free = free_unref_items(copyID);
4502
4503 /*
4504 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004505 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004506 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004508 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 else if (p_verbose > 0)
4510 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004511 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004513
4514 return did_free;
4515}
4516
4517/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004518 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004519 */
4520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004521free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004522{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004523 int did_free = FALSE;
4524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 // Let all "free" functions know that we are here. This means no
4526 // dictionaries, lists, channels or jobs are to be freed, because we will
4527 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004528 in_free_unref_items = TRUE;
4529
4530 /*
4531 * PASS 1: free the contents of the items. We don't free the items
4532 * themselves yet, so that it is possible to decrement refcount counters
4533 */
4534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004536 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004539 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004540
4541#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // Go through the list of jobs and free items without the copyID. This
4543 // must happen before doing channels, because jobs refer to channels, but
4544 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004545 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004548 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4549#endif
4550
4551 /*
4552 * PASS 2: free the items themselves.
4553 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004554 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004555 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004556
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004557#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004558 // Go through the list of jobs and free items without the copyID. This
4559 // must happen before doing channels, because jobs refer to channels, but
4560 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004561 free_unused_jobs(copyID, COPYID_MASK);
4562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004564 free_unused_channels(copyID, COPYID_MASK);
4565#endif
4566
4567 in_free_unref_items = FALSE;
4568
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004569 return did_free;
4570}
4571
4572/*
4573 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 * "list_stack" is used to add lists to be marked. Can be NULL.
4575 *
4576 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004577 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004579set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580{
4581 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004582 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004583 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004584 hashtab_T *cur_ht;
4585 ht_stack_T *ht_stack = NULL;
4586 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004587
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 cur_ht = ht;
4589 for (;;)
4590 {
4591 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004593 // Mark each item in the hashtab. If the item contains a hashtab
4594 // it is added to ht_stack, if it contains a list it is added to
4595 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004596 todo = (int)cur_ht->ht_used;
4597 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4598 if (!HASHITEM_EMPTY(hi))
4599 {
4600 --todo;
4601 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4602 &ht_stack, list_stack);
4603 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004604 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605
4606 if (ht_stack == NULL)
4607 break;
4608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004609 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 cur_ht = ht_stack->ht;
4611 tempitem = ht_stack;
4612 ht_stack = ht_stack->prev;
4613 free(tempitem);
4614 }
4615
4616 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617}
4618
4619/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004620 * Mark a dict and its items with "copyID".
4621 * Returns TRUE if setting references failed somehow.
4622 */
4623 int
4624set_ref_in_dict(dict_T *d, int copyID)
4625{
4626 if (d != NULL && d->dv_copyID != copyID)
4627 {
4628 d->dv_copyID = copyID;
4629 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4630 }
4631 return FALSE;
4632}
4633
4634/*
4635 * Mark a list and its items with "copyID".
4636 * Returns TRUE if setting references failed somehow.
4637 */
4638 int
4639set_ref_in_list(list_T *ll, int copyID)
4640{
4641 if (ll != NULL && ll->lv_copyID != copyID)
4642 {
4643 ll->lv_copyID = copyID;
4644 return set_ref_in_list_items(ll, copyID, NULL);
4645 }
4646 return FALSE;
4647}
4648
4649/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004650 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004651 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4652 *
4653 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004654 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004655 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004656set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004657{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004658 listitem_T *li;
4659 int abort = FALSE;
4660 list_T *cur_l;
4661 list_stack_T *list_stack = NULL;
4662 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004663
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004664 cur_l = l;
4665 for (;;)
4666 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004667 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // Mark each item in the list. If the item contains a hashtab
4669 // it is added to ht_stack, if it contains a list it is added to
4670 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004671 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4672 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4673 ht_stack, &list_stack);
4674 if (list_stack == NULL)
4675 break;
4676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004677 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004678 cur_l = list_stack->list;
4679 tempitem = list_stack;
4680 list_stack = list_stack->prev;
4681 free(tempitem);
4682 }
4683
4684 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004685}
4686
4687/*
4688 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 * "list_stack" is used to add lists to be marked. Can be NULL.
4690 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4691 *
4692 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004693 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004695set_ref_in_item(
4696 typval_T *tv,
4697 int copyID,
4698 ht_stack_T **ht_stack,
4699 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004700{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004702
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004703 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004704 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004705 dict_T *dd = tv->vval.v_dict;
4706
Bram Moolenaara03f2332016-02-06 18:09:59 +01004707 if (dd != NULL && dd->dv_copyID != copyID)
4708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004710 dd->dv_copyID = copyID;
4711 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004712 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004713 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4714 }
4715 else
4716 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004717 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4718
Bram Moolenaara03f2332016-02-06 18:09:59 +01004719 if (newitem == NULL)
4720 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004721 else
4722 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004723 newitem->ht = &dd->dv_hashtab;
4724 newitem->prev = *ht_stack;
4725 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004726 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004727 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004728 }
4729 }
4730 else if (tv->v_type == VAR_LIST)
4731 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 list_T *ll = tv->vval.v_list;
4733
Bram Moolenaara03f2332016-02-06 18:09:59 +01004734 if (ll != NULL && ll->lv_copyID != copyID)
4735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004737 ll->lv_copyID = copyID;
4738 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004739 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004740 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004741 }
4742 else
4743 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004744 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4745
Bram Moolenaara03f2332016-02-06 18:09:59 +01004746 if (newitem == NULL)
4747 abort = TRUE;
4748 else
4749 {
4750 newitem->list = ll;
4751 newitem->prev = *list_stack;
4752 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004753 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004754 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004755 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004756 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004757 else if (tv->v_type == VAR_FUNC)
4758 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004759 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004760 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004761 else if (tv->v_type == VAR_PARTIAL)
4762 {
4763 partial_T *pt = tv->vval.v_partial;
4764 int i;
4765
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004766 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004767 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004768 // Didn't see this partial yet.
4769 pt->pt_copyID = copyID;
4770
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004771 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004772
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004773 if (pt->pt_dict != NULL)
4774 {
4775 typval_T dtv;
4776
4777 dtv.v_type = VAR_DICT;
4778 dtv.vval.v_dict = pt->pt_dict;
4779 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4780 }
4781
4782 for (i = 0; i < pt->pt_argc; ++i)
4783 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4784 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004785 if (pt->pt_funcstack != NULL)
4786 {
4787 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4788
4789 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4790 abort = abort || set_ref_in_item(stack + i, copyID,
4791 ht_stack, list_stack);
4792 }
4793
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004794 }
4795 }
4796#ifdef FEAT_JOB_CHANNEL
4797 else if (tv->v_type == VAR_JOB)
4798 {
4799 job_T *job = tv->vval.v_job;
4800 typval_T dtv;
4801
4802 if (job != NULL && job->jv_copyID != copyID)
4803 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004804 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004805 if (job->jv_channel != NULL)
4806 {
4807 dtv.v_type = VAR_CHANNEL;
4808 dtv.vval.v_channel = job->jv_channel;
4809 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4810 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004811 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004812 {
4813 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004814 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004815 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4816 }
4817 }
4818 }
4819 else if (tv->v_type == VAR_CHANNEL)
4820 {
4821 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004822 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004823 typval_T dtv;
4824 jsonq_T *jq;
4825 cbq_T *cq;
4826
4827 if (ch != NULL && ch->ch_copyID != copyID)
4828 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004829 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004830 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 {
4832 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4833 jq = jq->jq_next)
4834 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4835 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4836 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004837 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 {
4839 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004840 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004841 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4842 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004843 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004844 {
4845 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004846 dtv.vval.v_partial =
4847 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004848 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4849 }
4850 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004851 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004852 {
4853 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004854 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004855 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4856 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004857 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004858 {
4859 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004860 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004861 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4862 }
4863 }
4864 }
4865#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004866 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004867}
4868
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 * Return a string with the string representation of a variable.
4871 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004872 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004873 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004874 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004875 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004876 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004877 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4878 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004879 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004881 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004882echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004883 typval_T *tv,
4884 char_u **tofree,
4885 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004886 int copyID,
4887 int echo_style,
4888 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004889 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004891 static int recurse = 0;
4892 char_u *r = NULL;
4893
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004895 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004896 if (!did_echo_string_emsg)
4897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // Only give this message once for a recursive call to avoid
4899 // flooding the user with errors. And stop iterating over lists
4900 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004901 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004902 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004904 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004905 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004906 }
4907 ++recurse;
4908
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 switch (tv->v_type)
4910 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004911 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004912 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004913 {
4914 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004915 r = tv->vval.v_string;
4916 if (r == NULL)
4917 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004918 }
4919 else
4920 {
4921 *tofree = string_quote(tv->vval.v_string, FALSE);
4922 r = *tofree;
4923 }
4924 break;
4925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004926 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004927 if (echo_style)
4928 {
4929 *tofree = NULL;
4930 r = tv->vval.v_string;
4931 }
4932 else
4933 {
4934 *tofree = string_quote(tv->vval.v_string, TRUE);
4935 r = *tofree;
4936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004937 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004938
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004939 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004940 {
4941 partial_T *pt = tv->vval.v_partial;
4942 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004943 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004944 garray_T ga;
4945 int i;
4946 char_u *tf;
4947
4948 ga_init2(&ga, 1, 100);
4949 ga_concat(&ga, (char_u *)"function(");
4950 if (fname != NULL)
4951 {
4952 ga_concat(&ga, fname);
4953 vim_free(fname);
4954 }
4955 if (pt != NULL && pt->pt_argc > 0)
4956 {
4957 ga_concat(&ga, (char_u *)", [");
4958 for (i = 0; i < pt->pt_argc; ++i)
4959 {
4960 if (i > 0)
4961 ga_concat(&ga, (char_u *)", ");
4962 ga_concat(&ga,
4963 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4964 vim_free(tf);
4965 }
4966 ga_concat(&ga, (char_u *)"]");
4967 }
4968 if (pt != NULL && pt->pt_dict != NULL)
4969 {
4970 typval_T dtv;
4971
4972 ga_concat(&ga, (char_u *)", ");
4973 dtv.v_type = VAR_DICT;
4974 dtv.vval.v_dict = pt->pt_dict;
4975 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4976 vim_free(tf);
4977 }
4978 ga_concat(&ga, (char_u *)")");
4979
4980 *tofree = ga.ga_data;
4981 r = *tofree;
4982 break;
4983 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004984
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004985 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004986 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004987 break;
4988
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004990 if (tv->vval.v_list == NULL)
4991 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004992 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004993 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004994 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004995 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004996 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4997 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004998 {
4999 *tofree = NULL;
5000 r = (char_u *)"[...]";
5001 }
5002 else
5003 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005004 int old_copyID = tv->vval.v_list->lv_copyID;
5005
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005006 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005007 *tofree = list2string(tv, copyID, restore_copyID);
5008 if (restore_copyID)
5009 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005010 r = *tofree;
5011 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005012 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005013
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005015 if (tv->vval.v_dict == NULL)
5016 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005017 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005018 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005019 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005020 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005021 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5022 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005023 {
5024 *tofree = NULL;
5025 r = (char_u *)"{...}";
5026 }
5027 else
5028 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005029 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005030
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005031 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005032 *tofree = dict2string(tv, copyID, restore_copyID);
5033 if (restore_copyID)
5034 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035 r = *tofree;
5036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005037 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005040 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005041 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005043 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005044 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005045 break;
5046
Bram Moolenaar835dc632016-02-07 14:27:38 +01005047 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005048 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005049 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005050 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005051 if (composite_val)
5052 {
5053 *tofree = string_quote(r, FALSE);
5054 r = *tofree;
5055 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005056 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005057
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005059#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005060 *tofree = NULL;
5061 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5062 r = numbuf;
5063 break;
5064#endif
5065
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005066 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005067 case VAR_SPECIAL:
5068 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005069 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005070 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005072
Bram Moolenaar8502c702014-06-17 12:51:16 +02005073 if (--recurse == 0)
5074 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076}
5077
5078/*
5079 * Return a string with the string representation of a variable.
5080 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5081 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005082 * Does not put quotes around strings, as ":echo" displays values.
5083 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5084 * May return NULL.
5085 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005087echo_string(
5088 typval_T *tv,
5089 char_u **tofree,
5090 char_u *numbuf,
5091 int copyID)
5092{
5093 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5094}
5095
5096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005097 * Return string "str" in ' quotes, doubling ' characters.
5098 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103{
Bram Moolenaar33570922005-01-25 22:26:29 +00005104 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 char_u *p, *r, *s;
5106
Bram Moolenaar33570922005-01-25 22:26:29 +00005107 len = (function ? 13 : 3);
5108 if (str != NULL)
5109 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005110 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005111 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005112 if (*p == '\'')
5113 ++len;
5114 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005115 s = r = alloc(len);
5116 if (r != NULL)
5117 {
5118 if (function)
5119 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 r += 10;
5122 }
5123 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005125 if (str != NULL)
5126 for (p = str; *p != NUL; )
5127 {
5128 if (*p == '\'')
5129 *r++ = '\'';
5130 MB_COPY_CHAR(p, r);
5131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 if (function)
5134 *r++ = ')';
5135 *r++ = NUL;
5136 }
5137 return s;
5138}
5139
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005140#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141/*
5142 * Convert the string "text" to a floating point number.
5143 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5144 * this always uses a decimal point.
5145 * Returns the length of the text that was consumed.
5146 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005148string2float(
5149 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005151{
5152 char *s = (char *)text;
5153 float_T f;
5154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005156 if (STRNICMP(text, "inf", 3) == 0)
5157 {
5158 *value = INFINITY;
5159 return 3;
5160 }
5161 if (STRNICMP(text, "-inf", 3) == 0)
5162 {
5163 *value = -INFINITY;
5164 return 4;
5165 }
5166 if (STRNICMP(text, "nan", 3) == 0)
5167 {
5168 *value = NAN;
5169 return 3;
5170 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 f = strtod(s, &s);
5172 *value = f;
5173 return (int)((char_u *)s - text);
5174}
5175#endif
5176
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005177/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005178 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5179 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005180 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005181 */
5182 int
5183buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5184{
5185 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005186 char_u *t;
5187 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005188
5189 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5190 return -1;
5191
5192 if (lnum > buf->b_ml.ml_line_count)
5193 lnum = buf->b_ml.ml_line_count;
5194
5195 str = ml_get_buf(buf, lnum, FALSE);
5196 if (str == NULL)
5197 return -1;
5198
5199 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005200 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005201
Bram Moolenaar91458462021-01-13 20:08:38 +01005202 // count the number of characters
5203 t = str;
5204 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5205 t += mb_ptr2len(t);
5206
5207 // In insert mode, when the cursor is at the end of a non-empty line,
5208 // byteidx points to the NUL character immediately past the end of the
5209 // string. In this case, add one to the character count.
5210 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5211 count++;
5212
5213 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214}
5215
5216/*
5217 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005218 * byte index. Works only for loaded buffers. Returns -1 on failure.
5219 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005220 */
5221 int
5222buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5223{
5224 char_u *str;
5225 char_u *t;
5226
5227 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5228 return -1;
5229
5230 if (lnum > buf->b_ml.ml_line_count)
5231 lnum = buf->b_ml.ml_line_count;
5232
5233 str = ml_get_buf(buf, lnum, FALSE);
5234 if (str == NULL)
5235 return -1;
5236
5237 // Convert the character offset to a byte offset
5238 t = str;
5239 while (*t != NUL && --charidx > 0)
5240 t += mb_ptr2len(t);
5241
Bram Moolenaar91458462021-01-13 20:08:38 +01005242 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005243}
5244
5245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005247 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005250var2fpos(
5251 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005253 int *fnum, // set to fnum for '0, 'A, etc.
5254 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005256 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005258 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005260 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005261 if (varp->v_type == VAR_LIST)
5262 {
5263 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005264 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005265 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005266 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005267
5268 l = varp->vval.v_list;
5269 if (l == NULL)
5270 return NULL;
5271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005273 pos.lnum = list_find_nr(l, 0L, &error);
5274 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005276 if (charcol)
5277 len = (long)mb_charlen(ml_get(pos.lnum));
5278 else
5279 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005280
Bram Moolenaarec65d772020-08-20 22:29:12 +02005281 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005282 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005283 li = list_find(l, 1L);
5284 if (li != NULL && li->li_tv.v_type == VAR_STRING
5285 && li->li_tv.vval.v_string != NULL
5286 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005287 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005288 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005289 }
5290 else
5291 {
5292 pos.col = list_find_nr(l, 1L, &error);
5293 if (error)
5294 return NULL;
5295 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005297 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005298 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005300 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005303 pos.coladd = list_find_nr(l, 2L, &error);
5304 if (error)
5305 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005306
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005307 return &pos;
5308 }
5309
Bram Moolenaarc5809432021-03-27 21:23:30 +01005310 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5311 return NULL;
5312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005313 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 if (name == NULL)
5315 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005317 {
5318 pos = curwin->w_cursor;
5319 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005320 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005321 return &pos;
5322 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005324 {
5325 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005326 pos = VIsual;
5327 else
5328 pos = curwin->w_cursor;
5329 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005330 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005331 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005332 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005335 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5337 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005338 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005339 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return pp;
5341 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005342
Bram Moolenaara5525202006-03-02 22:52:09 +00005343 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005344
Bram Moolenaar477933c2007-07-17 14:32:23 +00005345 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005346 {
5347 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005349 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005350 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // In silent Ex mode topline is zero, but that's not a valid line
5352 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005353 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005354 return &pos;
5355 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005357 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005358 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005360 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005361 return &pos;
5362 }
5363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005366 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 pos.lnum = curbuf->b_ml.ml_line_count;
5369 pos.col = 0;
5370 }
5371 else
5372 {
5373 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005374 if (charcol)
5375 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5376 else
5377 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 return &pos;
5380 }
5381 return NULL;
5382}
5383
5384/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005385 * Convert list in "arg" into a position and optional file number.
5386 * When "fnump" is NULL there is no file number, only 3 items.
5387 * Note that the column is passed on as-is, the caller may want to decrement
5388 * it to use 1 for the first column.
5389 * Return FAIL when conversion is not possible, doesn't check the position for
5390 * validity.
5391 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393list2fpos(
5394 typval_T *arg,
5395 pos_T *posp,
5396 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005397 colnr_T *curswantp,
5398 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005399{
5400 list_T *l = arg->vval.v_list;
5401 long i = 0;
5402 long n;
5403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5405 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005406 if (arg->v_type != VAR_LIST
5407 || l == NULL
5408 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005409 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005410 return FAIL;
5411
5412 if (fnump != NULL)
5413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005415 if (n < 0)
5416 return FAIL;
5417 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005419 *fnump = n;
5420 }
5421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005423 if (n < 0)
5424 return FAIL;
5425 posp->lnum = n;
5426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005428 if (n < 0)
5429 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005430 // If character position is specified, then convert to byte position
5431 if (charcol)
5432 {
5433 buf_T *buf;
5434
5435 // Get the text for the specified line in a loaded buffer
5436 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5437 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5438 return FAIL;
5439
Bram Moolenaar91458462021-01-13 20:08:38 +01005440 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005441 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005442 posp->col = n;
5443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005445 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005446 posp->coladd = 0;
5447 else
5448 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005449
Bram Moolenaar493c1782014-05-28 14:34:46 +02005450 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005452
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005453 return OK;
5454}
5455
5456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 * Get the length of an environment variable name.
5458 * Advance "arg" to the first character after the name.
5459 * Return 0 for error.
5460 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005462get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 char_u *p;
5465 int len;
5466
5467 for (p = *arg; vim_isIDc(*p); ++p)
5468 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 return 0;
5471
5472 len = (int)(p - *arg);
5473 *arg = p;
5474 return len;
5475}
5476
5477/*
5478 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005479 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 * Return 0 if something is wrong.
5481 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005483get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 char_u *p;
5486 int len;
5487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005490 {
5491 if (*p == ':')
5492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005493 // "s:" is start of "s:var", but "n:" is not and can be used in
5494 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005495 len = (int)(p - *arg);
5496 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5497 || len > 1)
5498 break;
5499 }
5500 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return 0;
5503
5504 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005505 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 return len;
5508}
5509
5510/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005511 * Get the length of the name of a variable or function.
5512 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 * Return -1 if curly braces expansion failed.
5515 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * If the name contains 'magic' {}'s, expand them and return the
5517 * expanded name in an allocated string via 'alias' - caller must free.
5518 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005520get_name_len(
5521 char_u **arg,
5522 char_u **alias,
5523 int evaluate,
5524 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u *p;
5528 char_u *expr_start;
5529 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5534 && (*arg)[2] == (int)KE_SNR)
5535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 *arg += 3;
5538 return get_id_len(arg) + 3;
5539 }
5540 len = eval_fname_script(*arg);
5541 if (len > 0)
5542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 *arg += len;
5545 }
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005550 p = find_name_end(*arg, &expr_start, &expr_end,
5551 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (expr_start != NULL)
5553 {
5554 char_u *temp_string;
5555
5556 if (!evaluate)
5557 {
5558 len += (int)(p - *arg);
5559 *arg = skipwhite(p);
5560 return len;
5561 }
5562
5563 /*
5564 * Include any <SID> etc in the expanded string:
5565 * Thus the -len here.
5566 */
5567 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5568 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005569 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 *alias = temp_string;
5571 *arg = skipwhite(p);
5572 return (int)STRLEN(temp_string);
5573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005576 // Only give an error when there is something, otherwise it will be
5577 // reported at a higher level.
5578 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005579 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581 return len;
5582}
5583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584/*
5585 * Find the end of a variable or function name, taking care of magic braces.
5586 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5587 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005588 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 * Return a pointer to just after the name. Equal to "arg" if there is no
5590 * valid name.
5591 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005592 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005593find_name_end(
5594 char_u *arg,
5595 char_u **expr_start,
5596 char_u **expr_end,
5597 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 int mb_nest = 0;
5600 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005602 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005603 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 *expr_start = NULL;
5608 *expr_end = NULL;
5609 }
5610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005612 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5613 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005614 return arg;
5615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 for (p = arg; *p != NUL
5617 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005618 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005619 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005620 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005622 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005624 if (*p == '\'')
5625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005627 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005628 ;
5629 if (*p == NUL)
5630 break;
5631 }
5632 else if (*p == '"')
5633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005634 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005635 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005636 if (*p == '\\' && p[1] != NUL)
5637 ++p;
5638 if (*p == NUL)
5639 break;
5640 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005641 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 // "s:" is start of "s:var", but "n:" is not and can be used in
5644 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005645 len = (int)(p - arg);
5646 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005647 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005648 break;
5649 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (*p == '[')
5654 ++br_nest;
5655 else if (*p == ']')
5656 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005658
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005659 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 if (*p == '{')
5662 {
5663 mb_nest++;
5664 if (expr_start != NULL && *expr_start == NULL)
5665 *expr_start = p;
5666 }
5667 else if (*p == '}')
5668 {
5669 mb_nest--;
5670 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5671 *expr_end = p;
5672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675
5676 return p;
5677}
5678
5679/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005680 * Expands out the 'magic' {}'s in a variable/function name.
5681 * Note that this can call itself recursively, to deal with
5682 * constructs like foo{bar}{baz}{bam}
5683 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5684 * "in_start" ^
5685 * "expr_start" ^
5686 * "expr_end" ^
5687 * "in_end" ^
5688 *
5689 * Returns a new allocated string, which the caller must free.
5690 * Returns NULL for failure.
5691 */
5692 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693make_expanded_name(
5694 char_u *in_start,
5695 char_u *expr_start,
5696 char_u *expr_end,
5697 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005698{
5699 char_u c1;
5700 char_u *retval = NULL;
5701 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005702
5703 if (expr_end == NULL || in_end == NULL)
5704 return NULL;
5705 *expr_start = NUL;
5706 *expr_end = NUL;
5707 c1 = *in_end;
5708 *in_end = NUL;
5709
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005710 temp_result = eval_to_string(expr_start + 1, FALSE);
5711 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005712 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005713 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5714 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005715 if (retval != NULL)
5716 {
5717 STRCPY(retval, in_start);
5718 STRCAT(retval, temp_result);
5719 STRCAT(retval, expr_end + 1);
5720 }
5721 }
5722 vim_free(temp_result);
5723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005724 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005725 *expr_start = '{';
5726 *expr_end = '}';
5727
5728 if (retval != NULL)
5729 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005730 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005731 if (expr_start != NULL)
5732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005733 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734 temp_result = make_expanded_name(retval, expr_start,
5735 expr_end, temp_result);
5736 vim_free(retval);
5737 retval = temp_result;
5738 }
5739 }
5740
5741 return retval;
5742}
5743
5744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005746 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005751 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005752}
5753
5754/*
5755 * Return TRUE if character "c" can be used as the first character in a
5756 * variable or function name (excluding '{' and '}').
5757 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005759eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005760{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005761 return ASCII_ISALPHA(c) || c == '_';
5762}
5763
5764/*
5765 * Return TRUE if character "c" can be used as the first character of a
5766 * dictionary key.
5767 */
5768 int
5769eval_isdictc(int c)
5770{
5771 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772}
5773
5774/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005775 * Handle:
5776 * - expr[expr], expr[expr:expr] subscript
5777 * - ".name" lookup
5778 * - function call with Funcref variable: func(expr)
5779 * - method call: var->method()
5780 *
5781 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005782 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005784handle_subscript(
5785 char_u **arg,
5786 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005787 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005788 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005789{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005790 int evaluate = evalarg != NULL
5791 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005792 int ret = OK;
5793 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005794 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005795 int getnext;
5796 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005797
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005798 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005799 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005800 // When at the end of the line and ".name" or "->{" or "->X" follows in
5801 // the next line then consume the line break.
5802 p = eval_next_non_blank(*arg, evalarg, &getnext);
5803 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005804 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005805 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005806 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005807 {
5808 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005809 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005810 check_white = FALSE;
5811 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005812
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005813 if (rettv->v_type == VAR_ANY)
5814 {
5815 char_u *exp_name;
5816 int cc;
5817 int idx;
5818 ufunc_T *ufunc;
5819 type_T *type;
5820
5821 // Found script from "import * as {name}", script item name must
5822 // follow.
5823 if (**arg != '.')
5824 {
5825 if (verbose)
5826 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5827 ret = FAIL;
5828 break;
5829 }
5830 ++*arg;
5831 if (IS_WHITE_OR_NUL(**arg))
5832 {
5833 if (verbose)
5834 emsg(_(e_no_white_space_allowed_after_dot));
5835 ret = FAIL;
5836 break;
5837 }
5838
5839 // isolate the name
5840 exp_name = *arg;
5841 while (eval_isnamec(**arg))
5842 ++*arg;
5843 cc = **arg;
5844 **arg = NUL;
5845
5846 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5847 evalarg->eval_cctx, verbose);
5848 **arg = cc;
5849 *arg = skipwhite(*arg);
5850
5851 if (idx < 0 && ufunc == NULL)
5852 {
5853 ret = FAIL;
5854 break;
5855 }
5856 if (idx >= 0)
5857 {
5858 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5859 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5860
5861 copy_tv(sv->sv_tv, rettv);
5862 }
5863 else
5864 {
5865 rettv->v_type = VAR_FUNC;
5866 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5867 }
5868 }
5869
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005870 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5871 || rettv->v_type == VAR_PARTIAL))
5872 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005873 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005874 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5875 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005876
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005877 // Stop the expression evaluation when immediately aborting on
5878 // error, or when an interrupt occurred or an exception was thrown
5879 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005880 if (aborting())
5881 {
5882 if (ret == OK)
5883 clear_tv(rettv);
5884 ret = FAIL;
5885 }
5886 dict_unref(selfdict);
5887 selfdict = NULL;
5888 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005889 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005890 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005891 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005892 if (ret == OK)
5893 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005894 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005895 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005896 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005897 else
5898 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005899 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005900 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005901 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005902 // "." is ".name" lookup when we found a dict or when evaluating and
5903 // scriptversion is at least 2, where string concatenation is "..".
5904 else if (**arg == '['
5905 || (**arg == '.' && (rettv->v_type == VAR_DICT
5906 || (!evaluate
5907 && (*arg)[1] != '.'
5908 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005909 {
5910 dict_unref(selfdict);
5911 if (rettv->v_type == VAR_DICT)
5912 {
5913 selfdict = rettv->vval.v_dict;
5914 if (selfdict != NULL)
5915 ++selfdict->dv_refcount;
5916 }
5917 else
5918 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005919 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005920 {
5921 clear_tv(rettv);
5922 ret = FAIL;
5923 }
5924 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005925 else
5926 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005927 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005929 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5930 // Don't do this when "Func" is already a partial that was bound
5931 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005932 if (selfdict != NULL
5933 && (rettv->v_type == VAR_FUNC
5934 || (rettv->v_type == VAR_PARTIAL
5935 && (rettv->vval.v_partial->pt_auto
5936 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005938
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005939 dict_unref(selfdict);
5940 return ret;
5941}
5942
5943/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005944 * Make a copy of an item.
5945 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005946 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5947 * reference to an already copied list/dict can be used.
5948 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005949 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951item_copy(
5952 typval_T *from,
5953 typval_T *to,
5954 int deep,
5955 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005956{
5957 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005959
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005962 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005963 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005964 }
5965 ++recurse;
5966
5967 switch (from->v_type)
5968 {
5969 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 case VAR_STRING:
5972 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005973 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005974 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005975 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005976 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005977 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005978 copy_tv(from, to);
5979 break;
5980 case VAR_LIST:
5981 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005982 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 if (from->vval.v_list == NULL)
5984 to->vval.v_list = NULL;
5985 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5986 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005987 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005988 to->vval.v_list = from->vval.v_list->lv_copylist;
5989 ++to->vval.v_list->lv_refcount;
5990 }
5991 else
5992 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5993 if (to->vval.v_list == NULL)
5994 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005996 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005998 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 case VAR_DICT:
6000 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006001 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006002 if (from->vval.v_dict == NULL)
6003 to->vval.v_dict = NULL;
6004 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6005 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006006 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006007 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6008 ++to->vval.v_dict->dv_refcount;
6009 }
6010 else
6011 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6012 if (to->vval.v_dict == NULL)
6013 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006015 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006016 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006018 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 }
6021 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023}
6024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 void
6026echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6027{
6028 char_u *tofree;
6029 char_u numbuf[NUMBUFLEN];
6030 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6031
6032 if (*atstart)
6033 {
6034 *atstart = FALSE;
6035 // Call msg_start() after eval1(), evaluating the expression
6036 // may cause a message to appear.
6037 if (with_space)
6038 {
6039 // Mark the saved text as finishing the line, so that what
6040 // follows is displayed on a new line when scrolling back
6041 // at the more prompt.
6042 msg_sb_eol();
6043 msg_start();
6044 }
6045 }
6046 else if (with_space)
6047 msg_puts_attr(" ", echo_attr);
6048
6049 if (p != NULL)
6050 for ( ; *p != NUL && !got_int; ++p)
6051 {
6052 if (*p == '\n' || *p == '\r' || *p == TAB)
6053 {
6054 if (*p != TAB && *needclr)
6055 {
6056 // remove any text still there from the command
6057 msg_clr_eos();
6058 *needclr = FALSE;
6059 }
6060 msg_putchar_attr(*p, echo_attr);
6061 }
6062 else
6063 {
6064 if (has_mbyte)
6065 {
6066 int i = (*mb_ptr2len)(p);
6067
6068 (void)msg_outtrans_len_attr(p, i, echo_attr);
6069 p += i - 1;
6070 }
6071 else
6072 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6073 }
6074 }
6075 vim_free(tofree);
6076}
6077
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 * ":echo expr1 ..." print each argument separated with a space, add a
6080 * newline at the end.
6081 * ":echon expr1 ..." print each argument plain.
6082 */
6083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 char_u *p;
6089 int needclr = TRUE;
6090 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006091 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006092 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006093 evalarg_T evalarg;
6094
Bram Moolenaare707c882020-07-01 13:07:37 +02006095 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097 if (eap->skip)
6098 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006099 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 // If eval1() causes an error message the text from the command may
6102 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006103 need_clr_eos = needclr;
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006106 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 /*
6109 * Report the invalid expression unless the expression evaluation
6110 * has been cancelled due to an aborting error, an interrupt, or an
6111 * exception.
6112 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006113 if (!aborting() && did_emsg == did_emsg_before
6114 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006115 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006116 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 break;
6118 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006119 need_clr_eos = FALSE;
6120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006123
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006124 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 arg = skipwhite(arg);
6126 }
6127 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006128 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 if (eap->skip)
6131 --emsg_skip;
6132 else
6133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (needclr)
6136 msg_clr_eos();
6137 if (eap->cmdidx == CMD_echo)
6138 msg_end();
6139 }
6140}
6141
6142/*
6143 * ":echohl {name}".
6144 */
6145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006148 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149}
6150
6151/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006152 * Returns the :echo attribute
6153 */
6154 int
6155get_echo_attr(void)
6156{
6157 return echo_attr;
6158}
6159
6160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 * ":execute expr1 ..." execute the result of an expression.
6162 * ":echomsg expr1 ..." Print a message
6163 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006164 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 * Each gets spaces around each argument and a newline at the end for
6166 * echo commands
6167 */
6168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 int ret = OK;
6174 char_u *p;
6175 garray_T ga;
6176 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 ga_init2(&ga, 1, 80);
6179
6180 if (eap->skip)
6181 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006182 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006184 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006185 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (!eap->skip)
6189 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006190 char_u buf[NUMBUFLEN];
6191
6192 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006193 {
6194 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6195 {
6196 emsg(_(e_inval_string));
6197 p = NULL;
6198 }
6199 else
6200 p = tv_get_string_buf(&rettv, buf);
6201 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006202 else
6203 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006204 if (p == NULL)
6205 {
6206 clear_tv(&rettv);
6207 ret = FAIL;
6208 break;
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 len = (int)STRLEN(p);
6211 if (ga_grow(&ga, len + 2) == FAIL)
6212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006213 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 ret = FAIL;
6215 break;
6216 }
6217 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 ga.ga_len += len;
6221 }
6222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 arg = skipwhite(arg);
6225 }
6226
6227 if (ret != FAIL && ga.ga_data != NULL)
6228 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006229 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6230 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006231 // Mark the already saved text as finishing the line, so that what
6232 // follows is displayed on a new line when scrolling back at the
6233 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006234 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006235 }
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006238 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006239 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006240 out_flush();
6241 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006242 else if (eap->cmdidx == CMD_echoconsole)
6243 {
6244 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6245 ui_write((char_u *)"\r\n", 2, TRUE);
6246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 else if (eap->cmdidx == CMD_echoerr)
6248 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006249 int save_did_emsg = did_emsg;
6250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006251 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006252 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (!force_abort)
6254 did_emsg = save_did_emsg;
6255 }
6256 else if (eap->cmdidx == CMD_execute)
6257 do_cmdline((char_u *)ga.ga_data,
6258 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6259 }
6260
6261 ga_clear(&ga);
6262
6263 if (eap->skip)
6264 --emsg_skip;
6265
6266 eap->nextcmd = check_nextcmd(arg);
6267}
6268
6269/*
6270 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6271 * "arg" points to the "&" or '+' when called, to "option" when returning.
6272 * Returns NULL when no option name found. Otherwise pointer to the char
6273 * after the option name.
6274 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006276find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 char_u *p = *arg;
6279
6280 ++p;
6281 if (*p == 'g' && p[1] == ':')
6282 {
6283 *opt_flags = OPT_GLOBAL;
6284 p += 2;
6285 }
6286 else if (*p == 'l' && p[1] == ':')
6287 {
6288 *opt_flags = OPT_LOCAL;
6289 p += 2;
6290 }
6291 else
6292 *opt_flags = 0;
6293
6294 if (!ASCII_ISALPHA(*p))
6295 return NULL;
6296 *arg = p;
6297
6298 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006299 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 else
6301 while (ASCII_ISALPHA(*p))
6302 ++p;
6303 return p;
6304}
6305
6306/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006307 * Display script name where an item was last set.
6308 * Should only be invoked when 'verbose' is non-zero.
6309 */
6310 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006311last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006312{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006313 char_u *p;
6314
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006315 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006316 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006317 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006318 if (p != NULL)
6319 {
6320 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006321 msg_puts(_("\n\tLast set from "));
6322 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006323 if (script_ctx.sc_lnum > 0)
6324 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006325 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006326 msg_outnum((long)script_ctx.sc_lnum);
6327 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006328 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006329 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006330 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006331 }
6332}
6333
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006334#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336/*
6337 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006338 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 * "flags" can be "g" to do a global substitute.
6340 * Returns an allocated string, NULL for error.
6341 */
6342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006343do_string_sub(
6344 char_u *str,
6345 char_u *pat,
6346 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006347 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006348 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 int sublen;
6351 regmatch_T regmatch;
6352 int i;
6353 int do_all;
6354 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006355 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 garray_T ga;
6357 char_u *ret;
6358 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006359 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006361 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006363 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 ga_init2(&ga, 1, 200);
6366
6367 do_all = (flags[0] == 'g');
6368
6369 regmatch.rm_ic = p_ic;
6370 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6371 if (regmatch.regprog != NULL)
6372 {
6373 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006374 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006377 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006378 if (regmatch.startp[0] == regmatch.endp[0])
6379 {
6380 if (zero_width == regmatch.startp[0])
6381 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006382 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006383 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006384 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6385 (size_t)i);
6386 ga.ga_len += i;
6387 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006388 continue;
6389 }
6390 zero_width = regmatch.startp[0];
6391 }
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 /*
6394 * Get some space for a temporary buffer to do the substitution
6395 * into. It will contain:
6396 * - The text up to where the match is.
6397 * - The substituted text.
6398 * - The text after the match.
6399 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006400 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006401 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6403 {
6404 ga_clear(&ga);
6405 break;
6406 }
6407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 i = (int)(regmatch.startp[0] - tail);
6410 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006411 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006412 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 + ga.ga_len + i, TRUE, TRUE, FALSE);
6414 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006415 tail = regmatch.endp[0];
6416 if (*tail == NUL)
6417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (!do_all)
6419 break;
6420 }
6421
6422 if (ga.ga_data != NULL)
6423 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6424
Bram Moolenaar473de612013-06-08 18:19:48 +02006425 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427
6428 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6429 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006430 if (p_cpo == empty_option)
6431 p_cpo = save_cpo;
6432 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006434 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006435 // If it's still empty it was changed and restored, need to restore in
6436 // the complicated way.
6437 if (*p_cpo == NUL)
6438 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006439 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
6442 return ret;
6443}