blob: 97c5f78dffaf50b2f72c2d49f8423f27a79d0b68 [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);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020054static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020055static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020056static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static 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 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107#if defined(EBCDIC) || defined(PROTO)
108/*
109 * Compare struct fst by function name.
110 */
111 static int
112compare_func_name(const void *s1, const void *s2)
113{
114 struct fst *p1 = (struct fst *)s1;
115 struct fst *p2 = (struct fst *)s2;
116
117 return STRCMP(p1->f_name, p2->f_name);
118}
119
120/*
121 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100122 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100123 * On machines using EBCDIC we have to sort it.
124 */
125 static void
126sortFunctions(void)
127{
128 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
129
130 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
131}
132#endif
133
Bram Moolenaar33570922005-01-25 22:26:29 +0000134/*
135 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000136 */
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200141 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000142
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143#ifdef EBCDIC
144 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100145 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200146 */
147 sortFunctions();
148#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000149}
150
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151#if defined(EXITFREE) || defined(PROTO)
152 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100153eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000154{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200155 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100156 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200157 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000158
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200159 // autoloaded script names
160 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000161
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200162 // unreferenced lists and dicts
163 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200164
165 // functions not garbage collected
166 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000167}
168#endif
169
Bram Moolenaare6b53242020-07-01 17:28:33 +0200170 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200171fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
172{
173 CLEAR_FIELD(*evalarg);
174 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
175 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
176 {
177 evalarg->eval_getline = eap->getline;
178 evalarg->eval_cookie = eap->cookie;
179 }
180}
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Top level evaluation function, returning a boolean.
184 * Sets "error" to TRUE if there was an error.
185 * Return TRUE or FALSE.
186 */
187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100188eval_to_bool(
189 char_u *arg,
190 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200191 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100192 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
Bram Moolenaar33570922005-01-25 22:26:29 +0000194 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200195 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200196 evalarg_T evalarg;
197
Bram Moolenaar37c83712020-06-30 21:18:36 +0200198 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200 if (skip)
201 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 else
205 {
206 *error = FALSE;
207 if (!skip)
208 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200209 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200210 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200211 else
212 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000213 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
215 }
216 if (skip)
217 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200218 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200220 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221}
222
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223/*
224 * Call eval1() and give an error message if not done at a lower level.
225 */
226 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200227eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100228{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 int ret;
231 int did_emsg_before = did_emsg;
232 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200233 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100234
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200235 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
236
237 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100238 if (ret == FAIL)
239 {
240 // Report the invalid expression unless the expression evaluation has
241 // been cancelled due to an aborting error, an interrupt, or an
242 // exception, or we already gave a more specific error.
243 // Also check called_emsg for when using assert_fails().
244 if (!aborting() && did_emsg == did_emsg_before
245 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100246 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100247 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200248 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100249 return ret;
250}
251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200253 * Return whether a typval is a valid expression to pass to eval_expr_typval()
254 * or eval_expr_to_bool(). An empty string returns FALSE;
255 */
256 int
257eval_expr_valid_arg(typval_T *tv)
258{
259 return tv->v_type != VAR_UNKNOWN
260 && (tv->v_type != VAR_STRING
261 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
262}
263
264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 * Evaluate an expression, which can be a function, partial or string.
266 * Pass arguments "argv[argc]".
267 * Return the result in "rettv" and OK or FAIL.
268 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200269 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100270eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
271{
272 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200274 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100275
276 if (expr->v_type == VAR_FUNC)
277 {
278 s = expr->vval.v_string;
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200282 funcexe.evaluate = TRUE;
283 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 return FAIL;
285 }
286 else if (expr->v_type == VAR_PARTIAL)
287 {
288 partial_T *partial = expr->vval.v_partial;
289
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200290 if (partial == NULL)
291 return FAIL;
292
Bram Moolenaar822ba242020-05-24 23:00:18 +0200293 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200294 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200296 if (call_def_function(partial->pt_func, argc, argv,
297 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 return FAIL;
299 }
300 else
301 {
302 s = partial_name(partial);
303 if (s == NULL || *s == NUL)
304 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200305 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 funcexe.evaluate = TRUE;
307 funcexe.partial = partial;
308 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
309 return FAIL;
310 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100311 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200312 else if (expr->v_type == VAR_INSTR)
313 {
314 return exe_typval_instr(expr, rettv);
315 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 else
317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100318 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100319 if (s == NULL)
320 return FAIL;
321 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200322 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200324 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200326 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100327 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 return FAIL;
329 }
330 }
331 return OK;
332}
333
334/*
335 * Like eval_to_bool() but using a typval_T instead of a string.
336 * Works for string, funcref and partial.
337 */
338 int
339eval_expr_to_bool(typval_T *expr, int *error)
340{
341 typval_T rettv;
342 int res;
343
344 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
345 {
346 *error = TRUE;
347 return FALSE;
348 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200349 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100350 clear_tv(&rettv);
351 return res;
352}
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Top level evaluation function, returning a string. If "skip" is TRUE,
356 * only parsing to "nextcmd" is done, without reporting errors. Return
357 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
358 */
359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100360eval_to_string_skip(
361 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200362 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100363 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364{
Bram Moolenaar33570922005-01-25 22:26:29 +0000365 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200367 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar37c83712020-06-30 21:18:36 +0200369 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 if (skip)
371 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200372 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 retval = NULL;
374 else
375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100376 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
379 if (skip)
380 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200381 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
383 return retval;
384}
385
386/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387 * Skip over an expression at "*pp".
388 * Return FAIL for an error, OK otherwise.
389 */
390 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200391skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000392{
Bram Moolenaar33570922005-01-25 22:26:29 +0000393 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000394
395 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200396 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000397}
398
399/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200400 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 * If in Vim9 script and line breaks are encountered, the lines are
402 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200403 * "arg" is advanced to just after the expression.
404 * "start" is set to the start of the expression, "end" to just after the end.
405 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 * Return FAIL for an error, OK otherwise.
407 */
408 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409skip_expr_concatenate(
410 char_u **arg,
411 char_u **start,
412 char_u **end,
413 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414{
415 typval_T rettv;
416 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200417 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200418 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 int evaluate = evalarg == NULL
421 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200423 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425 {
426 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200427 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200428 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200429 ++gap->ga_len;
430 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432
433 // Don't evaluate the expression.
434 if (evalarg != NULL)
435 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200436 *arg = skipwhite(*arg);
437 res = eval1(arg, &rettv, evalarg);
438 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200439 if (evalarg != NULL)
440 evalarg->eval_flags = save_flags;
441
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200442 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200443 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200444 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200445 if (evalarg->eval_ga.ga_len == 1)
446 {
447 // just one line, no need to concatenate
448 ga_clear(gap);
449 gap->ga_itemsize = 0;
450 }
451 else
452 {
453 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200454 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200455
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200456 // Line breaks encountered, concatenate all the lines.
457 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200458 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459
460 // free the lines only when using getsourceline()
461 if (evalarg->eval_cookie != NULL)
462 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 // Do not free the last line, "arg" points into it, free it
466 // later.
467 vim_free(evalarg->eval_tofree);
468 evalarg->eval_tofree =
469 ((char_u **)gap->ga_data)[gap->ga_len - 1];
470 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200471 ga_clear_strings(gap);
472 }
473 else
474 ga_clear(gap);
475 gap->ga_itemsize = 0;
476 if (p == NULL)
477 return FAIL;
478 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200479 vim_free(evalarg->eval_tofree_lambda);
480 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200481 // Compute "end" relative to the end.
482 *end = *start + STRLEN(*start) - endoff;
483 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484 }
485
486 return res;
487}
488
489/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100490 * Convert "tv" to a string.
491 * When "convert" is TRUE convert a List into a sequence of lines and convert
492 * a Float to a String.
493 * Returns an allocated string (NULL when out of memory).
494 */
495 char_u *
496typval2string(typval_T *tv, int convert)
497{
498 garray_T ga;
499 char_u *retval;
500#ifdef FEAT_FLOAT
501 char_u numbuf[NUMBUFLEN];
502#endif
503
504 if (convert && tv->v_type == VAR_LIST)
505 {
506 ga_init2(&ga, (int)sizeof(char), 80);
507 if (tv->vval.v_list != NULL)
508 {
509 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
510 if (tv->vval.v_list->lv_len > 0)
511 ga_append(&ga, NL);
512 }
513 ga_append(&ga, NUL);
514 retval = (char_u *)ga.ga_data;
515 }
516#ifdef FEAT_FLOAT
517 else if (convert && tv->v_type == VAR_FLOAT)
518 {
519 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
520 retval = vim_strsave(numbuf);
521 }
522#endif
523 else
524 retval = vim_strsave(tv_get_string(tv));
525 return retval;
526}
527
528/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200529 * Top level evaluation function, returning a string. Does not handle line
530 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000531 * When "convert" is TRUE convert a List into a sequence of lines and convert
532 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 * Return pointer to allocated memory, or NULL for failure.
534 */
535 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 int convert,
539 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaar33570922005-01-25 22:26:29 +0000541 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100543 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100545 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
546 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 retval = NULL;
548 else
549 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100550 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000551 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100553 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 return retval;
556}
557
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100558 char_u *
559eval_to_string(
560 char_u *arg,
561 int convert)
562{
563 return eval_to_string_eap(arg, convert, NULL);
564}
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200568 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200569 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 */
571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572eval_to_string_safe(
573 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100574 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200577 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200578 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200580 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200581 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000582 if (use_sandbox)
583 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200584 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200585 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000586 if (use_sandbox)
587 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200588 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200589 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200590 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 return retval;
592}
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594/*
595 * Top level evaluation function, returning a number.
596 * Evaluates "expr" silently.
597 * Returns -1 for an error.
598 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200599 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100600eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
Bram Moolenaar33570922005-01-25 22:26:29 +0000602 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200603 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000604 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
606 ++emsg_off;
607
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200608 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 retval = -1;
610 else
611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100612 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000613 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 }
615 --emsg_off;
616
617 return retval;
618}
619
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000621 * Top level evaluation function.
622 * Returns an allocated typval_T with the result.
623 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000624 */
625 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200626eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000627{
628 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200629 evalarg_T evalarg;
630
631 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000632
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200633 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200634 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100635 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000636
Bram Moolenaar37c83712020-06-30 21:18:36 +0200637 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000638 return tv;
639}
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100642 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200643 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
644 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000645 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100648call_vim_function(
649 char_u *func,
650 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200651 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200652 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000654 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200655 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100657 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200658 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200659 funcexe.firstline = curwin->w_cursor.lnum;
660 funcexe.lastline = curwin->w_cursor.lnum;
661 funcexe.evaluate = TRUE;
662 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000663 if (ret == FAIL)
664 clear_tv(rettv);
665
666 return ret;
667}
668
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100670 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100671 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200672 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
673 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100674 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676call_func_retnr(
677 char_u *func,
678 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100680{
681 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200682 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100683
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100685 return -1;
686
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100687 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100688 clear_tv(&rettv);
689 return retval;
690}
691
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000692/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100693 * Call Vim script function like call_func_retnr() and drop the result.
694 * Returns FAIL when calling the function fails.
695 */
696 int
697call_func_noret(
698 char_u *func,
699 int argc,
700 typval_T *argv)
701{
702 typval_T rettv;
703
704 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
705 return FAIL;
706 clear_tv(&rettv);
707 return OK;
708}
709
710/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100711 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100712 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000713 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000714 */
715 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100716call_func_retstr(
717 char_u *func,
718 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200719 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720{
721 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000722 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723
Bram Moolenaarded27a12018-08-01 19:06:03 +0200724 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000725 return NULL;
726
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100727 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 return retval;
730}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000732/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100733 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100734 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000735 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000736 */
737 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100738call_func_retlist(
739 char_u *func,
740 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200741 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000742{
743 typval_T rettv;
744
Bram Moolenaarded27a12018-08-01 19:06:03 +0200745 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000746 return NULL;
747
748 if (rettv.v_type != VAR_LIST)
749 {
750 clear_tv(&rettv);
751 return NULL;
752 }
753
754 return rettv.vval.v_list;
755}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#ifdef FEAT_FOLDING
758/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100759 * Evaluate "arg", which is 'foldexpr'.
760 * Note: caller must set "curwin" to match "arg".
761 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
762 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 */
764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100765eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
Bram Moolenaar33570922005-01-25 22:26:29 +0000767 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200768 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000770 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
771 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000774 if (use_sandbox)
775 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200776 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200778 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = 0;
780 else
781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 if (tv.v_type == VAR_NUMBER)
784 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000785 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 retval = 0;
787 else
788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 // If the result is a string, check if there is a non-digit before
790 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (!VIM_ISDIGIT(*s) && *s != '-')
793 *cp = *s++;
794 retval = atol((char *)s);
795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
798 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000799 if (use_sandbox)
800 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200801 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200802 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200804 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 * Get an lval: variable, Dict item or List item that can be assigned a value
810 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
811 * "name.key", "name.key[expr]" etc.
812 * Indexing only works if "name" is an existing List or Dictionary.
813 * "name" points to the start of the name.
814 * If "rettv" is not NULL it points to the value to be assigned.
815 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
816 * wrong; must end in space or cmd separator.
817 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100818 * flags:
819 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100820 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100821 * GLV_NO_AUTOLOAD: do not use script autoloading
822 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000824 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000826 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200827 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100828get_lval(
829 char_u *name,
830 typval_T *rettv,
831 lval_T *lp,
832 int unlet,
833 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 int flags, // GLV_ values
835 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 char_u *expr_start, *expr_end;
839 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 dictitem_T *v;
841 typval_T var1;
842 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100847 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100848 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100849 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200852 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100854 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100856 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200858 lp->ll_name_end = find_name_end(name, NULL, NULL,
859 FNE_INCL_BR | fne_flags);
860 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 }
862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000864 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 if (expr_start != NULL)
867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100869 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 && *p != '[' && *p != '.')
871 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200872 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 return NULL;
874 }
875
876 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
877 if (lp->ll_exp_name == NULL)
878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100879 // Report an invalid expression in braces, unless the
880 // expression evaluation has been cancelled due to an
881 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 if (!aborting() && !quiet)
883 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000884 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100885 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 return NULL;
887 }
888 }
889 lp->ll_name = lp->ll_exp_name;
890 }
891 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_name = name;
894
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200895 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200897 // "a: type" is declaring variable "a" with a type, not "a:".
898 if (p == name + 2 && p[-1] == ':')
899 {
900 --p;
901 lp->ll_name_end = p;
902 }
903 if (*p == ':')
904 {
905 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
906 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200908 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100909 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
910 if (lp->ll_type == NULL && !quiet)
911 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200912 lp->ll_name_end = tp;
913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 }
915 }
916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
919 return p;
920
921 cc = *p;
922 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100923 // When we would write to the variable pass &ht and prevent autoload.
924 writing = !(flags & GLV_READ_ONLY);
925 v = find_var(lp->ll_name, writing ? &ht : NULL,
926 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200928 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000930 if (v == NULL)
931 return NULL;
932
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100933 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
934 {
935 if (!quiet)
936 semsg(_(e_variable_already_declared), lp->ll_name);
937 return NULL;
938 }
939
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 /*
941 * Loop until no more [idx] or .key is following.
942 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100944 var1.v_type = VAR_UNKNOWN;
945 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200949 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100950 && !(lp->ll_tv->v_type == VAR_BLOB
951 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100954 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000963
Bram Moolenaar10c65862020-10-08 21:16:42 +0200964 if (in_vim9script() && lp->ll_valtype == NULL
965 && lp->ll_tv == &v->di_tv
966 && ht != NULL && ht == get_script_local_ht())
967 {
968 svar_T *sv = find_typval_in_script(lp->ll_tv);
969
970 // Vim9 script local variable: get the type
971 if (sv != NULL)
972 lp->ll_valtype = sv->sv_type;
973 }
974
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 len = -1;
976 if (*p == '.')
977 {
978 key = p + 1;
979 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
980 ;
981 if (len == 0)
982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 }
987 p = key + len;
988 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000989 else
990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100991 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000992 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 if (*p == ':')
994 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000995 else
996 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200998 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001000 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001003 clear_tv(&var1);
1004 return NULL;
1005 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001006 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 }
1008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001009 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001010 if (*p == ':')
1011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001015 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001016 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001018 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001019 if (rettv != NULL
1020 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001021 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001022 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001023 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001027 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 }
1030 p = skipwhite(p + 1);
1031 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 else
1034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001036 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001037 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001042 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001044 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001045 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001046 clear_tv(&var2);
1047 return NULL;
1048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001054
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001058 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001059 clear_tv(&var1);
1060 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 }
1063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 ++p;
1066 }
1067
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 {
1070 if (len == -1)
1071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001072 // "[key]": get key from "var1"
1073 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001074 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 }
1079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001080 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001081
1082 // a NULL dict is equivalent with an empty dict
1083 if (lp->ll_tv->vval.v_dict == NULL)
1084 {
1085 lp->ll_tv->vval.v_dict = dict_alloc();
1086 if (lp->ll_tv->vval.v_dict == NULL)
1087 {
1088 clear_tv(&var1);
1089 return NULL;
1090 }
1091 ++lp->ll_tv->vval.v_dict->dv_refcount;
1092 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001093 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001095 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 // When assigning to a scope dictionary check that a function and
1098 // variable name is valid (only variable name unless it is l: or
1099 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001100 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001101 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001102 int prevval;
1103 int wrong;
1104
1105 if (len != -1)
1106 {
1107 prevval = key[len];
1108 key[len] = NUL;
1109 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001110 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001112 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1113 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001114 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001115 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001116 if (len != -1)
1117 key[len] = prevval;
1118 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001119 {
1120 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001121 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001122 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001123 }
1124
Bram Moolenaar10c65862020-10-08 21:16:42 +02001125 if (lp->ll_valtype != NULL)
1126 // use the type of the member
1127 lp->ll_valtype = lp->ll_valtype->tt_member;
1128
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001131 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001132 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001135 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001136 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001137 return NULL;
1138 }
1139
Bram Moolenaar31b81602019-02-10 22:14:27 +01001140 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001144 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 }
1148 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001152 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 p = NULL;
1155 break;
1156 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001158 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001159 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1160 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001161 {
1162 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001163 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001164 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001165
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001166 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169 else if (lp->ll_tv->v_type == VAR_BLOB)
1170 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001171 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1172
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001173 /*
1174 * Get the number and item for the only or first index of the List.
1175 */
1176 if (empty1)
1177 lp->ll_n1 = 0;
1178 else
1179 // is number or string
1180 lp->ll_n1 = (long)tv_get_number(&var1);
1181 clear_tv(&var1);
1182
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001183 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001184 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001185 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 return NULL;
1187 }
1188 if (lp->ll_range && !lp->ll_empty2)
1189 {
1190 lp->ll_n2 = (long)tv_get_number(&var2);
1191 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001192 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1193 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001194 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001195 }
1196 lp->ll_blob = lp->ll_tv->vval.v_blob;
1197 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001198 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 else
1201 {
1202 /*
1203 * Get the number and item for the only or first index of the List.
1204 */
1205 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001207 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001208 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001209 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001210 clear_tv(&var1);
1211
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001212 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001214 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001215 if (lp->ll_li == NULL)
1216 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001217 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001218 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 }
1222
Bram Moolenaar10c65862020-10-08 21:16:42 +02001223 if (lp->ll_valtype != NULL)
1224 // use the type of the member
1225 lp->ll_valtype = lp->ll_valtype->tt_member;
1226
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 /*
1228 * May need to find the item or absolute index for the second
1229 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 * When no index given: "lp->ll_empty2" is TRUE.
1231 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001235 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001236 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001242 {
1243 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 }
1249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001250 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (lp->ll_n1 < 0)
1252 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1253 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001254 {
1255 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001256 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 }
1260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 }
1264
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001265 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return p;
1268}
1269
1270/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275{
1276 vim_free(lp->ll_exp_name);
1277 vim_free(lp->ll_newkey);
1278}
1279
1280/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001281 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001283 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1284 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287set_var_lval(
1288 lval_T *lp,
1289 char_u *endp,
1290 typval_T *rettv,
1291 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001292 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1293 char_u *op,
1294 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295{
1296 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 listitem_T *ri;
1298 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299
1300 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001302 cc = *endp;
1303 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 if (lp->ll_blob != NULL)
1305 {
1306 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001307
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 if (op != NULL && *op != '=')
1309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 return;
1312 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001313 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001314 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315
1316 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1317 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001318 if (lp->ll_empty2)
1319 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1320
Bram Moolenaar68452172021-04-12 21:21:02 +02001321 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1322 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001324 }
1325 else
1326 {
1327 val = (int)tv_get_number_chk(rettv, &error);
1328 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001329 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 }
1331 }
1332 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001334 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001336 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1337 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001338 {
1339 emsg(_(e_cannot_mod));
1340 *endp = cc;
1341 return;
1342 }
1343
Bram Moolenaarff697e62019-02-12 22:28:33 +01001344 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001345 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001346 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001347 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001348 {
1349 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001350 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1351 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 && tv_op(&tv, rettv, op) == OK)
1353 set_var(lp->ll_name, &tv, FALSE);
1354 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001358 {
1359 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001360 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001362 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1363 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001364 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001367 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001368 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001369 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001370 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 else if (lp->ll_range)
1372 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001373 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001374 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001375
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001376 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1377 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001378 {
1379 emsg(_("E996: Cannot lock a range"));
1380 return;
1381 }
1382
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383 /*
1384 * Check whether any of the list items is locked
1385 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001386 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001387 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001388 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001389 return;
1390 ri = ri->li_next;
1391 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1392 break;
1393 ll_li = ll_li->li_next;
1394 ++ll_n1;
1395 }
1396
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 /*
1398 * Assign the List values to the list items.
1399 */
1400 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 if (op != NULL && *op != '=')
1403 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1404 else
1405 {
1406 clear_tv(&lp->ll_li->li_tv);
1407 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1408 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 ri = ri->li_next;
1410 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1411 break;
1412 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001414 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001415 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = NULL;
1418 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001420 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 lp->ll_li = lp->ll_li->li_next;
1422 ++lp->ll_n1;
1423 }
1424 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001425 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 else if (lp->ll_empty2
1427 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001429 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 }
1431 else
1432 {
1433 /*
1434 * Assign to a List or Dictionary item.
1435 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001436 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1437 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001438 {
1439 emsg(_("E996: Cannot lock a list or dict"));
1440 return;
1441 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001442
1443 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001444 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001445 return;
1446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 if (lp->ll_newkey != NULL)
1448 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 if (op != NULL && *op != '=')
1450 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001451 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 return;
1453 }
1454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001455 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001456 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 if (di == NULL)
1458 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001459 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1460 {
1461 vim_free(di);
1462 return;
1463 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001465 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 else if (op != NULL && *op != '=')
1467 {
1468 tv_op(lp->ll_tv, rettv, op);
1469 return;
1470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 /*
1475 * Assign the value to the variable or list item.
1476 */
1477 if (copy)
1478 copy_tv(rettv, lp->ll_tv);
1479 else
1480 {
1481 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001482 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 }
1485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486}
1487
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001488/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001489 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1490 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001491 * Returns OK or FAIL.
1492 */
1493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001496 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 char_u numbuf[NUMBUFLEN];
1498 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001499 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001501 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001502 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001503 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504 {
1505 switch (tv1->v_type)
1506 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001507 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001508 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 case VAR_DICT:
1511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001512 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001513 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001514 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001516 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001517 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 break;
1519
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001520 case VAR_BLOB:
1521 if (*op != '+' || tv2->v_type != VAR_BLOB)
1522 break;
1523 // BLOB += BLOB
1524 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1525 {
1526 blob_T *b1 = tv1->vval.v_blob;
1527 blob_T *b2 = tv2->vval.v_blob;
1528 int i, len = blob_len(b2);
1529 for (i = 0; i < len; i++)
1530 ga_append(&b1->bv_ga, blob_get(b2, i));
1531 }
1532 return OK;
1533
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 case VAR_LIST:
1535 if (*op != '+' || tv2->v_type != VAR_LIST)
1536 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001538 if (tv2->vval.v_list != NULL)
1539 {
1540 if (tv1->vval.v_list == NULL)
1541 {
1542 tv1->vval.v_list = tv2->vval.v_list;
1543 ++tv1->vval.v_list->lv_refcount;
1544 }
1545 else
1546 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 return OK;
1549
1550 case VAR_NUMBER:
1551 case VAR_STRING:
1552 if (tv2->v_type == VAR_LIST)
1553 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001554 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001556 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001557 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558#ifdef FEAT_FLOAT
1559 if (tv2->v_type == VAR_FLOAT)
1560 {
1561 float_T f = n;
1562
Bram Moolenaarff697e62019-02-12 22:28:33 +01001563 if (*op == '%')
1564 break;
1565 switch (*op)
1566 {
1567 case '+': f += tv2->vval.v_float; break;
1568 case '-': f -= tv2->vval.v_float; break;
1569 case '*': f *= tv2->vval.v_float; break;
1570 case '/': f /= tv2->vval.v_float; break;
1571 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572 clear_tv(tv1);
1573 tv1->v_type = VAR_FLOAT;
1574 tv1->vval.v_float = f;
1575 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577#endif
1578 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 switch (*op)
1580 {
1581 case '+': n += tv_get_number(tv2); break;
1582 case '-': n -= tv_get_number(tv2); break;
1583 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001584 case '/': n = num_divide(n, tv_get_number(tv2),
1585 &failed); break;
1586 case '%': n = num_modulus(n, tv_get_number(tv2),
1587 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 clear_tv(tv1);
1590 tv1->v_type = VAR_NUMBER;
1591 tv1->vval.v_number = n;
1592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001593 }
1594 else
1595 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001596 if (tv2->v_type == VAR_FLOAT)
1597 break;
1598
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001600 s = tv_get_string(tv1);
1601 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001602 clear_tv(tv1);
1603 tv1->v_type = VAR_STRING;
1604 tv1->vval.v_string = s;
1605 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001606 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001609#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 {
1611 float_T f;
1612
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 if (*op == '%' || *op == '.'
1614 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 && tv2->v_type != VAR_NUMBER
1616 && tv2->v_type != VAR_STRING))
1617 break;
1618 if (tv2->v_type == VAR_FLOAT)
1619 f = tv2->vval.v_float;
1620 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001621 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 switch (*op)
1623 {
1624 case '+': tv1->vval.v_float += f; break;
1625 case '-': tv1->vval.v_float -= f; break;
1626 case '*': tv1->vval.v_float *= f; break;
1627 case '/': tv1->vval.v_float /= f; break;
1628 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001631 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 }
1633 }
1634
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 return FAIL;
1637}
1638
1639/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 * Evaluate the expression used in a ":for var in expr" command.
1641 * "arg" points to "var".
1642 * Set "*errp" to TRUE for an error, FALSE otherwise;
1643 * Return a pointer that holds the info. Null when there is an error.
1644 */
1645 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646eval_for_line(
1647 char_u *arg,
1648 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001649 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001650 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651{
Bram Moolenaar33570922005-01-25 22:26:29 +00001652 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001654 typval_T tv;
1655 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001656 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001658 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001660 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 if (fi == NULL)
1662 return NULL;
1663
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001664 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 if (expr == NULL)
1666 return fi;
1667
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001668 expr = skipwhite_and_linebreak(expr, evalarg);
1669 if (expr[0] != 'i' || expr[1] != 'n'
1670 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 return fi;
1674 }
1675
1676 if (skip)
1677 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001678 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1679 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 {
1681 *errp = FALSE;
1682 if (!skip)
1683 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001684 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001685 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001686 l = tv.vval.v_list;
1687 if (l == NULL)
1688 {
1689 // a null list is like an empty list: do nothing
1690 clear_tv(&tv);
1691 }
1692 else
1693 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001695 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 // No need to increment the refcount, it's already set for
1698 // the list being used in "tv".
1699 fi->fi_list = l;
1700 list_add_watch(l, &fi->fi_lw);
1701 fi->fi_lw.lw_item = l->lv_first;
1702 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001703 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001704 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001705 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001706 fi->fi_bi = 0;
1707 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 typval_T btv;
1710
1711 // Make a copy, so that the iteration still works when the
1712 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001714 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001716 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001717 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001718 else if (tv.v_type == VAR_STRING)
1719 {
1720 fi->fi_byte_idx = 0;
1721 fi->fi_string = tv.vval.v_string;
1722 tv.vval.v_string = NULL;
1723 if (fi->fi_string == NULL)
1724 fi->fi_string = vim_strsave((char_u *)"");
1725 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 else
1727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001728 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 }
1731 }
1732 }
1733 if (skip)
1734 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001735 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736
1737 return fi;
1738}
1739
1740/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001741 * Used when looping over a :for line, skip the "in expr" part.
1742 */
1743 void
1744skip_for_lines(void *fi_void, evalarg_T *evalarg)
1745{
1746 forinfo_T *fi = (forinfo_T *)fi_void;
1747 int i;
1748
1749 for (i = 0; i < fi->fi_break_count; ++i)
1750 eval_next_line(evalarg);
1751}
1752
1753/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 * Use the first item in a ":for" list. Advance to the next.
1755 * Assign the values to the variable (list). "arg" points to the first one.
1756 * Return TRUE when a valid item was found, FALSE when at end of list or
1757 * something wrong.
1758 */
1759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001762 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001764 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1765 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1766 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001767 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001769 if (fi->fi_blob != NULL)
1770 {
1771 typval_T tv;
1772
1773 if (fi->fi_bi >= blob_len(fi->fi_blob))
1774 return FALSE;
1775 tv.v_type = VAR_NUMBER;
1776 tv.v_lock = VAR_FIXED;
1777 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1778 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001779 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001780 fi->fi_varcount, flag, NULL) == OK;
1781 }
1782
1783 if (fi->fi_string != NULL)
1784 {
1785 typval_T tv;
1786 int len;
1787
1788 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1789 if (len == 0)
1790 return FALSE;
1791 tv.v_type = VAR_STRING;
1792 tv.v_lock = VAR_FIXED;
1793 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1794 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001795 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001796 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001797 vim_free(tv.vval.v_string);
1798 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001799 }
1800
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 item = fi->fi_lw.lw_item;
1802 if (item == NULL)
1803 result = FALSE;
1804 else
1805 {
1806 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001807 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001808 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 }
1810 return result;
1811}
1812
1813/*
1814 * Free the structure used to store info used by ":for".
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001821 if (fi == NULL)
1822 return;
1823 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001824 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001826 list_unref(fi->fi_list);
1827 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001828 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001829 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001830 else
1831 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 vim_free(fi);
1833}
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836set_context_for_expression(
1837 expand_T *xp,
1838 char_u *arg,
1839 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001841 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001845 if (cmdidx == CMD_let || cmdidx == CMD_var
1846 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
1848 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001851 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001852 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 {
1854 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001855 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001856 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 break;
1858 }
1859 return;
1860 }
1861 }
1862 else
1863 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1864 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 while ((xp->xp_pattern = vim_strpbrk(arg,
1866 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1867 {
1868 c = *xp->xp_pattern;
1869 if (c == '&')
1870 {
1871 c = xp->xp_pattern[1];
1872 if (c == '&')
1873 {
1874 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001875 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001880 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1881 xp->xp_pattern += 2;
1882
1883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 else if (c == '$')
1886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001887 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 xp->xp_context = EXPAND_ENV_VARS;
1889 }
1890 else if (c == '=')
1891 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001892 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 xp->xp_context = EXPAND_EXPRESSION;
1894 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001895 else if (c == '#'
1896 && xp->xp_context == EXPAND_EXPRESSION)
1897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001899 break;
1900 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001901 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 && xp->xp_context == EXPAND_FUNCTIONS
1903 && vim_strchr(xp->xp_pattern, '(') == NULL)
1904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 break;
1907 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001908 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1913 if (c == '\\' && xp->xp_pattern[1] != NUL)
1914 ++xp->xp_pattern;
1915 xp->xp_context = EXPAND_NOTHING;
1916 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001917 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1921 /* skip */ ;
1922 xp->xp_context = EXPAND_NOTHING;
1923 }
1924 else if (c == '|')
1925 {
1926 if (xp->xp_pattern[1] == '|')
1927 {
1928 ++xp->xp_pattern;
1929 xp->xp_context = EXPAND_EXPRESSION;
1930 }
1931 else
1932 xp->xp_context = EXPAND_COMMANDS;
1933 }
1934 else
1935 xp->xp_context = EXPAND_EXPRESSION;
1936 }
1937 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 // Doesn't look like something valid, expand as an expression
1939 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 arg = xp->xp_pattern;
1942 if (*arg != NUL)
1943 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1944 /* skip */ ;
1945 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001946
1947 // ":exe one two" completes "two"
1948 if ((cmdidx == CMD_execute
1949 || cmdidx == CMD_echo
1950 || cmdidx == CMD_echon
1951 || cmdidx == CMD_echomsg)
1952 && xp->xp_context == EXPAND_EXPRESSION)
1953 {
1954 for (;;)
1955 {
1956 char_u *n = skiptowhite(arg);
1957
1958 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1959 break;
1960 arg = skipwhite(n);
1961 }
1962 }
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 xp->xp_pattern = arg;
1965}
1966
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001968 * Return TRUE if "pat" matches "text".
1969 * Does not use 'cpo' and always uses 'magic'.
1970 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001971 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001972pattern_match(char_u *pat, char_u *text, int ic)
1973{
1974 int matches = FALSE;
1975 char_u *save_cpo;
1976 regmatch_T regmatch;
1977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001978 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001979 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001980 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001981 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1982 if (regmatch.regprog != NULL)
1983 {
1984 regmatch.rm_ic = ic;
1985 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1986 vim_regfree(regmatch.regprog);
1987 }
1988 p_cpo = save_cpo;
1989 return matches;
1990}
1991
1992/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001993 * Handle a name followed by "(". Both for just "name(arg)" and for
1994 * "expr->name(arg)".
1995 * Returns OK or FAIL.
1996 */
1997 static int
1998eval_func(
1999 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002000 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 char_u *name,
2002 int name_len,
2003 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002004 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 typval_T *basetv) // "expr" for "expr->name(arg)"
2006{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002007 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 char_u *s = name;
2009 int len = name_len;
2010 partial_T *partial;
2011 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002012 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013
2014 if (!evaluate)
2015 check_vars(s, len);
2016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // If "s" is the name of a variable of type VAR_FUNC
2018 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002019 s = deref_func_name(s, &len, &partial,
2020 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002022 // Need to make a copy, in case evaluating the arguments makes
2023 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002025 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 ret = FAIL;
2027 else
2028 {
2029 funcexe_T funcexe;
2030
2031 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002032 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033 funcexe.firstline = curwin->w_cursor.lnum;
2034 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035 funcexe.evaluate = evaluate;
2036 funcexe.partial = partial;
2037 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002038 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002039 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 }
2041 vim_free(s);
2042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // If evaluate is FALSE rettv->v_type was not set in
2044 // get_func_tv, but it's needed in handle_subscript() to parse
2045 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2047 {
2048 rettv->vval.v_string = NULL;
2049 rettv->v_type = VAR_FUNC;
2050 }
2051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002052 // Stop the expression evaluation when immediately
2053 // aborting on error, or when an interrupt occurred or
2054 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 if (evaluate && aborting())
2056 {
2057 if (ret == OK)
2058 clear_tv(rettv);
2059 ret = FAIL;
2060 }
2061 return ret;
2062}
2063
2064/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002065 * Get the next line source line without advancing. But do skip over comment
2066 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002067 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002068 */
2069 static char_u *
2070getline_peek_skip_comments(evalarg_T *evalarg)
2071{
2072 for (;;)
2073 {
2074 char_u *next = getline_peek(evalarg->eval_getline,
2075 evalarg->eval_cookie);
2076 char_u *p;
2077
2078 if (next == NULL)
2079 break;
2080 p = skipwhite(next);
2081 if (*p != NUL && !vim9_comment_start(p))
2082 return next;
2083 (void)eval_next_line(evalarg);
2084 }
2085 return NULL;
2086}
2087
2088/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002089 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2090 * comment) and there is a next line, return the next line (skipping blanks)
2091 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002092 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2093 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094 * "arg" must point somewhere inside a line, not at the start.
2095 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002096 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002097eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2098{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 char_u *p = skipwhite(arg);
2100
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002102 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002104 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002105 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002106 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002108
2109 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002110 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002111 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113
Bram Moolenaar9d489562020-07-30 20:08:50 +02002114 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 {
2116 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118 }
2119 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121}
2122
2123/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002125 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002126 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002127 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002128eval_next_line(evalarg_T *evalarg)
2129{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002130 garray_T *gap = &evalarg->eval_ga;
2131 char_u *line;
2132
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002133 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002134 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2135 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002136 else
2137 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002138 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002139 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2140 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002141 char_u *p = skipwhite(line);
2142
2143 // Going to concatenate the lines after parsing. For an empty or
2144 // comment line use an empty string.
2145 if (*p == NUL || vim9_comment_start(p))
2146 {
2147 vim_free(line);
2148 line = vim_strsave((char_u *)"");
2149 }
2150
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002151 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2152 ++gap->ga_len;
2153 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002154 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002155 {
2156 vim_free(evalarg->eval_tofree);
2157 evalarg->eval_tofree = line;
2158 }
2159 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002160}
2161
Bram Moolenaare6b53242020-07-01 17:28:33 +02002162/*
2163 * Call eval_next_non_blank() and get the next line if needed.
2164 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002165 char_u *
2166skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2167{
2168 int getnext;
2169 char_u *p = skipwhite(arg);
2170
Bram Moolenaar962d7212020-07-04 14:15:00 +02002171 if (evalarg == NULL)
2172 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002173 eval_next_non_blank(p, evalarg, &getnext);
2174 if (getnext)
2175 return eval_next_line(evalarg);
2176 return p;
2177}
2178
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002179/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002180 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002181 */
2182 void
2183clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2184{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002185 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002186 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002187 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002188 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002189 if (eap != NULL)
2190 {
2191 // We may need to keep the original command line, e.g. for
2192 // ":let" it has the variable names. But we may also need the
2193 // new one, "nextcmd" points into it. Keep both.
2194 vim_free(eap->cmdline_tofree);
2195 eap->cmdline_tofree = *eap->cmdlinep;
2196 *eap->cmdlinep = evalarg->eval_tofree;
2197 }
2198 else
2199 vim_free(evalarg->eval_tofree);
2200 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002201 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002202
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002203 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2204 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002205 }
2206}
2207
2208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2212 */
2213
2214/*
2215 * Handle zero level expression.
2216 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002218 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002219 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * Return OK or FAIL.
2221 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223eval0(
2224 char_u *arg,
2225 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002226 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int ret;
2230 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002231 int did_emsg_before = did_emsg;
2232 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002236 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002237 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002238
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002239 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /*
2244 * Report the invalid expression unless the expression evaluation has
2245 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002246 * exception, or we already gave a more specific error.
2247 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002249 if (!aborting()
2250 && did_emsg == did_emsg_before
2251 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002252 && (flags & EVAL_CONSTANT) == 0
2253 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002255
2256 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002257 // a next command to avoid more errors, unless "|" is following, which
2258 // could only be a command separator.
2259 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2260 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002263
2264 if (eap != NULL)
2265 eap->nextcmd = check_nextcmd(p);
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return ret;
2268}
2269
2270/*
2271 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002272 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002273 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 *
2275 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002276 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002278 * Note: "rettv.v_lock" is not set.
2279 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * Return OK or FAIL.
2281 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002282 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002285 char_u *p;
2286 int getnext;
2287
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002288 CLEAR_POINTER(rettv);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 /*
2291 * Get the first variable.
2292 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return FAIL;
2295
Bram Moolenaar793648f2020-06-26 21:28:25 +02002296 p = eval_next_non_blank(*arg, evalarg, &getnext);
2297 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002299 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 int result;
2301 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 evalarg_T *evalarg_used = evalarg;
2303 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002305 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002306 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307
2308 if (evalarg == NULL)
2309 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 CLEAR_FIELD(local_evalarg);
2311 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313 orig_flags = evalarg_used->eval_flags;
2314 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002316 if (getnext)
2317 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002318 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002319 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002321 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002322 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002323 clear_tv(rettv);
2324 return FAIL;
2325 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002326 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002327 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 int error = FALSE;
2333
Bram Moolenaar13106602020-10-04 16:06:05 +02002334 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002335 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002336 else if (vim9script)
2337 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002338 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 if (error || !op_falsy || !result)
2341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 if (error)
2343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002347 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002349 if (op_falsy)
2350 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002351 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002352 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002353 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002354 clear_tv(rettv);
2355 return FAIL;
2356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 evalarg_used->eval_flags = (op_falsy ? !result : result)
2359 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2360 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002361 {
2362 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002364 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002365 if (!op_falsy || !result)
2366 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002368 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002370 /*
2371 * Check for the ":".
2372 */
2373 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2374 if (*p != ':')
2375 {
2376 emsg(_(e_missing_colon));
2377 if (evaluate && result)
2378 clear_tv(rettv);
2379 evalarg_used->eval_flags = orig_flags;
2380 return FAIL;
2381 }
2382 if (getnext)
2383 *arg = eval_next_line(evalarg_used);
2384 else
2385 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002386 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 {
2388 error_white_both(p, 1);
2389 clear_tv(rettv);
2390 evalarg_used->eval_flags = orig_flags;
2391 return FAIL;
2392 }
2393 *arg = p;
2394 }
2395
2396 /*
2397 * Get the third variable. Recursive!
2398 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002399 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 {
2401 error_white_both(p, 1);
2402 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002403 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002404 return FAIL;
2405 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002406 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2407 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002409 if (eval1(arg, &var2, evalarg_used) == FAIL)
2410 {
2411 if (evaluate && result)
2412 clear_tv(rettv);
2413 evalarg_used->eval_flags = orig_flags;
2414 return FAIL;
2415 }
2416 if (evaluate && !result)
2417 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002419
2420 if (evalarg == NULL)
2421 clear_evalarg(&local_evalarg, NULL);
2422 else
2423 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
2426 return OK;
2427}
2428
2429/*
2430 * Handle first level expression:
2431 * expr2 || expr2 || expr2 logical OR
2432 *
2433 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002434 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 *
2436 * Return OK or FAIL.
2437 */
2438 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002441 char_u *p;
2442 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 /*
2445 * Get the first variable.
2446 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 return FAIL;
2449
2450 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002453 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 evalarg_T *evalarg_used = evalarg;
2457 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002458 int evaluate;
2459 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 long result = FALSE;
2461 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002462 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002463 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002464
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (evalarg == NULL)
2466 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 CLEAR_FIELD(local_evalarg);
2468 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 orig_flags = evalarg_used->eval_flags;
2471 evaluate = orig_flags & EVAL_EVALUATE;
2472 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002474 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002475 result = tv_get_bool_chk(rettv, &error);
2476 else if (tv_get_number_chk(rettv, &error) != 0)
2477 result = TRUE;
2478 clear_tv(rettv);
2479 if (error)
2480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486 while (p[0] == '|' && p[1] == '|')
2487 {
2488 if (getnext)
2489 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002490 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002491 {
2492 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2493 {
2494 error_white_both(p, 2);
2495 clear_tv(rettv);
2496 return FAIL;
2497 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002498 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500
2501 /*
2502 * Get the second variable.
2503 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002504 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2505 {
2506 error_white_both(p, 2);
2507 clear_tv(rettv);
2508 return FAIL;
2509 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002510 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2511 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002515
2516 /*
2517 * Compute the result.
2518 */
2519 if (evaluate && !result)
2520 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002521 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002522 result = tv_get_bool_chk(&var2, &error);
2523 else if (tv_get_number_chk(&var2, &error) != 0)
2524 result = TRUE;
2525 clear_tv(&var2);
2526 if (error)
2527 return FAIL;
2528 }
2529 if (evaluate)
2530 {
2531 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002532 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 rettv->v_type = VAR_BOOL;
2534 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002535 }
2536 else
2537 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002538 rettv->v_type = VAR_NUMBER;
2539 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542
2543 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546 if (evalarg == NULL)
2547 clear_evalarg(&local_evalarg, NULL);
2548 else
2549 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551
2552 return OK;
2553}
2554
2555/*
2556 * Handle second level expression:
2557 * expr3 && expr3 && expr3 logical AND
2558 *
2559 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002560 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 *
2562 * Return OK or FAIL.
2563 */
2564 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 char_u *p;
2568 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 /*
2571 * Get the first variable.
2572 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 return FAIL;
2575
2576 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002579 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 evalarg_T *evalarg_used = evalarg;
2583 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 int orig_flags;
2585 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 long result = TRUE;
2587 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002588 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002589 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 if (evalarg == NULL)
2592 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 CLEAR_FIELD(local_evalarg);
2594 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 orig_flags = evalarg_used->eval_flags;
2597 evaluate = orig_flags & EVAL_EVALUATE;
2598 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002600 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002601 result = tv_get_bool_chk(rettv, &error);
2602 else if (tv_get_number_chk(rettv, &error) == 0)
2603 result = FALSE;
2604 clear_tv(rettv);
2605 if (error)
2606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
2609 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612 while (p[0] == '&' && p[1] == '&')
2613 {
2614 if (getnext)
2615 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002616 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002617 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002618 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 {
2620 error_white_both(p, 2);
2621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002624 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626
2627 /*
2628 * Get the second variable.
2629 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2631 {
2632 error_white_both(p, 2);
2633 clear_tv(rettv);
2634 return FAIL;
2635 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2637 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002638 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002639 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002642
2643 /*
2644 * Compute the result.
2645 */
2646 if (evaluate && result)
2647 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002648 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002649 result = tv_get_bool_chk(&var2, &error);
2650 else if (tv_get_number_chk(&var2, &error) == 0)
2651 result = FALSE;
2652 clear_tv(&var2);
2653 if (error)
2654 return FAIL;
2655 }
2656 if (evaluate)
2657 {
2658 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 rettv->v_type = VAR_BOOL;
2661 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 }
2663 else
2664 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002665 rettv->v_type = VAR_NUMBER;
2666 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669
2670 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002672
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 if (evalarg == NULL)
2674 clear_evalarg(&local_evalarg, NULL);
2675 else
2676 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 return OK;
2680}
2681
2682/*
2683 * Handle third level expression:
2684 * var1 == var2
2685 * var1 =~ var2
2686 * var1 != var2
2687 * var1 !~ var2
2688 * var1 > var2
2689 * var1 >= var2
2690 * var1 < var2
2691 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002692 * var1 is var2
2693 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 *
2695 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002696 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 *
2698 * Return OK or FAIL.
2699 */
2700 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002701eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002704 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002705 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002707 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 /*
2710 * Get the first variable.
2711 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return FAIL;
2714
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002716 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002721 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002723 typval_T var2;
2724 int ic;
2725 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002726 int evaluate = evalarg == NULL
2727 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002728
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002729 if (getnext)
2730 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002731 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2732 {
2733 error_white_both(p, len);
2734 clear_tv(rettv);
2735 return FAIL;
2736 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002737
Bram Moolenaar696ba232020-07-29 21:20:41 +02002738 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2739 {
2740 semsg(_(e_invexpr2), p);
2741 clear_tv(rettv);
2742 return FAIL;
2743 }
2744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002745 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (p[len] == '?')
2747 {
2748 ic = TRUE;
2749 ++len;
2750 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if (p[len] == '#')
2753 {
2754 ic = FALSE;
2755 ++len;
2756 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002757 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002759 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Get the second variable.
2763 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002764 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2765 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002766 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002767 clear_tv(rettv);
2768 return FAIL;
2769 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002770 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002771 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return FAIL;
2775 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002776 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002777 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002778 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002779
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002780 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002781 {
2782 ret = FAIL;
2783 clear_tv(rettv);
2784 }
2785 else
2786 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002787 clear_tv(&var2);
2788 return ret;
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791
2792 return OK;
2793}
2794
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002795/*
2796 * Make a copy of blob "tv1" and append blob "tv2".
2797 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 void
2799eval_addblob(typval_T *tv1, typval_T *tv2)
2800{
2801 blob_T *b1 = tv1->vval.v_blob;
2802 blob_T *b2 = tv2->vval.v_blob;
2803 blob_T *b = blob_alloc();
2804 int i;
2805
2806 if (b != NULL)
2807 {
2808 for (i = 0; i < blob_len(b1); i++)
2809 ga_append(&b->bv_ga, blob_get(b1, i));
2810 for (i = 0; i < blob_len(b2); i++)
2811 ga_append(&b->bv_ga, blob_get(b2, i));
2812
2813 clear_tv(tv1);
2814 rettv_blob_set(tv1, b);
2815 }
2816}
2817
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002818/*
2819 * Make a copy of list "tv1" and append list "tv2".
2820 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 int
2822eval_addlist(typval_T *tv1, typval_T *tv2)
2823{
2824 typval_T var3;
2825
2826 // concatenate Lists
2827 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2828 {
2829 clear_tv(tv1);
2830 clear_tv(tv2);
2831 return FAIL;
2832 }
2833 clear_tv(tv1);
2834 *tv1 = var3;
2835 return OK;
2836}
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838/*
2839 * Handle fourth level expression:
2840 * + number addition
2841 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002842 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002843 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *
2845 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 *
2848 * Return OK or FAIL.
2849 */
2850 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /*
2854 * Get the first variable.
2855 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return FAIL;
2858
2859 /*
2860 * Repeat computing, until no '+', '-' or '.' is following.
2861 */
2862 for (;;)
2863 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002864 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865 int getnext;
2866 char_u *p;
2867 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002868 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 int concat;
2870 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002871 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002873 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002874 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002875 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002876 p = eval_next_non_blank(*arg, evalarg, &getnext);
2877 op = *p;
2878 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002879 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2880 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002882 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2883 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002884
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002885 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002886 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002888 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002889 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002890 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002891 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002892 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002893 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002894 clear_tv(rettv);
2895 return FAIL;
2896 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002897 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002898 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002899 if ((op != '+' || (rettv->v_type != VAR_LIST
2900 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002901#ifdef FEAT_FLOAT
2902 && (op == '.' || rettv->v_type != VAR_FLOAT)
2903#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002904 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002905 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002906 int error = FALSE;
2907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002908 // For "list + ...", an illegal use of the first operand as
2909 // a number cannot be determined before evaluating the 2nd
2910 // operand: if this is also a list, all is ok.
2911 // For "something . ...", "something - ..." or "non-list + ...",
2912 // we know that the first operand needs to be a string or number
2913 // without evaluating the 2nd operand. So check before to avoid
2914 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002915 if (op != '.')
2916 tv_get_number_chk(rettv, &error);
2917 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002918 {
2919 clear_tv(rettv);
2920 return FAIL;
2921 }
2922 }
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /*
2925 * Get the second variable.
2926 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002927 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002928 {
2929 error_white_both(p, oplen);
2930 clear_tv(rettv);
2931 return FAIL;
2932 }
2933 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002934 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return FAIL;
2938 }
2939
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
2942 /*
2943 * Compute the result.
2944 */
2945 if (op == '.')
2946 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2948 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002949 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950
Bram Moolenaar2e086612020-08-25 22:37:48 +02002951 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002952 || var2.v_type == VAR_CHANNEL
2953 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002954 semsg(_(e_using_invalid_value_as_string_str),
2955 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002956#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002957 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002958 {
2959 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2960 var2.vval.v_float);
2961 s2 = buf2;
2962 }
2963#endif
2964 else
2965 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002966 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967 {
2968 clear_tv(rettv);
2969 clear_tv(&var2);
2970 return FAIL;
2971 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 clear_tv(rettv);
2974 rettv->v_type = VAR_STRING;
2975 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002977 else if (op == '+' && rettv->v_type == VAR_BLOB
2978 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002980 else if (op == '+' && rettv->v_type == VAR_LIST
2981 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002982 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002984 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 else
2987 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 int error = FALSE;
2989 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002991 float_T f1 = 0, f2 = 0;
2992
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002993 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002995 f1 = rettv->vval.v_float;
2996 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002997 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998 else
2999#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003001 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 if (error)
3003 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003004 // This can only happen for "list + non-list" or
3005 // "blob + non-blob". For "non-list + ..." or
3006 // "something - ...", we returned before evaluating the
3007 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003008 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003009 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010 return FAIL;
3011 }
3012#ifdef FEAT_FLOAT
3013 if (var2.v_type == VAR_FLOAT)
3014 f1 = n1;
3015#endif
3016 }
3017#ifdef FEAT_FLOAT
3018 if (var2.v_type == VAR_FLOAT)
3019 {
3020 f2 = var2.vval.v_float;
3021 n2 = 0;
3022 }
3023 else
3024#endif
3025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003026 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027 if (error)
3028 {
3029 clear_tv(rettv);
3030 clear_tv(&var2);
3031 return FAIL;
3032 }
3033#ifdef FEAT_FLOAT
3034 if (rettv->v_type == VAR_FLOAT)
3035 f2 = n2;
3036#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003038 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039
3040#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3043 {
3044 if (op == '+')
3045 f1 = f1 + f2;
3046 else
3047 f1 = f1 - f2;
3048 rettv->v_type = VAR_FLOAT;
3049 rettv->vval.v_float = f1;
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052#endif
3053 {
3054 if (op == '+')
3055 n1 = n1 + n2;
3056 else
3057 n1 = n1 - n2;
3058 rettv->v_type = VAR_NUMBER;
3059 rettv->vval.v_number = n1;
3060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064 }
3065 return OK;
3066}
3067
3068/*
3069 * Handle fifth level expression:
3070 * * number multiplication
3071 * / number division
3072 * % number modulo
3073 *
3074 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003075 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 *
3077 * Return OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080eval6(
3081 char_u **arg,
3082 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003083 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003084 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003087 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 /*
3091 * Get the first variable.
3092 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003093 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 return FAIL;
3095
3096 /*
3097 * Repeat computing, until no '*', '/' or '%' is following.
3098 */
3099 for (;;)
3100 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003101 int evaluate;
3102 int getnext;
3103 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003104 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003105 int op;
3106 varnumber_T n1, n2;
3107#ifdef FEAT_FLOAT
3108 float_T f1, f2;
3109#endif
3110 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003111
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003112 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003113 p = eval_next_non_blank(*arg, evalarg, &getnext);
3114 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003115 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003117
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003118 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003119 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003120 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003121 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003122 {
3123 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3124 {
3125 error_white_both(p, 1);
3126 clear_tv(rettv);
3127 return FAIL;
3128 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003129 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003132#ifdef FEAT_FLOAT
3133 f1 = 0;
3134 f2 = 0;
3135#endif
3136 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003137 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139#ifdef FEAT_FLOAT
3140 if (rettv->v_type == VAR_FLOAT)
3141 {
3142 f1 = rettv->vval.v_float;
3143 use_float = TRUE;
3144 n1 = 0;
3145 }
3146 else
3147#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003149 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003150 if (error)
3151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
3153 else
3154 n1 = 0;
3155
3156 /*
3157 * Get the second variable.
3158 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003159 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3160 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003161 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003162 clear_tv(rettv);
3163 return FAIL;
3164 }
3165 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003166 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 return FAIL;
3168
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003169 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#ifdef FEAT_FLOAT
3172 if (var2.v_type == VAR_FLOAT)
3173 {
3174 if (!use_float)
3175 {
3176 f1 = n1;
3177 use_float = TRUE;
3178 }
3179 f2 = var2.vval.v_float;
3180 n2 = 0;
3181 }
3182 else
3183#endif
3184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003185 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003186 clear_tv(&var2);
3187 if (error)
3188 return FAIL;
3189#ifdef FEAT_FLOAT
3190 if (use_float)
3191 f2 = n2;
3192#endif
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /*
3196 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003199#ifdef FEAT_FLOAT
3200 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 if (op == '*')
3203 f1 = f1 * f2;
3204 else if (op == '/')
3205 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003206# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003208 if (f2 == 0.0)
3209 {
3210 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003211 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003212 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003213 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003214 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003215 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003216 }
3217 else
3218 f1 = f1 / f2;
3219# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003220 // We rely on the floating point library to handle divide
3221 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003223# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228 return FAIL;
3229 }
3230 rettv->v_type = VAR_FLOAT;
3231 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003236 int failed = FALSE;
3237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003238 if (op == '*')
3239 n1 = n1 * n2;
3240 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003241 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003243 n1 = num_modulus(n1, n2, &failed);
3244 if (failed)
3245 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003246
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003247 rettv->v_type = VAR_NUMBER;
3248 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 }
3252
3253 return OK;
3254}
3255
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003256/*
3257 * Handle a type cast before a base level expression.
3258 * "arg" must point to the first non-white of the expression.
3259 * "arg" is advanced to just after the recognized expression.
3260 * Return OK or FAIL.
3261 */
3262 static int
3263eval7t(
3264 char_u **arg,
3265 typval_T *rettv,
3266 evalarg_T *evalarg,
3267 int want_string) // after "." operator
3268{
3269 type_T *want_type = NULL;
3270 garray_T type_list; // list of pointers to allocated types
3271 int res;
3272 int evaluate = evalarg == NULL ? 0
3273 : (evalarg->eval_flags & EVAL_EVALUATE);
3274
3275 // Recognize <type> in Vim9 script only.
3276 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3277 {
3278 ++*arg;
3279 ga_init2(&type_list, sizeof(type_T *), 10);
3280 want_type = parse_type(arg, &type_list, TRUE);
3281 if (want_type == NULL && (evaluate || **arg != '>'))
3282 {
3283 clear_type_list(&type_list);
3284 return FAIL;
3285 }
3286
3287 if (**arg != '>')
3288 {
3289 if (*skipwhite(*arg) == '>')
3290 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3291 else
3292 emsg(_(e_missing_gt));
3293 clear_type_list(&type_list);
3294 return FAIL;
3295 }
3296 ++*arg;
3297 *arg = skipwhite_and_linebreak(*arg, evalarg);
3298 }
3299
3300 res = eval7(arg, rettv, evalarg, want_string);
3301
3302 if (want_type != NULL && evaluate)
3303 {
3304 if (res == OK)
3305 {
3306 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3307
3308 if (!equal_type(want_type, actual))
3309 {
3310 if (want_type == &t_bool && actual != &t_bool
3311 && (actual->tt_flags & TTFLAG_BOOL_OK))
3312 {
3313 int n = tv2bool(rettv);
3314
3315 // can use "0" and "1" for boolean in some places
3316 clear_tv(rettv);
3317 rettv->v_type = VAR_BOOL;
3318 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3319 }
3320 else
3321 {
3322 where_T where;
3323
3324 where.wt_index = 0;
3325 where.wt_variable = TRUE;
3326 res = check_type(want_type, actual, TRUE, where);
3327 }
3328 }
3329 }
3330 clear_type_list(&type_list);
3331 }
3332
3333 return res;
3334}
3335
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003336 int
3337eval_leader(char_u **arg, int vim9)
3338{
3339 char_u *s = *arg;
3340 char_u *p = *arg;
3341
3342 while (*p == '!' || *p == '-' || *p == '+')
3343 {
3344 char_u *n = skipwhite(p + 1);
3345
3346 // ++, --, -+ and +- are not accepted in Vim9 script
3347 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3348 {
3349 semsg(_(e_invexpr2), s);
3350 return FAIL;
3351 }
3352 p = n;
3353 }
3354 *arg = p;
3355 return OK;
3356}
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358/*
3359 * Handle sixth level expression:
3360 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003361 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003362 * "string" string constant
3363 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * &option-name option value
3365 * @r register contents
3366 * identifier variable value
3367 * function() function call
3368 * $VAR environment variable
3369 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003372 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003373 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 *
3375 * Also handle:
3376 * ! in front logical NOT
3377 * - in front unary minus
3378 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003379 * trailing [] subscript in String or List
3380 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003381 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 *
3383 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003384 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 *
3386 * Return OK or FAIL.
3387 */
3388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389eval7(
3390 char_u **arg,
3391 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003392 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003395 int evaluate = evalarg != NULL
3396 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 int len;
3398 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 char_u *start_leader, *end_leader;
3400 int ret = OK;
3401 char_u *alias;
3402
3403 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003405 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003410 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 */
3412 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003413 if (eval_leader(arg, in_vim9script()) == FAIL)
3414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 end_leader = *arg;
3416
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003417 if (**arg == '.' && (!isdigit(*(*arg + 1))
3418#ifdef FEAT_FLOAT
3419 || current_sctx.sc_version < 2
3420#endif
3421 ))
3422 {
3423 semsg(_(e_invexpr2), *arg);
3424 ++*arg;
3425 return FAIL;
3426 }
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 switch (**arg)
3429 {
3430 /*
3431 * Number constant.
3432 */
3433 case '0':
3434 case '1':
3435 case '2':
3436 case '3':
3437 case '4':
3438 case '5':
3439 case '6':
3440 case '7':
3441 case '8':
3442 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003443 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003444
3445 // Apply prefixed "-" and "+" now. Matters especially when
3446 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003447 if (ret == OK && evaluate && end_leader > start_leader
3448 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003449 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 break;
3451
3452 /*
3453 * String constant: "string".
3454 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003455 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 break;
3457
3458 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003461 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003462 break;
3463
3464 /*
3465 * List: [expr, expr]
3466 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003467 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 break;
3469
3470 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003471 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003472 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003473 case '#': if (in_vim9script())
3474 {
3475 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3476 }
3477 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003478 {
3479 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003480 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003481 }
3482 else
3483 ret = NOTDONE;
3484 break;
3485
3486 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003487 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003488 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003489 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003490 case '{': if (in_vim9script())
3491 ret = NOTDONE;
3492 else
3493 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003494 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003495 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003496 break;
3497
3498 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003499 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003501 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 break;
3503
3504 /*
3505 * Environment variable: $VAR.
3506 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003507 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510 /*
3511 * Register contents: @r.
3512 */
3513 case '@': ++*arg;
3514 if (evaluate)
3515 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003516 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3517 semsg(_(e_syntax_error_at_str), *arg);
3518 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3519 emsg_invreg(**arg);
3520 else
3521 {
3522 rettv->v_type = VAR_STRING;
3523 rettv->vval.v_string = get_reg_contents(**arg,
3524 GREG_EXPR_SRC);
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 if (**arg != NUL)
3528 ++*arg;
3529 break;
3530
3531 /*
3532 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003533 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003535 case '(': ret = NOTDONE;
3536 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003537 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003538 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003539 if (ret == OK && evaluate)
3540 {
3541 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3542
3543 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003544 if (compile_def_function(ufunc,
3545 TRUE, PROFILING(ufunc), NULL) == FAIL)
3546 {
3547 clear_tv(rettv);
3548 ret = FAIL;
3549 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003550 }
3551 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003552 if (ret == NOTDONE)
3553 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003554 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003555 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003556
Bram Moolenaar9215f012020-06-27 21:18:00 +02003557 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003558 if (**arg == ')')
3559 ++*arg;
3560 else if (ret == OK)
3561 {
3562 emsg(_(e_missing_close));
3563 clear_tv(rettv);
3564 ret = FAIL;
3565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 break;
3568
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 break;
3571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003572
3573 if (ret == NOTDONE)
3574 {
3575 /*
3576 * Must be a variable or function name.
3577 * Can also be a curly-braces kind of name: {expr}.
3578 */
3579 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003580 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 if (alias != NULL)
3582 s = alias;
3583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003584 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 ret = FAIL;
3586 else
3587 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003588 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3589
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003590 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003591 {
3592 emsg(_(e_cannot_use_underscore_here));
3593 ret = FAIL;
3594 }
3595 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003596 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003597 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003598 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003599 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003600 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003601 else if (flags & EVAL_CONSTANT)
3602 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003604 {
3605 // get the value of "true", "false" or a variable
3606 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3607 {
3608 rettv->v_type = VAR_BOOL;
3609 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003610 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003611 }
3612 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003613 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003614 {
3615 rettv->v_type = VAR_BOOL;
3616 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003617 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003618 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003619 else if (len == 4 && in_vim9script()
3620 && STRNCMP(s, "null", 4) == 0)
3621 {
3622 rettv->v_type = VAR_SPECIAL;
3623 rettv->vval.v_number = VVAL_NULL;
3624 ret = OK;
3625 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003626 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003627 ret = eval_variable(s, len, rettv, NULL,
3628 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003629 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003630 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003631 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003633 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003634 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003637 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638 }
3639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003640 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3641 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003642 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003643 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
3646 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3647 */
3648 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003649 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003650 return ret;
3651}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003652
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003653/*
3654 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003655 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003656 * Adjusts "end_leaderp" until it is at "start_leader".
3657 */
3658 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003659eval7_leader(
3660 typval_T *rettv,
3661 int numeric_only,
3662 char_u *start_leader,
3663 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664{
3665 char_u *end_leader = *end_leaderp;
3666 int ret = OK;
3667 int error = FALSE;
3668 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003669 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003670#ifdef FEAT_FLOAT
3671 float_T f = 0.0;
3672
3673 if (rettv->v_type == VAR_FLOAT)
3674 f = rettv->vval.v_float;
3675 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003676#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003677 {
3678 while (VIM_ISWHITE(end_leader[-1]))
3679 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003680 if (in_vim9script() && end_leader[-1] == '!')
3681 val = tv2bool(rettv);
3682 else
3683 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003684 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003685 if (error)
3686 {
3687 clear_tv(rettv);
3688 ret = FAIL;
3689 }
3690 else
3691 {
3692 while (end_leader > start_leader)
3693 {
3694 --end_leader;
3695 if (*end_leader == '!')
3696 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003697 if (numeric_only)
3698 {
3699 ++end_leader;
3700 break;
3701 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003702#ifdef FEAT_FLOAT
3703 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003704 {
3705 if (in_vim9script())
3706 {
3707 rettv->v_type = VAR_BOOL;
3708 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3709 }
3710 else
3711 f = !f;
3712 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003713 else
3714#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003715 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003716 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003717 type = VAR_BOOL;
3718 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003719 }
3720 else if (*end_leader == '-')
3721 {
3722#ifdef FEAT_FLOAT
3723 if (rettv->v_type == VAR_FLOAT)
3724 f = -f;
3725 else
3726#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003727 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003728 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003729 type = VAR_NUMBER;
3730 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003731 }
3732 }
3733#ifdef FEAT_FLOAT
3734 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003739 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003743 if (in_vim9script())
3744 rettv->v_type = type;
3745 else
3746 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003747 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003750 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 return ret;
3752}
3753
3754/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003755 * Call the function referred to in "rettv".
3756 */
3757 static int
3758call_func_rettv(
3759 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003760 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003761 typval_T *rettv,
3762 int evaluate,
3763 dict_T *selfdict,
3764 typval_T *basetv)
3765{
3766 partial_T *pt = NULL;
3767 funcexe_T funcexe;
3768 typval_T functv;
3769 char_u *s;
3770 int ret;
3771
3772 // need to copy the funcref so that we can clear rettv
3773 if (evaluate)
3774 {
3775 functv = *rettv;
3776 rettv->v_type = VAR_UNKNOWN;
3777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003779 if (functv.v_type == VAR_PARTIAL)
3780 {
3781 pt = functv.vval.v_partial;
3782 s = partial_name(pt);
3783 }
3784 else
3785 s = functv.vval.v_string;
3786 }
3787 else
3788 s = (char_u *)"";
3789
Bram Moolenaara80faa82020-04-12 19:37:17 +02003790 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003791 funcexe.firstline = curwin->w_cursor.lnum;
3792 funcexe.lastline = curwin->w_cursor.lnum;
3793 funcexe.evaluate = evaluate;
3794 funcexe.partial = pt;
3795 funcexe.selfdict = selfdict;
3796 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003797 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // Clear the funcref afterwards, so that deleting it while
3800 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003801 if (evaluate)
3802 clear_tv(&functv);
3803
3804 return ret;
3805}
3806
3807/*
3808 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003809 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003810 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3811 */
3812 static int
3813eval_lambda(
3814 char_u **arg,
3815 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003816 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003818{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003819 int evaluate = evalarg != NULL
3820 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003821 typval_T base = *rettv;
3822 int ret;
3823
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003824 rettv->v_type = VAR_UNKNOWN;
3825
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003826 if (**arg == '{')
3827 {
3828 // ->{lambda}()
3829 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3830 }
3831 else
3832 {
3833 // ->(lambda)()
3834 ++*arg;
3835 ret = eval1(arg, rettv, evalarg);
3836 *arg = skipwhite_and_linebreak(*arg, evalarg);
3837 if (**arg != ')')
3838 {
3839 emsg(_(e_missing_close));
3840 ret = FAIL;
3841 }
3842 ++*arg;
3843 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003844 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 return FAIL;
3846 else if (**arg != '(')
3847 {
3848 if (verbose)
3849 {
3850 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003851 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003852 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003854 }
3855 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003856 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003857 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003858 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003859 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003860
3861 // Clear the funcref afterwards, so that deleting it while
3862 // evaluating the arguments is possible (see test55).
3863 if (evaluate)
3864 clear_tv(&base);
3865
3866 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003867}
3868
3869/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003870 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003871 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003872 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3873 */
3874 static int
3875eval_method(
3876 char_u **arg,
3877 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003878 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003880{
3881 char_u *name;
3882 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003883 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003884 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003885 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003886 int evaluate = evalarg != NULL
3887 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003888
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003889 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003890
Bram Moolenaarac92e252019-08-03 21:58:38 +02003891 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003892 len = get_name_len(arg, &alias, evaluate, TRUE);
3893 if (alias != NULL)
3894 name = alias;
3895
3896 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897 {
3898 if (verbose)
3899 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003900 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003902 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003903 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003904 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003905 if (**arg != '(')
3906 {
3907 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003909 ret = FAIL;
3910 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003911 else if (VIM_ISWHITE((*arg)[-1]))
3912 {
3913 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003914 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003915 ret = FAIL;
3916 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003917 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003918 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003919 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003921
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003922 // Clear the funcref afterwards, so that deleting it while
3923 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003924 if (evaluate)
3925 clear_tv(&base);
3926
Bram Moolenaarac92e252019-08-03 21:58:38 +02003927 return ret;
3928}
3929
3930/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003931 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3932 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003933 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3934 */
3935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936eval_index(
3937 char_u **arg,
3938 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003939 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003940 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003942 int evaluate = evalarg != NULL
3943 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003946 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003947 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003948 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003949 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003950
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003951 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3952 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003953
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003954 init_tv(&var1);
3955 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003956 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 /*
3959 * dict.name
3960 */
3961 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003964 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003966 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967 }
3968 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003970 /*
3971 * something[idx]
3972 *
3973 * Get the (first) variable from inside the [].
3974 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003975 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003976 if (**arg == ':')
3977 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003978 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003979 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003980 else if (vim9 && **arg == ':')
3981 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003982 semsg(_(e_white_space_required_before_and_after_str_at_str),
3983 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003984 clear_tv(&var1);
3985 return FAIL;
3986 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003987 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003989#ifdef FEAT_FLOAT
3990 // allow for indexing with float
3991 if (vim9 && rettv->v_type == VAR_DICT
3992 && var1.v_type == VAR_FLOAT)
3993 {
3994 var1.vval.v_string = typval_tostring(&var1, TRUE);
3995 var1.v_type = VAR_STRING;
3996 }
3997#endif
3998 if (tv_get_string_chk(&var1) == NULL)
3999 {
4000 // not a number or string
4001 clear_tv(&var1);
4002 return FAIL;
4003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004005
4006 /*
4007 * Get the second variable from inside the [:].
4008 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004009 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004010 if (**arg == ':')
4011 {
4012 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004013 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004014 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004015 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004016 semsg(_(e_white_space_required_before_and_after_str_at_str),
4017 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004018 if (!empty1)
4019 clear_tv(&var1);
4020 return FAIL;
4021 }
4022 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 if (**arg == ']')
4024 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004025 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027 if (!empty1)
4028 clear_tv(&var1);
4029 return FAIL;
4030 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004031 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004034 if (!empty1)
4035 clear_tv(&var1);
4036 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 return FAIL;
4038 }
4039 }
4040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004042 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 if (**arg != ']')
4044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004045 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004046 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 clear_tv(&var1);
4048 if (range)
4049 clear_tv(&var2);
4050 return FAIL;
4051 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004052 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004053 }
4054
4055 if (evaluate)
4056 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004057 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004058 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004059 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004060
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004061 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004063 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004064 clear_tv(&var2);
4065 return res;
4066 }
4067 return OK;
4068}
4069
4070/*
4071 * Check if "rettv" can have an [index] or [sli:ce]
4072 */
4073 int
4074check_can_index(typval_T *rettv, int evaluate, int verbose)
4075{
4076 switch (rettv->v_type)
4077 {
4078 case VAR_FUNC:
4079 case VAR_PARTIAL:
4080 if (verbose)
4081 emsg(_("E695: Cannot index a Funcref"));
4082 return FAIL;
4083 case VAR_FLOAT:
4084#ifdef FEAT_FLOAT
4085 if (verbose)
4086 emsg(_(e_float_as_string));
4087 return FAIL;
4088#endif
4089 case VAR_BOOL:
4090 case VAR_SPECIAL:
4091 case VAR_JOB:
4092 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004093 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004094 if (verbose)
4095 emsg(_(e_cannot_index_special_variable));
4096 return FAIL;
4097 case VAR_UNKNOWN:
4098 case VAR_ANY:
4099 case VAR_VOID:
4100 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004101 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004102 emsg(_(e_cannot_index_special_variable));
4103 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004104 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004105 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004106
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004107 case VAR_STRING:
4108 case VAR_LIST:
4109 case VAR_DICT:
4110 case VAR_BLOB:
4111 break;
4112 case VAR_NUMBER:
4113 if (in_vim9script())
4114 emsg(_(e_cannot_index_number));
4115 break;
4116 }
4117 return OK;
4118}
4119
4120/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004121 * slice() function
4122 */
4123 void
4124f_slice(typval_T *argvars, typval_T *rettv)
4125{
4126 if (check_can_index(argvars, TRUE, FALSE) == OK)
4127 {
4128 copy_tv(argvars, rettv);
4129 eval_index_inner(rettv, TRUE, argvars + 1,
4130 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4131 TRUE, NULL, 0, FALSE);
4132 }
4133}
4134
4135/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004136 * Apply index or range to "rettv".
4137 * "var1" is the first index, NULL for [:expr].
4138 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004139 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4140 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004141 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4142 */
4143 int
4144eval_index_inner(
4145 typval_T *rettv,
4146 int is_range,
4147 typval_T *var1,
4148 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004149 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 char_u *key,
4151 int keylen,
4152 int verbose)
4153{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004154 varnumber_T n1, n2 = 0;
4155 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004156
4157 n1 = 0;
4158 if (var1 != NULL && rettv->v_type != VAR_DICT)
4159 n1 = tv_get_number(var1);
4160
4161 if (is_range)
4162 {
4163 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004164 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 if (verbose)
4166 emsg(_(e_cannot_slice_dictionary));
4167 return FAIL;
4168 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004169 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004171 else
4172 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004174
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 switch (rettv->v_type)
4176 {
4177 case VAR_UNKNOWN:
4178 case VAR_ANY:
4179 case VAR_VOID:
4180 case VAR_FUNC:
4181 case VAR_PARTIAL:
4182 case VAR_FLOAT:
4183 case VAR_BOOL:
4184 case VAR_SPECIAL:
4185 case VAR_JOB:
4186 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004187 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004188 break; // not evaluating, skipping over subscript
4189
4190 case VAR_NUMBER:
4191 case VAR_STRING:
4192 {
4193 char_u *s = tv_get_string(rettv);
4194
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004196 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004197 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004198 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004199 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004200 else
4201 s = char_from_string(s, n1);
4202 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004203 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004204 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004205 // The resulting variable is a substring. If the indexes
4206 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004207 if (n1 < 0)
4208 {
4209 n1 = len + n1;
4210 if (n1 < 0)
4211 n1 = 0;
4212 }
4213 if (n2 < 0)
4214 n2 = len + n2;
4215 else if (n2 >= len)
4216 n2 = len;
4217 if (n1 >= len || n2 < 0 || n1 > n2)
4218 s = NULL;
4219 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004220 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 }
4222 else
4223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004224 // The resulting variable is a string of a single
4225 // character. If the index is too big or negative the
4226 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004227 if (n1 >= len || n1 < 0)
4228 s = NULL;
4229 else
4230 s = vim_strnsave(s + n1, 1);
4231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
4233 rettv->v_type = VAR_STRING;
4234 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 }
4236 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004237
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004238 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004239 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4240 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004241 break;
4242
4243 case VAR_LIST:
4244 if (var1 == NULL)
4245 n1 = 0;
4246 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004247 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004248 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004249 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004250 return FAIL;
4251 break;
4252
4253 case VAR_DICT:
4254 {
4255 dictitem_T *item;
4256 typval_T tmp;
4257
4258 if (key == NULL)
4259 {
4260 key = tv_get_string_chk(var1);
4261 if (key == NULL)
4262 return FAIL;
4263 }
4264
4265 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4266
4267 if (item == NULL && verbose)
4268 semsg(_(e_dictkey), key);
4269 if (item == NULL)
4270 return FAIL;
4271
4272 copy_tv(&item->di_tv, &tmp);
4273 clear_tv(rettv);
4274 *rettv = tmp;
4275 }
4276 break;
4277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 return OK;
4279}
4280
4281/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004282 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004283 */
4284 char_u *
4285partial_name(partial_T *pt)
4286{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004287 if (pt != NULL)
4288 {
4289 if (pt->pt_name != NULL)
4290 return pt->pt_name;
4291 if (pt->pt_func != NULL)
4292 return pt->pt_func->uf_name;
4293 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004294 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004295}
4296
Bram Moolenaarddecc252016-04-06 22:59:37 +02004297 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004298partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004299{
4300 int i;
4301
4302 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004303 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004304 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004306 if (pt->pt_name != NULL)
4307 {
4308 func_unref(pt->pt_name);
4309 vim_free(pt->pt_name);
4310 }
4311 else
4312 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004313
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004314 // Decrease the reference count for the context of a closure. If down
4315 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004316 if (pt->pt_funcstack != NULL)
4317 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004318 --pt->pt_funcstack->fs_refcount;
4319 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004320 }
4321
Bram Moolenaarddecc252016-04-06 22:59:37 +02004322 vim_free(pt);
4323}
4324
4325/*
4326 * Unreference a closure: decrement the reference count and free it when it
4327 * becomes zero.
4328 */
4329 void
4330partial_unref(partial_T *pt)
4331{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004332 if (pt != NULL)
4333 {
4334 if (--pt->pt_refcount <= 0)
4335 partial_free(pt);
4336
4337 // If the reference count goes down to one, the funcstack may be the
4338 // only reference and can be freed if no other partials reference it.
4339 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4340 funcstack_check_refcount(pt->pt_funcstack);
4341 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004342}
4343
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004345 * Return the next (unique) copy ID.
4346 * Used for serializing nested structures.
4347 */
4348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004349get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004350{
4351 current_copyID += COPYID_INC;
4352 return current_copyID;
4353}
4354
4355/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004356 * Garbage collection for lists and dictionaries.
4357 *
4358 * We use reference counts to be able to free most items right away when they
4359 * are no longer used. But for composite items it's possible that it becomes
4360 * unused while the reference count is > 0: When there is a recursive
4361 * reference. Example:
4362 * :let l = [1, 2, 3]
4363 * :let d = {9: l}
4364 * :let l[1] = d
4365 *
4366 * Since this is quite unusual we handle this with garbage collection: every
4367 * once in a while find out which lists and dicts are not referenced from any
4368 * variable.
4369 *
4370 * Here is a good reference text about garbage collection (refers to Python
4371 * but it applies to all reference-counting mechanisms):
4372 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004373 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004374
4375/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004376 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004377 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004378 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004379 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004381garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004382{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004383 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004384 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004385 buf_T *buf;
4386 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004387 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004388 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004390 if (!testing)
4391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004392 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004393 want_garbage_collect = FALSE;
4394 may_garbage_collect = FALSE;
4395 garbage_collect_at_exit = FALSE;
4396 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004397
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004398 // The execution stack can grow big, limit the size.
4399 if (exestack.ga_maxlen - exestack.ga_len > 500)
4400 {
4401 size_t new_len;
4402 char_u *pp;
4403 int n;
4404
4405 // Keep 150% of the current size, with a minimum of the growth size.
4406 n = exestack.ga_len / 2;
4407 if (n < exestack.ga_growsize)
4408 n = exestack.ga_growsize;
4409
4410 // Don't make it bigger though.
4411 if (exestack.ga_len + n < exestack.ga_maxlen)
4412 {
4413 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4414 pp = vim_realloc(exestack.ga_data, new_len);
4415 if (pp == NULL)
4416 return FAIL;
4417 exestack.ga_maxlen = exestack.ga_len + n;
4418 exestack.ga_data = pp;
4419 }
4420 }
4421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004422 // We advance by two because we add one for items referenced through
4423 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004424 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004425
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004426 /*
4427 * 1. Go through all accessible variables and mark all lists and dicts
4428 * with copyID.
4429 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 // Don't free variables in the previous_funccal list unless they are only
4432 // referenced through previous_funccal. This must be first, because if
4433 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004437 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004440 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004441 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4442 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004445 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004446 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4447 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004448 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4450 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004451#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004452 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004453 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4454 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004455 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004456 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004457 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4458 NULL, NULL);
4459#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004461 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004462 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4464 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004465 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004466 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004472 abort = abort || set_ref_in_functions(copyID);
4473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004474 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004478 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004479
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004480 // callbacks in buffers
4481 abort = abort || set_ref_in_buffers(copyID);
4482
Bram Moolenaar1dced572012-04-05 16:54:08 +02004483#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004484 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004485#endif
4486
Bram Moolenaardb913952012-06-29 12:54:53 +02004487#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004488 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004489#endif
4490
4491#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004492 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004493#endif
4494
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004496 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004497 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004498#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004499#ifdef FEAT_NETBEANS_INTG
4500 abort = abort || set_ref_in_nb_channel(copyID);
4501#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004502
Bram Moolenaare3188e22016-05-31 21:13:04 +02004503#ifdef FEAT_TIMERS
4504 abort = abort || set_ref_in_timer(copyID);
4505#endif
4506
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004507#ifdef FEAT_QUICKFIX
4508 abort = abort || set_ref_in_quickfix(copyID);
4509#endif
4510
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004511#ifdef FEAT_TERMINAL
4512 abort = abort || set_ref_in_term(copyID);
4513#endif
4514
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004515#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004516 abort = abort || set_ref_in_popups(copyID);
4517#endif
4518
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004520 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 /*
4522 * 2. Free lists and dictionaries that are not referenced.
4523 */
4524 did_free = free_unref_items(copyID);
4525
4526 /*
4527 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004531 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 else if (p_verbose > 0)
4533 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004534 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004536
4537 return did_free;
4538}
4539
4540/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004541 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004542 */
4543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004544free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004545{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004546 int did_free = FALSE;
4547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // Let all "free" functions know that we are here. This means no
4549 // dictionaries, lists, channels or jobs are to be freed, because we will
4550 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004551 in_free_unref_items = TRUE;
4552
4553 /*
4554 * PASS 1: free the contents of the items. We don't free the items
4555 * themselves yet, so that it is possible to decrement refcount counters
4556 */
4557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004558 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004559 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004562 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004563
4564#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // Go through the list of jobs and free items without the copyID. This
4566 // must happen before doing channels, because jobs refer to channels, but
4567 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004568 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004570 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4572#endif
4573
4574 /*
4575 * PASS 2: free the items themselves.
4576 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004577 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004578 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004579
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004580#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of jobs and free items without the copyID. This
4582 // must happen before doing channels, because jobs refer to channels, but
4583 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584 free_unused_jobs(copyID, COPYID_MASK);
4585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004587 free_unused_channels(copyID, COPYID_MASK);
4588#endif
4589
4590 in_free_unref_items = FALSE;
4591
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004592 return did_free;
4593}
4594
4595/*
4596 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004597 * "list_stack" is used to add lists to be marked. Can be NULL.
4598 *
4599 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004602set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004603{
4604 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004606 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004607 hashtab_T *cur_ht;
4608 ht_stack_T *ht_stack = NULL;
4609 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004610
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004611 cur_ht = ht;
4612 for (;;)
4613 {
4614 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004616 // Mark each item in the hashtab. If the item contains a hashtab
4617 // it is added to ht_stack, if it contains a list it is added to
4618 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 todo = (int)cur_ht->ht_used;
4620 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4621 if (!HASHITEM_EMPTY(hi))
4622 {
4623 --todo;
4624 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4625 &ht_stack, list_stack);
4626 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628
4629 if (ht_stack == NULL)
4630 break;
4631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 cur_ht = ht_stack->ht;
4634 tempitem = ht_stack;
4635 ht_stack = ht_stack->prev;
4636 free(tempitem);
4637 }
4638
4639 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640}
4641
4642/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004643 * Mark a dict and its items with "copyID".
4644 * Returns TRUE if setting references failed somehow.
4645 */
4646 int
4647set_ref_in_dict(dict_T *d, int copyID)
4648{
4649 if (d != NULL && d->dv_copyID != copyID)
4650 {
4651 d->dv_copyID = copyID;
4652 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4653 }
4654 return FALSE;
4655}
4656
4657/*
4658 * Mark a list and its items with "copyID".
4659 * Returns TRUE if setting references failed somehow.
4660 */
4661 int
4662set_ref_in_list(list_T *ll, int copyID)
4663{
4664 if (ll != NULL && ll->lv_copyID != copyID)
4665 {
4666 ll->lv_copyID = copyID;
4667 return set_ref_in_list_items(ll, copyID, NULL);
4668 }
4669 return FALSE;
4670}
4671
4672/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004673 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004674 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4675 *
4676 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004677 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004678 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004679set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 listitem_T *li;
4682 int abort = FALSE;
4683 list_T *cur_l;
4684 list_stack_T *list_stack = NULL;
4685 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004686
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004687 cur_l = l;
4688 for (;;)
4689 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004690 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // Mark each item in the list. If the item contains a hashtab
4692 // it is added to ht_stack, if it contains a list it is added to
4693 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4695 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4696 ht_stack, &list_stack);
4697 if (list_stack == NULL)
4698 break;
4699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 cur_l = list_stack->list;
4702 tempitem = list_stack;
4703 list_stack = list_stack->prev;
4704 free(tempitem);
4705 }
4706
4707 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004708}
4709
4710/*
4711 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 * "list_stack" is used to add lists to be marked. Can be NULL.
4713 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4714 *
4715 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004718set_ref_in_item(
4719 typval_T *tv,
4720 int copyID,
4721 ht_stack_T **ht_stack,
4722 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004723{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004724 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004725
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004727 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 dict_T *dd = tv->vval.v_dict;
4729
Bram Moolenaara03f2332016-02-06 18:09:59 +01004730 if (dd != NULL && dd->dv_copyID != copyID)
4731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004733 dd->dv_copyID = copyID;
4734 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004735 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004736 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4737 }
4738 else
4739 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004740 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4741
Bram Moolenaara03f2332016-02-06 18:09:59 +01004742 if (newitem == NULL)
4743 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004744 else
4745 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004746 newitem->ht = &dd->dv_hashtab;
4747 newitem->prev = *ht_stack;
4748 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004749 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004750 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004751 }
4752 }
4753 else if (tv->v_type == VAR_LIST)
4754 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004755 list_T *ll = tv->vval.v_list;
4756
Bram Moolenaara03f2332016-02-06 18:09:59 +01004757 if (ll != NULL && ll->lv_copyID != copyID)
4758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004760 ll->lv_copyID = copyID;
4761 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004762 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004763 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004764 }
4765 else
4766 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004767 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4768
Bram Moolenaara03f2332016-02-06 18:09:59 +01004769 if (newitem == NULL)
4770 abort = TRUE;
4771 else
4772 {
4773 newitem->list = ll;
4774 newitem->prev = *list_stack;
4775 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004776 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004777 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004779 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004780 else if (tv->v_type == VAR_FUNC)
4781 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004782 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004783 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004784 else if (tv->v_type == VAR_PARTIAL)
4785 {
4786 partial_T *pt = tv->vval.v_partial;
4787 int i;
4788
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004789 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004791 // Didn't see this partial yet.
4792 pt->pt_copyID = copyID;
4793
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004794 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004795
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004796 if (pt->pt_dict != NULL)
4797 {
4798 typval_T dtv;
4799
4800 dtv.v_type = VAR_DICT;
4801 dtv.vval.v_dict = pt->pt_dict;
4802 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4803 }
4804
4805 for (i = 0; i < pt->pt_argc; ++i)
4806 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4807 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004808 if (pt->pt_funcstack != NULL)
4809 {
4810 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4811
4812 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4813 abort = abort || set_ref_in_item(stack + i, copyID,
4814 ht_stack, list_stack);
4815 }
4816
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 }
4818 }
4819#ifdef FEAT_JOB_CHANNEL
4820 else if (tv->v_type == VAR_JOB)
4821 {
4822 job_T *job = tv->vval.v_job;
4823 typval_T dtv;
4824
4825 if (job != NULL && job->jv_copyID != copyID)
4826 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004827 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 if (job->jv_channel != NULL)
4829 {
4830 dtv.v_type = VAR_CHANNEL;
4831 dtv.vval.v_channel = job->jv_channel;
4832 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4833 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004834 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835 {
4836 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004837 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4839 }
4840 }
4841 }
4842 else if (tv->v_type == VAR_CHANNEL)
4843 {
4844 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004845 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004846 typval_T dtv;
4847 jsonq_T *jq;
4848 cbq_T *cq;
4849
4850 if (ch != NULL && ch->ch_copyID != copyID)
4851 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004852 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004853 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 {
4855 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4856 jq = jq->jq_next)
4857 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4858 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4859 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004860 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004861 {
4862 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004863 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4865 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004866 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004867 {
4868 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004869 dtv.vval.v_partial =
4870 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004871 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4872 }
4873 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 {
4876 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4879 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 {
4882 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004883 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4885 }
4886 }
4887 }
4888#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004889 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004890}
4891
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 * Return a string with the string representation of a variable.
4894 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004895 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004896 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004897 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004898 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004899 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004900 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4901 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004902 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004904 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004905echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004906 typval_T *tv,
4907 char_u **tofree,
4908 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004909 int copyID,
4910 int echo_style,
4911 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004912 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004914 static int recurse = 0;
4915 char_u *r = NULL;
4916
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004918 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004919 if (!did_echo_string_emsg)
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Only give this message once for a recursive call to avoid
4922 // flooding the user with errors. And stop iterating over lists
4923 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004924 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004925 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004927 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004928 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 }
4930 ++recurse;
4931
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004932 switch (tv->v_type)
4933 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004934 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004935 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004936 {
4937 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004938 r = tv->vval.v_string;
4939 if (r == NULL)
4940 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004941 }
4942 else
4943 {
4944 *tofree = string_quote(tv->vval.v_string, FALSE);
4945 r = *tofree;
4946 }
4947 break;
4948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004949 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004950 if (echo_style)
4951 {
4952 *tofree = NULL;
4953 r = tv->vval.v_string;
4954 }
4955 else
4956 {
4957 *tofree = string_quote(tv->vval.v_string, TRUE);
4958 r = *tofree;
4959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004960 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004961
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004962 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004963 {
4964 partial_T *pt = tv->vval.v_partial;
4965 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004966 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004967 garray_T ga;
4968 int i;
4969 char_u *tf;
4970
4971 ga_init2(&ga, 1, 100);
4972 ga_concat(&ga, (char_u *)"function(");
4973 if (fname != NULL)
4974 {
4975 ga_concat(&ga, fname);
4976 vim_free(fname);
4977 }
4978 if (pt != NULL && pt->pt_argc > 0)
4979 {
4980 ga_concat(&ga, (char_u *)", [");
4981 for (i = 0; i < pt->pt_argc; ++i)
4982 {
4983 if (i > 0)
4984 ga_concat(&ga, (char_u *)", ");
4985 ga_concat(&ga,
4986 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4987 vim_free(tf);
4988 }
4989 ga_concat(&ga, (char_u *)"]");
4990 }
4991 if (pt != NULL && pt->pt_dict != NULL)
4992 {
4993 typval_T dtv;
4994
4995 ga_concat(&ga, (char_u *)", ");
4996 dtv.v_type = VAR_DICT;
4997 dtv.vval.v_dict = pt->pt_dict;
4998 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4999 vim_free(tf);
5000 }
5001 ga_concat(&ga, (char_u *)")");
5002
5003 *tofree = ga.ga_data;
5004 r = *tofree;
5005 break;
5006 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005007
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005008 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005009 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005010 break;
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005013 if (tv->vval.v_list == NULL)
5014 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005015 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005016 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005017 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005018 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005019 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5020 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005021 {
5022 *tofree = NULL;
5023 r = (char_u *)"[...]";
5024 }
5025 else
5026 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005027 int old_copyID = tv->vval.v_list->lv_copyID;
5028
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005029 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005030 *tofree = list2string(tv, copyID, restore_copyID);
5031 if (restore_copyID)
5032 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005033 r = *tofree;
5034 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005035 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005036
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038 if (tv->vval.v_dict == NULL)
5039 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005040 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005042 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005043 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005044 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5045 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005046 {
5047 *tofree = NULL;
5048 r = (char_u *)"{...}";
5049 }
5050 else
5051 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005052 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005053
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005054 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005055 *tofree = dict2string(tv, copyID, restore_copyID);
5056 if (restore_copyID)
5057 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 r = *tofree;
5059 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005061
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005063 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005064 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005066 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005067 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005068 break;
5069
Bram Moolenaar835dc632016-02-07 14:27:38 +01005070 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005071 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005072 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005074 if (composite_val)
5075 {
5076 *tofree = string_quote(r, FALSE);
5077 r = *tofree;
5078 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005080
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005081 case VAR_INSTR:
5082 *tofree = NULL;
5083 r = (char_u *)"instructions";
5084 break;
5085
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005086 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005087#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005088 *tofree = NULL;
5089 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5090 r = numbuf;
5091 break;
5092#endif
5093
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005094 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005095 case VAR_SPECIAL:
5096 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005097 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005098 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100
Bram Moolenaar8502c702014-06-17 12:51:16 +02005101 if (--recurse == 0)
5102 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104}
5105
5106/*
5107 * Return a string with the string representation of a variable.
5108 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5109 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005110 * Does not put quotes around strings, as ":echo" displays values.
5111 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5112 * May return NULL.
5113 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005115echo_string(
5116 typval_T *tv,
5117 char_u **tofree,
5118 char_u *numbuf,
5119 int copyID)
5120{
5121 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5122}
5123
5124/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005125 * Return string "str" in ' quotes, doubling ' characters.
5126 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005129 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005130string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131{
Bram Moolenaar33570922005-01-25 22:26:29 +00005132 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 char_u *p, *r, *s;
5134
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 len = (function ? 13 : 3);
5136 if (str != NULL)
5137 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005138 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005139 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 if (*p == '\'')
5141 ++len;
5142 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005143 s = r = alloc(len);
5144 if (r != NULL)
5145 {
5146 if (function)
5147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 r += 10;
5150 }
5151 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005153 if (str != NULL)
5154 for (p = str; *p != NUL; )
5155 {
5156 if (*p == '\'')
5157 *r++ = '\'';
5158 MB_COPY_CHAR(p, r);
5159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005161 if (function)
5162 *r++ = ')';
5163 *r++ = NUL;
5164 }
5165 return s;
5166}
5167
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005168#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169/*
5170 * Convert the string "text" to a floating point number.
5171 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5172 * this always uses a decimal point.
5173 * Returns the length of the text that was consumed.
5174 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005175 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005176string2float(
5177 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179{
5180 char *s = (char *)text;
5181 float_T f;
5182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005184 if (STRNICMP(text, "inf", 3) == 0)
5185 {
5186 *value = INFINITY;
5187 return 3;
5188 }
5189 if (STRNICMP(text, "-inf", 3) == 0)
5190 {
5191 *value = -INFINITY;
5192 return 4;
5193 }
5194 if (STRNICMP(text, "nan", 3) == 0)
5195 {
5196 *value = NAN;
5197 return 3;
5198 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199 f = strtod(s, &s);
5200 *value = f;
5201 return (int)((char_u *)s - text);
5202}
5203#endif
5204
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005205/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005206 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5207 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005208 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005209 */
5210 int
5211buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5212{
5213 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005214 char_u *t;
5215 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005216
5217 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5218 return -1;
5219
5220 if (lnum > buf->b_ml.ml_line_count)
5221 lnum = buf->b_ml.ml_line_count;
5222
5223 str = ml_get_buf(buf, lnum, FALSE);
5224 if (str == NULL)
5225 return -1;
5226
5227 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005228 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005229
Bram Moolenaar91458462021-01-13 20:08:38 +01005230 // count the number of characters
5231 t = str;
5232 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5233 t += mb_ptr2len(t);
5234
5235 // In insert mode, when the cursor is at the end of a non-empty line,
5236 // byteidx points to the NUL character immediately past the end of the
5237 // string. In this case, add one to the character count.
5238 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5239 count++;
5240
5241 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005242}
5243
5244/*
5245 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005246 * byte index. Works only for loaded buffers. Returns -1 on failure.
5247 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005248 */
5249 int
5250buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5251{
5252 char_u *str;
5253 char_u *t;
5254
5255 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5256 return -1;
5257
5258 if (lnum > buf->b_ml.ml_line_count)
5259 lnum = buf->b_ml.ml_line_count;
5260
5261 str = ml_get_buf(buf, lnum, FALSE);
5262 if (str == NULL)
5263 return -1;
5264
5265 // Convert the character offset to a byte offset
5266 t = str;
5267 while (*t != NUL && --charidx > 0)
5268 t += mb_ptr2len(t);
5269
Bram Moolenaar91458462021-01-13 20:08:38 +01005270 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005271}
5272
5273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005275 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005278var2fpos(
5279 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005281 int *fnum, // set to fnum for '0, 'A, etc.
5282 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005284 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005286 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005289 if (varp->v_type == VAR_LIST)
5290 {
5291 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005292 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005293 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005294 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005295
5296 l = varp->vval.v_list;
5297 if (l == NULL)
5298 return NULL;
5299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005300 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005301 pos.lnum = list_find_nr(l, 0L, &error);
5302 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005304 if (charcol)
5305 len = (long)mb_charlen(ml_get(pos.lnum));
5306 else
5307 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005308
Bram Moolenaarec65d772020-08-20 22:29:12 +02005309 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005311 li = list_find(l, 1L);
5312 if (li != NULL && li->li_tv.v_type == VAR_STRING
5313 && li->li_tv.vval.v_string != NULL
5314 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005315 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005316 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005317 }
5318 else
5319 {
5320 pos.col = list_find_nr(l, 1L, &error);
5321 if (error)
5322 return NULL;
5323 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005326 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005328 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005331 pos.coladd = list_find_nr(l, 2L, &error);
5332 if (error)
5333 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005334
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005335 return &pos;
5336 }
5337
Bram Moolenaarc5809432021-03-27 21:23:30 +01005338 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5339 return NULL;
5340
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005341 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 if (name == NULL)
5343 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005345 {
5346 pos = curwin->w_cursor;
5347 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005348 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005349 return &pos;
5350 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005352 {
5353 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005354 pos = VIsual;
5355 else
5356 pos = curwin->w_cursor;
5357 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005358 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005359 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005360 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005363 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5365 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005366 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005367 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 return pp;
5369 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005370
Bram Moolenaara5525202006-03-02 22:52:09 +00005371 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005372
Bram Moolenaar477933c2007-07-17 14:32:23 +00005373 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005374 {
5375 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005376 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005377 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005378 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 // In silent Ex mode topline is zero, but that's not a valid line
5380 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005381 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005382 return &pos;
5383 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005388 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005389 return &pos;
5390 }
5391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005394 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 pos.lnum = curbuf->b_ml.ml_line_count;
5397 pos.col = 0;
5398 }
5399 else
5400 {
5401 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005402 if (charcol)
5403 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5404 else
5405 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407 return &pos;
5408 }
5409 return NULL;
5410}
5411
5412/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005413 * Convert list in "arg" into a position and optional file number.
5414 * When "fnump" is NULL there is no file number, only 3 items.
5415 * Note that the column is passed on as-is, the caller may want to decrement
5416 * it to use 1 for the first column.
5417 * Return FAIL when conversion is not possible, doesn't check the position for
5418 * validity.
5419 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005421list2fpos(
5422 typval_T *arg,
5423 pos_T *posp,
5424 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005425 colnr_T *curswantp,
5426 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005427{
5428 list_T *l = arg->vval.v_list;
5429 long i = 0;
5430 long n;
5431
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5433 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005434 if (arg->v_type != VAR_LIST
5435 || l == NULL
5436 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005437 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005438 return FAIL;
5439
5440 if (fnump != NULL)
5441 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005442 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005443 if (n < 0)
5444 return FAIL;
5445 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005447 *fnump = n;
5448 }
5449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451 if (n < 0)
5452 return FAIL;
5453 posp->lnum = n;
5454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005455 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005456 if (n < 0)
5457 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005458 // If character position is specified, then convert to byte position
5459 if (charcol)
5460 {
5461 buf_T *buf;
5462
5463 // Get the text for the specified line in a loaded buffer
5464 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5465 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5466 return FAIL;
5467
Bram Moolenaar91458462021-01-13 20:08:38 +01005468 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005469 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005470 posp->col = n;
5471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005473 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005474 posp->coladd = 0;
5475 else
5476 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005477
Bram Moolenaar493c1782014-05-28 14:34:46 +02005478 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005479 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005480
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005481 return OK;
5482}
5483
5484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 * Get the length of an environment variable name.
5486 * Advance "arg" to the first character after the name.
5487 * Return 0 for error.
5488 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 char_u *p;
5493 int len;
5494
5495 for (p = *arg; vim_isIDc(*p); ++p)
5496 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005497 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return 0;
5499
5500 len = (int)(p - *arg);
5501 *arg = p;
5502 return len;
5503}
5504
5505/*
5506 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005507 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * Return 0 if something is wrong.
5509 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 char_u *p;
5514 int len;
5515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005518 {
5519 if (*p == ':')
5520 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005521 // "s:" is start of "s:var", but "n:" is not and can be used in
5522 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005523 len = (int)(p - *arg);
5524 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5525 || len > 1)
5526 break;
5527 }
5528 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 return 0;
5531
5532 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005533 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 return len;
5536}
5537
5538/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005539 * Get the length of the name of a variable or function.
5540 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005542 * Return -1 if curly braces expansion failed.
5543 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 * If the name contains 'magic' {}'s, expand them and return the
5545 * expanded name in an allocated string via 'alias' - caller must free.
5546 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005547 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005548get_name_len(
5549 char_u **arg,
5550 char_u **alias,
5551 int evaluate,
5552 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 char_u *p;
5556 char_u *expr_start;
5557 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5562 && (*arg)[2] == (int)KE_SNR)
5563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 *arg += 3;
5566 return get_id_len(arg) + 3;
5567 }
5568 len = eval_fname_script(*arg);
5569 if (len > 0)
5570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 *arg += len;
5573 }
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005578 p = find_name_end(*arg, &expr_start, &expr_end,
5579 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (expr_start != NULL)
5581 {
5582 char_u *temp_string;
5583
5584 if (!evaluate)
5585 {
5586 len += (int)(p - *arg);
5587 *arg = skipwhite(p);
5588 return len;
5589 }
5590
5591 /*
5592 * Include any <SID> etc in the expanded string:
5593 * Thus the -len here.
5594 */
5595 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5596 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005597 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 *alias = temp_string;
5599 *arg = skipwhite(p);
5600 return (int)STRLEN(temp_string);
5601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
5603 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005604 // Only give an error when there is something, otherwise it will be
5605 // reported at a higher level.
5606 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005607 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 return len;
5610}
5611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612/*
5613 * Find the end of a variable or function name, taking care of magic braces.
5614 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5615 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005616 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 * Return a pointer to just after the name. Equal to "arg" if there is no
5618 * valid name.
5619 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005621find_name_end(
5622 char_u *arg,
5623 char_u **expr_start,
5624 char_u **expr_end,
5625 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 int mb_nest = 0;
5628 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005630 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005631 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 *expr_start = NULL;
5636 *expr_end = NULL;
5637 }
5638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005640 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5641 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005642 return arg;
5643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 for (p = arg; *p != NUL
5645 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005646 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005647 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005648 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005650 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005652 if (*p == '\'')
5653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005655 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005656 ;
5657 if (*p == NUL)
5658 break;
5659 }
5660 else if (*p == '"')
5661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005663 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005664 if (*p == '\\' && p[1] != NUL)
5665 ++p;
5666 if (*p == NUL)
5667 break;
5668 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005669 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005671 // "s:" is start of "s:var", but "n:" is not and can be used in
5672 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005673 len = (int)(p - arg);
5674 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005675 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005676 break;
5677 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 if (*p == '[')
5682 ++br_nest;
5683 else if (*p == ']')
5684 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005686
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005687 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 if (*p == '{')
5690 {
5691 mb_nest++;
5692 if (expr_start != NULL && *expr_start == NULL)
5693 *expr_start = p;
5694 }
5695 else if (*p == '}')
5696 {
5697 mb_nest--;
5698 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5699 *expr_end = p;
5700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703
5704 return p;
5705}
5706
5707/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005708 * Expands out the 'magic' {}'s in a variable/function name.
5709 * Note that this can call itself recursively, to deal with
5710 * constructs like foo{bar}{baz}{bam}
5711 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5712 * "in_start" ^
5713 * "expr_start" ^
5714 * "expr_end" ^
5715 * "in_end" ^
5716 *
5717 * Returns a new allocated string, which the caller must free.
5718 * Returns NULL for failure.
5719 */
5720 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005721make_expanded_name(
5722 char_u *in_start,
5723 char_u *expr_start,
5724 char_u *expr_end,
5725 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726{
5727 char_u c1;
5728 char_u *retval = NULL;
5729 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730
5731 if (expr_end == NULL || in_end == NULL)
5732 return NULL;
5733 *expr_start = NUL;
5734 *expr_end = NUL;
5735 c1 = *in_end;
5736 *in_end = NUL;
5737
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005738 temp_result = eval_to_string(expr_start + 1, FALSE);
5739 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005741 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5742 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743 if (retval != NULL)
5744 {
5745 STRCPY(retval, in_start);
5746 STRCAT(retval, temp_result);
5747 STRCAT(retval, expr_end + 1);
5748 }
5749 }
5750 vim_free(temp_result);
5751
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005752 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005753 *expr_start = '{';
5754 *expr_end = '}';
5755
5756 if (retval != NULL)
5757 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005758 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005759 if (expr_start != NULL)
5760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005761 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005762 temp_result = make_expanded_name(retval, expr_start,
5763 expr_end, temp_result);
5764 vim_free(retval);
5765 retval = temp_result;
5766 }
5767 }
5768
5769 return retval;
5770}
5771
5772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005774 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005779 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005780}
5781
5782/*
5783 * Return TRUE if character "c" can be used as the first character in a
5784 * variable or function name (excluding '{' and '}').
5785 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005787eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005788{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005789 return ASCII_ISALPHA(c) || c == '_';
5790}
5791
5792/*
5793 * Return TRUE if character "c" can be used as the first character of a
5794 * dictionary key.
5795 */
5796 int
5797eval_isdictc(int c)
5798{
5799 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800}
5801
5802/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005803 * Handle:
5804 * - expr[expr], expr[expr:expr] subscript
5805 * - ".name" lookup
5806 * - function call with Funcref variable: func(expr)
5807 * - method call: var->method()
5808 *
5809 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005810 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005812handle_subscript(
5813 char_u **arg,
5814 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005815 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005816 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005817{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005818 int evaluate = evalarg != NULL
5819 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005820 int ret = OK;
5821 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005822 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005823 int getnext;
5824 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005825
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005826 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005827 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005828 // When at the end of the line and ".name" or "->{" or "->X" follows in
5829 // the next line then consume the line break.
5830 p = eval_next_non_blank(*arg, evalarg, &getnext);
5831 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005832 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005833 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005834 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005835 {
5836 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005837 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005838 check_white = FALSE;
5839 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005840
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005841 if (rettv->v_type == VAR_ANY)
5842 {
5843 char_u *exp_name;
5844 int cc;
5845 int idx;
5846 ufunc_T *ufunc;
5847 type_T *type;
5848
5849 // Found script from "import * as {name}", script item name must
5850 // follow.
5851 if (**arg != '.')
5852 {
5853 if (verbose)
5854 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5855 ret = FAIL;
5856 break;
5857 }
5858 ++*arg;
5859 if (IS_WHITE_OR_NUL(**arg))
5860 {
5861 if (verbose)
5862 emsg(_(e_no_white_space_allowed_after_dot));
5863 ret = FAIL;
5864 break;
5865 }
5866
5867 // isolate the name
5868 exp_name = *arg;
5869 while (eval_isnamec(**arg))
5870 ++*arg;
5871 cc = **arg;
5872 **arg = NUL;
5873
5874 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5875 evalarg->eval_cctx, verbose);
5876 **arg = cc;
5877 *arg = skipwhite(*arg);
5878
5879 if (idx < 0 && ufunc == NULL)
5880 {
5881 ret = FAIL;
5882 break;
5883 }
5884 if (idx >= 0)
5885 {
5886 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5887 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5888
5889 copy_tv(sv->sv_tv, rettv);
5890 }
5891 else
5892 {
5893 rettv->v_type = VAR_FUNC;
5894 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5895 }
5896 }
5897
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005898 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5899 || rettv->v_type == VAR_PARTIAL))
5900 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005901 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005902 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5903 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005904
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005905 // Stop the expression evaluation when immediately aborting on
5906 // error, or when an interrupt occurred or an exception was thrown
5907 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005908 if (aborting())
5909 {
5910 if (ret == OK)
5911 clear_tv(rettv);
5912 ret = FAIL;
5913 }
5914 dict_unref(selfdict);
5915 selfdict = NULL;
5916 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005917 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005918 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005919 if (in_vim9script())
5920 *arg = skipwhite(p + 2);
5921 else
5922 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005923 if (ret == OK)
5924 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005925 if (VIM_ISWHITE(**arg))
5926 {
5927 emsg(_(e_nowhitespace));
5928 ret = FAIL;
5929 }
5930 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005931 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005932 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005933 else
5934 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005935 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005936 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005937 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005938 // "." is ".name" lookup when we found a dict or when evaluating and
5939 // scriptversion is at least 2, where string concatenation is "..".
5940 else if (**arg == '['
5941 || (**arg == '.' && (rettv->v_type == VAR_DICT
5942 || (!evaluate
5943 && (*arg)[1] != '.'
5944 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005945 {
5946 dict_unref(selfdict);
5947 if (rettv->v_type == VAR_DICT)
5948 {
5949 selfdict = rettv->vval.v_dict;
5950 if (selfdict != NULL)
5951 ++selfdict->dv_refcount;
5952 }
5953 else
5954 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005955 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005956 {
5957 clear_tv(rettv);
5958 ret = FAIL;
5959 }
5960 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005961 else
5962 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005963 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005965 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5966 // Don't do this when "Func" is already a partial that was bound
5967 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005968 if (selfdict != NULL
5969 && (rettv->v_type == VAR_FUNC
5970 || (rettv->v_type == VAR_PARTIAL
5971 && (rettv->vval.v_partial->pt_auto
5972 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005973 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005974
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005975 dict_unref(selfdict);
5976 return ret;
5977}
5978
5979/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005980 * Make a copy of an item.
5981 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005982 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5983 * reference to an already copied list/dict can be used.
5984 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987item_copy(
5988 typval_T *from,
5989 typval_T *to,
5990 int deep,
5991 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005992{
5993 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005994 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005998 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000 }
6001 ++recurse;
6002
6003 switch (from->v_type)
6004 {
6005 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006006 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006007 case VAR_STRING:
6008 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006009 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006010 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006011 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006012 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006013 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006014 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006015 copy_tv(from, to);
6016 break;
6017 case VAR_LIST:
6018 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006019 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006020 if (from->vval.v_list == NULL)
6021 to->vval.v_list = NULL;
6022 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006024 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006025 to->vval.v_list = from->vval.v_list->lv_copylist;
6026 ++to->vval.v_list->lv_refcount;
6027 }
6028 else
6029 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6030 if (to->vval.v_list == NULL)
6031 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006032 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006033 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006035 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036 case VAR_DICT:
6037 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006038 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006039 if (from->vval.v_dict == NULL)
6040 to->vval.v_dict = NULL;
6041 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006043 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6045 ++to->vval.v_dict->dv_refcount;
6046 }
6047 else
6048 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6049 if (to->vval.v_dict == NULL)
6050 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006052 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006053 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006055 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006057 }
6058 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006059 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060}
6061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 void
6063echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6064{
6065 char_u *tofree;
6066 char_u numbuf[NUMBUFLEN];
6067 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6068
6069 if (*atstart)
6070 {
6071 *atstart = FALSE;
6072 // Call msg_start() after eval1(), evaluating the expression
6073 // may cause a message to appear.
6074 if (with_space)
6075 {
6076 // Mark the saved text as finishing the line, so that what
6077 // follows is displayed on a new line when scrolling back
6078 // at the more prompt.
6079 msg_sb_eol();
6080 msg_start();
6081 }
6082 }
6083 else if (with_space)
6084 msg_puts_attr(" ", echo_attr);
6085
6086 if (p != NULL)
6087 for ( ; *p != NUL && !got_int; ++p)
6088 {
6089 if (*p == '\n' || *p == '\r' || *p == TAB)
6090 {
6091 if (*p != TAB && *needclr)
6092 {
6093 // remove any text still there from the command
6094 msg_clr_eos();
6095 *needclr = FALSE;
6096 }
6097 msg_putchar_attr(*p, echo_attr);
6098 }
6099 else
6100 {
6101 if (has_mbyte)
6102 {
6103 int i = (*mb_ptr2len)(p);
6104
6105 (void)msg_outtrans_len_attr(p, i, echo_attr);
6106 p += i - 1;
6107 }
6108 else
6109 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6110 }
6111 }
6112 vim_free(tofree);
6113}
6114
Bram Moolenaare9a41262005-01-15 22:18:47 +00006115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * ":echo expr1 ..." print each argument separated with a space, add a
6117 * newline at the end.
6118 * ":echon expr1 ..." print each argument plain.
6119 */
6120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006125 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int needclr = TRUE;
6127 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006128 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006129 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006130 evalarg_T evalarg;
6131
Bram Moolenaare707c882020-07-01 13:07:37 +02006132 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 if (eap->skip)
6135 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006136 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006138 // If eval1() causes an error message the text from the command may
6139 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006140 need_clr_eos = needclr;
6141
Bram Moolenaar68db9962021-05-09 23:19:22 +02006142 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006143 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 {
6145 /*
6146 * Report the invalid expression unless the expression evaluation
6147 * has been cancelled due to an aborting error, an interrupt, or an
6148 * exception.
6149 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006150 if (!aborting() && did_emsg == did_emsg_before
6151 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006152 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006153 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 break;
6155 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006156 need_clr_eos = FALSE;
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006159 {
6160 if (rettv.v_type == VAR_VOID)
6161 {
6162 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6163 break;
6164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006166 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006168 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 arg = skipwhite(arg);
6170 }
6171 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006172 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 if (eap->skip)
6175 --emsg_skip;
6176 else
6177 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006178 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (needclr)
6180 msg_clr_eos();
6181 if (eap->cmdidx == CMD_echo)
6182 msg_end();
6183 }
6184}
6185
6186/*
6187 * ":echohl {name}".
6188 */
6189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006192 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193}
6194
6195/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006196 * Returns the :echo attribute
6197 */
6198 int
6199get_echo_attr(void)
6200{
6201 return echo_attr;
6202}
6203
6204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 * ":execute expr1 ..." execute the result of an expression.
6206 * ":echomsg expr1 ..." Print a message
6207 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006208 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 * Each gets spaces around each argument and a newline at the end for
6210 * echo commands
6211 */
6212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 int ret = OK;
6218 char_u *p;
6219 garray_T ga;
6220 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
6222 ga_init2(&ga, 1, 80);
6223
6224 if (eap->skip)
6225 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006226 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006228 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006229 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 if (!eap->skip)
6233 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006234 char_u buf[NUMBUFLEN];
6235
6236 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006237 {
6238 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6239 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006240 semsg(_(e_using_invalid_value_as_string_str),
6241 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006242 p = NULL;
6243 }
6244 else
6245 p = tv_get_string_buf(&rettv, buf);
6246 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006247 else
6248 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006249 if (p == NULL)
6250 {
6251 clear_tv(&rettv);
6252 ret = FAIL;
6253 break;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 len = (int)STRLEN(p);
6256 if (ga_grow(&ga, len + 2) == FAIL)
6257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006258 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 ret = FAIL;
6260 break;
6261 }
6262 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 ga.ga_len += len;
6266 }
6267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006268 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 arg = skipwhite(arg);
6270 }
6271
6272 if (ret != FAIL && ga.ga_data != NULL)
6273 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006274 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006276 // Mark the already saved text as finishing the line, so that what
6277 // follows is displayed on a new line when scrolling back at the
6278 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006279 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006280 }
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006283 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006284 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006285 out_flush();
6286 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006287 else if (eap->cmdidx == CMD_echoconsole)
6288 {
6289 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6290 ui_write((char_u *)"\r\n", 2, TRUE);
6291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 else if (eap->cmdidx == CMD_echoerr)
6293 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006294 int save_did_emsg = did_emsg;
6295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006296 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006297 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (!force_abort)
6299 did_emsg = save_did_emsg;
6300 }
6301 else if (eap->cmdidx == CMD_execute)
6302 do_cmdline((char_u *)ga.ga_data,
6303 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6304 }
6305
6306 ga_clear(&ga);
6307
6308 if (eap->skip)
6309 --emsg_skip;
6310
6311 eap->nextcmd = check_nextcmd(arg);
6312}
6313
6314/*
6315 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6316 * "arg" points to the "&" or '+' when called, to "option" when returning.
6317 * Returns NULL when no option name found. Otherwise pointer to the char
6318 * after the option name.
6319 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006320 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006321find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 char_u *p = *arg;
6324
6325 ++p;
6326 if (*p == 'g' && p[1] == ':')
6327 {
6328 *opt_flags = OPT_GLOBAL;
6329 p += 2;
6330 }
6331 else if (*p == 'l' && p[1] == ':')
6332 {
6333 *opt_flags = OPT_LOCAL;
6334 p += 2;
6335 }
6336 else
6337 *opt_flags = 0;
6338
6339 if (!ASCII_ISALPHA(*p))
6340 return NULL;
6341 *arg = p;
6342
6343 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006344 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 else
6346 while (ASCII_ISALPHA(*p))
6347 ++p;
6348 return p;
6349}
6350
6351/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006352 * Display script name where an item was last set.
6353 * Should only be invoked when 'verbose' is non-zero.
6354 */
6355 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006356last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006357{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006358 char_u *p;
6359
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006360 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006361 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006362 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006363 if (p != NULL)
6364 {
6365 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006366 msg_puts(_("\n\tLast set from "));
6367 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006368 if (script_ctx.sc_lnum > 0)
6369 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006370 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006371 msg_outnum((long)script_ctx.sc_lnum);
6372 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006373 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006374 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006375 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006376 }
6377}
6378
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006379#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381/*
6382 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006383 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 * "flags" can be "g" to do a global substitute.
6385 * Returns an allocated string, NULL for error.
6386 */
6387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388do_string_sub(
6389 char_u *str,
6390 char_u *pat,
6391 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006392 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int sublen;
6396 regmatch_T regmatch;
6397 int i;
6398 int do_all;
6399 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006400 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 garray_T ga;
6402 char_u *ret;
6403 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006404 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006406 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006408 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 ga_init2(&ga, 1, 200);
6411
6412 do_all = (flags[0] == 'g');
6413
6414 regmatch.rm_ic = p_ic;
6415 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6416 if (regmatch.regprog != NULL)
6417 {
6418 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006419 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006422 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006423 if (regmatch.startp[0] == regmatch.endp[0])
6424 {
6425 if (zero_width == regmatch.startp[0])
6426 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006427 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006428 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006429 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6430 (size_t)i);
6431 ga.ga_len += i;
6432 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006433 continue;
6434 }
6435 zero_width = regmatch.startp[0];
6436 }
6437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 /*
6439 * Get some space for a temporary buffer to do the substitution
6440 * into. It will contain:
6441 * - The text up to where the match is.
6442 * - The substituted text.
6443 * - The text after the match.
6444 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006445 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006446 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6448 {
6449 ga_clear(&ga);
6450 break;
6451 }
6452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006453 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 i = (int)(regmatch.startp[0] - tail);
6455 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006457 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 + ga.ga_len + i, TRUE, TRUE, FALSE);
6459 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006460 tail = regmatch.endp[0];
6461 if (*tail == NUL)
6462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (!do_all)
6464 break;
6465 }
6466
6467 if (ga.ga_data != NULL)
6468 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6469
Bram Moolenaar473de612013-06-08 18:19:48 +02006470 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
6472
6473 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6474 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006475 if (p_cpo == empty_option)
6476 p_cpo = save_cpo;
6477 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006478 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006480 // If it's still empty it was changed and restored, need to restore in
6481 // the complicated way.
6482 if (*p_cpo == NUL)
6483 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006484 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 return ret;
6488}