blob: 1a84d2c1f5fc09ed932c04f655d70c5f2d4ef06c [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 Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000115
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200116#ifdef EBCDIC
117 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100118 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200119 */
120 sortFunctions();
121#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000122}
123
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124#if defined(EXITFREE) || defined(PROTO)
125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100126eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000127{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100129 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200130 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000131
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200132 // autoloaded script names
133 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000134
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200135 // unreferenced lists and dicts
136 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200137
138 // functions not garbage collected
139 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140}
141#endif
142
Bram Moolenaare6b53242020-07-01 17:28:33 +0200143 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200144fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
145{
146 CLEAR_FIELD(*evalarg);
147 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
148 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
149 {
150 evalarg->eval_getline = eap->getline;
151 evalarg->eval_cookie = eap->cookie;
152 }
153}
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200164 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200169 evalarg_T evalarg;
170
Bram Moolenaar37c83712020-06-30 21:18:36 +0200171 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173 if (skip)
174 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200175 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 else
178 {
179 *error = FALSE;
180 if (!skip)
181 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200182 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200183 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200184 else
185 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000186 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188 }
189 if (skip)
190 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200191 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200193 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194}
195
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196/*
197 * Call eval1() and give an error message if not done at a lower level.
198 */
199 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100202 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 int ret;
204 int did_emsg_before = did_emsg;
205 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200206 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200208 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
209
210 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211 if (ret == FAIL)
212 {
213 // Report the invalid expression unless the expression evaluation has
214 // been cancelled due to an aborting error, an interrupt, or an
215 // exception, or we already gave a more specific error.
216 // Also check called_emsg for when using assert_fails().
217 if (!aborting() && did_emsg == did_emsg_before
218 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200219 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200221 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 return ret;
223}
224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200226 * Return whether a typval is a valid expression to pass to eval_expr_typval()
227 * or eval_expr_to_bool(). An empty string returns FALSE;
228 */
229 int
230eval_expr_valid_arg(typval_T *tv)
231{
232 return tv->v_type != VAR_UNKNOWN
233 && (tv->v_type != VAR_STRING
234 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
235}
236
237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Evaluate an expression, which can be a function, partial or string.
239 * Pass arguments "argv[argc]".
240 * Return the result in "rettv" and OK or FAIL.
241 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200242 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100243eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
244{
245 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200247 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100248
249 if (expr->v_type == VAR_FUNC)
250 {
251 s = expr->vval.v_string;
252 if (s == NULL || *s == NUL)
253 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200254 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200255 funcexe.evaluate = TRUE;
256 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 return FAIL;
258 }
259 else if (expr->v_type == VAR_PARTIAL)
260 {
261 partial_T *partial = expr->vval.v_partial;
262
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200263 if (partial == NULL)
264 return FAIL;
265
Bram Moolenaar822ba242020-05-24 23:00:18 +0200266 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200267 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200269 if (call_def_function(partial->pt_func, argc, argv,
270 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 return FAIL;
272 }
273 else
274 {
275 s = partial_name(partial);
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 funcexe.evaluate = TRUE;
280 funcexe.partial = partial;
281 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
282 return FAIL;
283 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200285 else if (expr->v_type == VAR_INSTR)
286 {
287 return exe_typval_instr(expr, rettv);
288 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 else
290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100291 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 if (s == NULL)
293 return FAIL;
294 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200295 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200297 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200299 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200300 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 return FAIL;
302 }
303 }
304 return OK;
305}
306
307/*
308 * Like eval_to_bool() but using a typval_T instead of a string.
309 * Works for string, funcref and partial.
310 */
311 int
312eval_expr_to_bool(typval_T *expr, int *error)
313{
314 typval_T rettv;
315 int res;
316
317 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
318 {
319 *error = TRUE;
320 return FALSE;
321 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200322 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 clear_tv(&rettv);
324 return res;
325}
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327/*
328 * Top level evaluation function, returning a string. If "skip" is TRUE,
329 * only parsing to "nextcmd" is done, without reporting errors. Return
330 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
331 */
332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100333eval_to_string_skip(
334 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200335 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100336 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
Bram Moolenaar33570922005-01-25 22:26:29 +0000338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200340 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341
Bram Moolenaar37c83712020-06-30 21:18:36 +0200342 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (skip)
344 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200345 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 retval = NULL;
347 else
348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100349 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000350 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352 if (skip)
353 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200354 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 return retval;
357}
358
359/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000360 * Skip over an expression at "*pp".
361 * Return FAIL for an error, OK otherwise.
362 */
363 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200364skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000365{
Bram Moolenaar33570922005-01-25 22:26:29 +0000366 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367
368 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200369 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370}
371
372/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200373 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200374 * If in Vim9 script and line breaks are encountered, the lines are
375 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200376 * "arg" is advanced to just after the expression.
377 * "start" is set to the start of the expression, "end" to just after the end.
378 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Return FAIL for an error, OK otherwise.
380 */
381 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200382skip_expr_concatenate(
383 char_u **arg,
384 char_u **start,
385 char_u **end,
386 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200387{
388 typval_T rettv;
389 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200390 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200391 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200392 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200394 int evaluate = evalarg == NULL
395 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200398 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 {
400 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200404 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200406 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407
408 // Don't evaluate the expression.
409 if (evalarg != NULL)
410 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *arg = skipwhite(*arg);
412 res = eval1(arg, &rettv, evalarg);
413 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414 if (evalarg != NULL)
415 evalarg->eval_flags = save_flags;
416
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200417 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200418 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 if (evalarg->eval_ga.ga_len == 1)
421 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200422 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 ga_clear(gap);
424 gap->ga_itemsize = 0;
425 }
426 else
427 {
428 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200430
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200431 // Line breaks encountered, concatenate all the lines.
432 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200433 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434
435 // free the lines only when using getsourceline()
436 if (evalarg->eval_cookie != NULL)
437 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200438 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200439 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200440 // Do not free the last line, "arg" points into it, free it
441 // later.
442 vim_free(evalarg->eval_tofree);
443 evalarg->eval_tofree =
444 ((char_u **)gap->ga_data)[gap->ga_len - 1];
445 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear_strings(gap);
447 }
448 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200449 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200451
452 // free lines that were explicitly marked for freeing
453 ga_clear_strings(freegap);
454 }
455
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200456 gap->ga_itemsize = 0;
457 if (p == NULL)
458 return FAIL;
459 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 vim_free(evalarg->eval_tofree_lambda);
461 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200462 // Compute "end" relative to the end.
463 *end = *start + STRLEN(*start) - endoff;
464 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200465 }
466
467 return res;
468}
469
470/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100471 * Convert "tv" to a string.
472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
474 * Returns an allocated string (NULL when out of memory).
475 */
476 char_u *
477typval2string(typval_T *tv, int convert)
478{
479 garray_T ga;
480 char_u *retval;
481#ifdef FEAT_FLOAT
482 char_u numbuf[NUMBUFLEN];
483#endif
484
485 if (convert && tv->v_type == VAR_LIST)
486 {
487 ga_init2(&ga, (int)sizeof(char), 80);
488 if (tv->vval.v_list != NULL)
489 {
490 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
491 if (tv->vval.v_list->lv_len > 0)
492 ga_append(&ga, NL);
493 }
494 ga_append(&ga, NUL);
495 retval = (char_u *)ga.ga_data;
496 }
497#ifdef FEAT_FLOAT
498 else if (convert && tv->v_type == VAR_FLOAT)
499 {
500 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
501 retval = vim_strsave(numbuf);
502 }
503#endif
504 else
505 retval = vim_strsave(tv_get_string(tv));
506 return retval;
507}
508
509/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200510 * Top level evaluation function, returning a string. Does not handle line
511 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000512 * When "convert" is TRUE convert a List into a sequence of lines and convert
513 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 * Return pointer to allocated memory, or NULL for failure.
515 */
516 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100517eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100518 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100519 int convert,
520 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
Bram Moolenaar33570922005-01-25 22:26:29 +0000522 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100524 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100526 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
527 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 retval = NULL;
529 else
530 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100531 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000532 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100534 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 return retval;
537}
538
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100539 char_u *
540eval_to_string(
541 char_u *arg,
542 int convert)
543{
544 return eval_to_string_eap(arg, convert, NULL);
545}
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000548 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200550 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 */
552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100553eval_to_string_safe(
554 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100555 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200558 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200559 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200561 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000563 if (use_sandbox)
564 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200565 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 return retval;
573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575/*
576 * Top level evaluation function, returning a number.
577 * Evaluates "expr" silently.
578 * Returns -1 for an error.
579 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200580 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100581eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
Bram Moolenaar33570922005-01-25 22:26:29 +0000583 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000585 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 ++emsg_off;
588
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200589 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 retval = -1;
591 else
592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596 --emsg_off;
597
598 return retval;
599}
600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 * Top level evaluation function.
603 * Returns an allocated typval_T with the result.
604 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605 */
606 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200607eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000608{
609 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200610 evalarg_T evalarg;
611
612 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200614 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200615 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100616 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
Bram Moolenaar37c83712020-06-30 21:18:36 +0200618 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619 return tv;
620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100623 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200624 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
625 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000626 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629call_vim_function(
630 char_u *func,
631 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200632 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000635 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200636 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100638 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200639 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200640 funcexe.firstline = curwin->w_cursor.lnum;
641 funcexe.lastline = curwin->w_cursor.lnum;
642 funcexe.evaluate = TRUE;
643 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644 if (ret == FAIL)
645 clear_tv(rettv);
646
647 return ret;
648}
649
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100651 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100652 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200653 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
654 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100655 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200656 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657call_func_retnr(
658 char_u *func,
659 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200660 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100661{
662 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200663 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664
Bram Moolenaarded27a12018-08-01 19:06:03 +0200665 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666 return -1;
667
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100668 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669 clear_tv(&rettv);
670 return retval;
671}
672
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100674 * Call Vim script function like call_func_retnr() and drop the result.
675 * Returns FAIL when calling the function fails.
676 */
677 int
678call_func_noret(
679 char_u *func,
680 int argc,
681 typval_T *argv)
682{
683 typval_T rettv;
684
685 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
686 return FAIL;
687 clear_tv(&rettv);
688 return OK;
689}
690
691/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100692 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100693 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000694 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000695 */
696 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100697call_func_retstr(
698 char_u *func,
699 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200700 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701{
702 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000703 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000704
Bram Moolenaarded27a12018-08-01 19:06:03 +0200705 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000706 return NULL;
707
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100708 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 return retval;
711}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000712
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000713/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100714 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100715 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000716 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000717 */
718 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719call_func_retlist(
720 char_u *func,
721 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200722 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723{
724 typval_T rettv;
725
Bram Moolenaarded27a12018-08-01 19:06:03 +0200726 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727 return NULL;
728
729 if (rettv.v_type != VAR_LIST)
730 {
731 clear_tv(&rettv);
732 return NULL;
733 }
734
735 return rettv.vval.v_list;
736}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#ifdef FEAT_FOLDING
739/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100740 * Evaluate "arg", which is 'foldexpr'.
741 * Note: caller must set "curwin" to match "arg".
742 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
743 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100746eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200749 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000751 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
752 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000755 if (use_sandbox)
756 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200757 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200759 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 retval = 0;
761 else
762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000764 if (tv.v_type == VAR_NUMBER)
765 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000766 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 retval = 0;
768 else
769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 // If the result is a string, check if there is a non-digit before
771 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (!VIM_ISDIGIT(*s) && *s != '-')
774 *cp = *s++;
775 retval = atol((char *)s);
776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000777 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000780 if (use_sandbox)
781 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200782 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200783 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200785 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787#endif
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 * Get an lval: variable, Dict item or List item that can be assigned a value
791 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
792 * "name.key", "name.key[expr]" etc.
793 * Indexing only works if "name" is an existing List or Dictionary.
794 * "name" points to the start of the name.
795 * If "rettv" is not NULL it points to the value to be assigned.
796 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
797 * wrong; must end in space or cmd separator.
798 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100799 * flags:
800 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100801 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100802 * GLV_NO_AUTOLOAD: do not use script autoloading
803 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000805 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200808 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809get_lval(
810 char_u *name,
811 typval_T *rettv,
812 lval_T *lp,
813 int unlet,
814 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 int flags, // GLV_ values
816 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 char_u *expr_start, *expr_end;
820 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 dictitem_T *v;
822 typval_T var1;
823 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100828 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100829 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100830 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200833 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100835 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100837 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200839 lp->ll_name_end = find_name_end(name, NULL, NULL,
840 FNE_INCL_BR | fne_flags);
841 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 }
843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000845 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (expr_start != NULL)
848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100850 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 && *p != '[' && *p != '.')
852 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200853 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 return NULL;
855 }
856
857 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
858 if (lp->ll_exp_name == NULL)
859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 // Report an invalid expression in braces, unless the
861 // expression evaluation has been cancelled due to an
862 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 if (!aborting() && !quiet)
864 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000865 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 return NULL;
868 }
869 }
870 lp->ll_name = lp->ll_exp_name;
871 }
872 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 lp->ll_name = name;
875
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200876 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200878 // "a: type" is declaring variable "a" with a type, not "a:".
879 if (p == name + 2 && p[-1] == ':')
880 {
881 --p;
882 lp->ll_name_end = p;
883 }
884 if (*p == ':')
885 {
886 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
887 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200889 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100890 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
891 if (lp->ll_type == NULL && !quiet)
892 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200893 lp->ll_name_end = tp;
894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 }
896 }
897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100898 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
900 return p;
901
902 cc = *p;
903 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100904 // When we would write to the variable pass &ht and prevent autoload.
905 writing = !(flags & GLV_READ_ONLY);
906 v = find_var(lp->ll_name, writing ? &ht : NULL,
907 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200909 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000911 if (v == NULL)
912 return NULL;
913
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100914 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
915 {
916 if (!quiet)
917 semsg(_(e_variable_already_declared), lp->ll_name);
918 return NULL;
919 }
920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 /*
922 * Loop until no more [idx] or .key is following.
923 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000924 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100925 var1.v_type = VAR_UNKNOWN;
926 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200927 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200929 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
930 {
931 if (!quiet)
932 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
933 return NULL;
934 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200935 if (lp->ll_tv->v_type != VAR_LIST
936 && lp->ll_tv->v_type != VAR_DICT
937 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200943
944 // a NULL list/blob works like an empty list/blob, allocate one now.
945 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
946 rettv_list_alloc(lp->ll_tv);
947 else if (lp->ll_tv->v_type == VAR_BLOB
948 && lp->ll_tv->vval.v_blob == NULL)
949 rettv_blob_alloc(lp->ll_tv);
950
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (lp->ll_range)
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(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000957
Bram Moolenaar10c65862020-10-08 21:16:42 +0200958 if (in_vim9script() && lp->ll_valtype == NULL
959 && lp->ll_tv == &v->di_tv
960 && ht != NULL && ht == get_script_local_ht())
961 {
962 svar_T *sv = find_typval_in_script(lp->ll_tv);
963
964 // Vim9 script local variable: get the type
965 if (sv != NULL)
966 lp->ll_valtype = sv->sv_type;
967 }
968
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 len = -1;
970 if (*p == '.')
971 {
972 key = p + 1;
973 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
974 ;
975 if (len == 0)
976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100978 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 }
981 p = key + len;
982 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000983 else
984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100985 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000986 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 if (*p == ':')
988 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000989 else
990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200992 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100994 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000997 clear_tv(&var1);
998 return NULL;
999 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001000 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 }
1002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 if (*p == ':')
1005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001009 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001010 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001012 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001013 if (rettv != NULL
1014 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001015 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001017 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001021 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 }
1024 p = skipwhite(p + 1);
1025 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 else
1028 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001030 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001031 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001033 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001036 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001038 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001040 clear_tv(&var2);
1041 return NULL;
1042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001048
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001052 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001053 clear_tv(&var1);
1054 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 }
1057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001058 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 ++p;
1060 }
1061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 {
1064 if (len == -1)
1065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001066 // "[key]": get key from "var1"
1067 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001068 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 }
1073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001074 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001075
1076 // a NULL dict is equivalent with an empty dict
1077 if (lp->ll_tv->vval.v_dict == NULL)
1078 {
1079 lp->ll_tv->vval.v_dict = dict_alloc();
1080 if (lp->ll_tv->vval.v_dict == NULL)
1081 {
1082 clear_tv(&var1);
1083 return NULL;
1084 }
1085 ++lp->ll_tv->vval.v_dict->dv_refcount;
1086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001087 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001088
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001089 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // When assigning to a scope dictionary check that a function and
1092 // variable name is valid (only variable name unless it is l: or
1093 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001094 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001095 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001096 int prevval;
1097 int wrong;
1098
1099 if (len != -1)
1100 {
1101 prevval = key[len];
1102 key[len] = NUL;
1103 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001104 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001105 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001106 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1107 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001108 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001109 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001110 if (len != -1)
1111 key[len] = prevval;
1112 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001113 {
1114 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001115 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001116 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 }
1118
Bram Moolenaar10c65862020-10-08 21:16:42 +02001119 if (lp->ll_valtype != NULL)
1120 // use the type of the member
1121 lp->ll_valtype = lp->ll_valtype->tt_member;
1122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001125 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001126 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001127 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001130 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001131 return NULL;
1132 }
1133
Bram Moolenaar31b81602019-02-10 22:14:27 +01001134 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
1142 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001146 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 p = NULL;
1149 break;
1150 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001152 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001153 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1154 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001155 {
1156 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001157 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001158 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001159
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 else if (lp->ll_tv->v_type == VAR_BLOB)
1164 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001165 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1166
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 /*
1168 * Get the number and item for the only or first index of the List.
1169 */
1170 if (empty1)
1171 lp->ll_n1 = 0;
1172 else
1173 // is number or string
1174 lp->ll_n1 = (long)tv_get_number(&var1);
1175 clear_tv(&var1);
1176
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001177 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001178 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180 return NULL;
1181 }
1182 if (lp->ll_range && !lp->ll_empty2)
1183 {
1184 lp->ll_n2 = (long)tv_get_number(&var2);
1185 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001186 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1187 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 }
1190 lp->ll_blob = lp->ll_tv->vval.v_blob;
1191 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001192 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 else
1195 {
1196 /*
1197 * Get the number and item for the only or first index of the List.
1198 */
1199 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001201 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001203 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001204 clear_tv(&var1);
1205
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001206 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001208 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001209 if (lp->ll_li == NULL)
1210 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001211 // Vim9: Allow for adding an item at the end.
1212 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1213 && lp->ll_list->lv_lock == 0)
1214 {
1215 list_append_number(lp->ll_list, 0);
1216 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1217 }
1218 if (lp->ll_li == NULL)
1219 {
1220 clear_tv(&var2);
1221 if (!quiet)
1222 semsg(_(e_listidx), lp->ll_n1);
1223 return NULL;
1224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar10c65862020-10-08 21:16:42 +02001227 if (lp->ll_valtype != NULL)
1228 // use the type of the member
1229 lp->ll_valtype = lp->ll_valtype->tt_member;
1230
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 /*
1232 * May need to find the item or absolute index for the second
1233 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * When no index given: "lp->ll_empty2" is TRUE.
1235 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001239 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 {
1247 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (lp->ll_n1 < 0)
1256 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1257 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 {
1259 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001262 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001263 }
1264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 }
1268
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return p;
1272}
1273
1274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279{
1280 vim_free(lp->ll_exp_name);
1281 vim_free(lp->ll_newkey);
1282}
1283
1284/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001285 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001287 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1288 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291set_var_lval(
1292 lval_T *lp,
1293 char_u *endp,
1294 typval_T *rettv,
1295 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001296 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1297 char_u *op,
1298 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299{
1300 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 listitem_T *ri;
1302 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303
1304 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001306 cc = *endp;
1307 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001308 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1309 return;
1310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 if (lp->ll_blob != NULL)
1312 {
1313 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001314
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 if (op != NULL && *op != '=')
1316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001317 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 return;
1319 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001320 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001321 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322
1323 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1324 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001325 if (lp->ll_empty2)
1326 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1327
Bram Moolenaar68452172021-04-12 21:21:02 +02001328 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1329 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 }
1332 else
1333 {
1334 val = (int)tv_get_number_chk(rettv, &error);
1335 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001336 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 }
1339 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001342
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001343 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1344 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001345 {
1346 emsg(_(e_cannot_mod));
1347 *endp = cc;
1348 return;
1349 }
1350
Bram Moolenaarff697e62019-02-12 22:28:33 +01001351 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001353 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001354 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001355 {
1356 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001357 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1358 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 && tv_op(&tv, rettv, op) == OK)
1360 set_var(lp->ll_name, &tv, FALSE);
1361 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001364 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001365 {
1366 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001367 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001368 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001369 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1370 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001371 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001372 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001374 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001375 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001376 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001377 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378 else if (lp->ll_range)
1379 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001380 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001381 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001383 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1384 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001385 {
1386 emsg(_("E996: Cannot lock a range"));
1387 return;
1388 }
1389
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001390 /*
1391 * Check whether any of the list items is locked
1392 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001393 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001394 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001395 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 return;
1397 ri = ri->li_next;
1398 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1399 break;
1400 ll_li = ll_li->li_next;
1401 ++ll_n1;
1402 }
1403
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 /*
1405 * Assign the List values to the list items.
1406 */
1407 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 if (op != NULL && *op != '=')
1410 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1411 else
1412 {
1413 clear_tv(&lp->ll_li->li_tv);
1414 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1415 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 ri = ri->li_next;
1417 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1418 break;
1419 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001421 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001422 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 ri = NULL;
1425 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 lp->ll_li = lp->ll_li->li_next;
1429 ++lp->ll_n1;
1430 }
1431 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001432 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 else if (lp->ll_empty2
1434 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001436 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 }
1438 else
1439 {
1440 /*
1441 * Assign to a List or Dictionary item.
1442 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001443 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1444 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001445 {
1446 emsg(_("E996: Cannot lock a list or dict"));
1447 return;
1448 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001449
1450 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001451 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001452 return;
1453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (lp->ll_newkey != NULL)
1455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 if (op != NULL && *op != '=')
1457 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001458 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 return;
1460 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001461 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1462 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001463 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001465 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001466 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 if (di == NULL)
1468 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001469 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1470 {
1471 vim_free(di);
1472 return;
1473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 else if (op != NULL && *op != '=')
1477 {
1478 tv_op(lp->ll_tv, rettv, op);
1479 return;
1480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 /*
1485 * Assign the value to the variable or list item.
1486 */
1487 if (copy)
1488 copy_tv(rettv, lp->ll_tv);
1489 else
1490 {
1491 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001492 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 }
1495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496}
1497
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001499 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1500 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501 * Returns OK or FAIL.
1502 */
1503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001506 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 char_u numbuf[NUMBUFLEN];
1508 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001509 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001511 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001512 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001513 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 {
1515 switch (tv1->v_type)
1516 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001517 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001518 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 case VAR_DICT:
1521 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001522 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001523 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001524 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001525 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001527 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 break;
1529
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001530 case VAR_BLOB:
1531 if (*op != '+' || tv2->v_type != VAR_BLOB)
1532 break;
1533 // BLOB += BLOB
1534 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1535 {
1536 blob_T *b1 = tv1->vval.v_blob;
1537 blob_T *b2 = tv2->vval.v_blob;
1538 int i, len = blob_len(b2);
1539 for (i = 0; i < len; i++)
1540 ga_append(&b1->bv_ga, blob_get(b2, i));
1541 }
1542 return OK;
1543
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 case VAR_LIST:
1545 if (*op != '+' || tv2->v_type != VAR_LIST)
1546 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001547 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001548 if (tv2->vval.v_list != NULL)
1549 {
1550 if (tv1->vval.v_list == NULL)
1551 {
1552 tv1->vval.v_list = tv2->vval.v_list;
1553 ++tv1->vval.v_list->lv_refcount;
1554 }
1555 else
1556 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 return OK;
1559
1560 case VAR_NUMBER:
1561 case VAR_STRING:
1562 if (tv2->v_type == VAR_LIST)
1563 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001567 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568#ifdef FEAT_FLOAT
1569 if (tv2->v_type == VAR_FLOAT)
1570 {
1571 float_T f = n;
1572
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 if (*op == '%')
1574 break;
1575 switch (*op)
1576 {
1577 case '+': f += tv2->vval.v_float; break;
1578 case '-': f -= tv2->vval.v_float; break;
1579 case '*': f *= tv2->vval.v_float; break;
1580 case '/': f /= tv2->vval.v_float; break;
1581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 clear_tv(tv1);
1583 tv1->v_type = VAR_FLOAT;
1584 tv1->vval.v_float = f;
1585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587#endif
1588 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 switch (*op)
1590 {
1591 case '+': n += tv_get_number(tv2); break;
1592 case '-': n -= tv_get_number(tv2); break;
1593 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001594 case '/': n = num_divide(n, tv_get_number(tv2),
1595 &failed); break;
1596 case '%': n = num_modulus(n, tv_get_number(tv2),
1597 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599 clear_tv(tv1);
1600 tv1->v_type = VAR_NUMBER;
1601 tv1->vval.v_number = n;
1602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 }
1604 else
1605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 if (tv2->v_type == VAR_FLOAT)
1607 break;
1608
Bram Moolenaarff697e62019-02-12 22:28:33 +01001609 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001610 s = tv_get_string(tv1);
1611 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 clear_tv(tv1);
1613 tv1->v_type = VAR_STRING;
1614 tv1->vval.v_string = s;
1615 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001616 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001619#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620 {
1621 float_T f;
1622
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 if (*op == '%' || *op == '.'
1624 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 && tv2->v_type != VAR_NUMBER
1626 && tv2->v_type != VAR_STRING))
1627 break;
1628 if (tv2->v_type == VAR_FLOAT)
1629 f = tv2->vval.v_float;
1630 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001631 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001632 switch (*op)
1633 {
1634 case '+': tv1->vval.v_float += f; break;
1635 case '-': tv1->vval.v_float -= f; break;
1636 case '*': tv1->vval.v_float *= f; break;
1637 case '/': tv1->vval.v_float /= f; break;
1638 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001641 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 }
1643 }
1644
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001645 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 return FAIL;
1647}
1648
1649/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 * Evaluate the expression used in a ":for var in expr" command.
1651 * "arg" points to "var".
1652 * Set "*errp" to TRUE for an error, FALSE otherwise;
1653 * Return a pointer that holds the info. Null when there is an error.
1654 */
1655 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001656eval_for_line(
1657 char_u *arg,
1658 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001659 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001660 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661{
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001663 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 typval_T tv;
1666 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001667 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001669 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001671 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (fi == NULL)
1673 return NULL;
1674
Bram Moolenaar404557e2021-07-05 21:41:48 +02001675 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1676 &fi->fi_semicolon, FALSE);
1677 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 return fi;
1679
Bram Moolenaar404557e2021-07-05 21:41:48 +02001680 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001681 if (expr[0] != 'i' || expr[1] != 'n'
1682 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001684 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1685 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1686 else
1687 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 return fi;
1689 }
1690
1691 if (skip)
1692 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001693 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1694 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 {
1696 *errp = FALSE;
1697 if (!skip)
1698 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001700 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 l = tv.vval.v_list;
1702 if (l == NULL)
1703 {
1704 // a null list is like an empty list: do nothing
1705 clear_tv(&tv);
1706 }
1707 else
1708 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001710 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001712 // No need to increment the refcount, it's already set for
1713 // the list being used in "tv".
1714 fi->fi_list = l;
1715 list_add_watch(l, &fi->fi_lw);
1716 fi->fi_lw.lw_item = l->lv_first;
1717 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001718 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001719 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001720 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001721 fi->fi_bi = 0;
1722 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 typval_T btv;
1725
1726 // Make a copy, so that the iteration still works when the
1727 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001729 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001731 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001732 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001733 else if (tv.v_type == VAR_STRING)
1734 {
1735 fi->fi_byte_idx = 0;
1736 fi->fi_string = tv.vval.v_string;
1737 tv.vval.v_string = NULL;
1738 if (fi->fi_string == NULL)
1739 fi->fi_string = vim_strsave((char_u *)"");
1740 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 else
1742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001743 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 }
1746 }
1747 }
1748 if (skip)
1749 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001750 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
1752 return fi;
1753}
1754
1755/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001756 * Used when looping over a :for line, skip the "in expr" part.
1757 */
1758 void
1759skip_for_lines(void *fi_void, evalarg_T *evalarg)
1760{
1761 forinfo_T *fi = (forinfo_T *)fi_void;
1762 int i;
1763
1764 for (i = 0; i < fi->fi_break_count; ++i)
1765 eval_next_line(evalarg);
1766}
1767
1768/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Use the first item in a ":for" list. Advance to the next.
1770 * Assign the values to the variable (list). "arg" points to the first one.
1771 * Return TRUE when a valid item was found, FALSE when at end of list or
1772 * something wrong.
1773 */
1774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001779 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1780 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1781 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001784 if (fi->fi_blob != NULL)
1785 {
1786 typval_T tv;
1787
1788 if (fi->fi_bi >= blob_len(fi->fi_blob))
1789 return FALSE;
1790 tv.v_type = VAR_NUMBER;
1791 tv.v_lock = VAR_FIXED;
1792 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1793 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001794 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001795 fi->fi_varcount, flag, NULL) == OK;
1796 }
1797
1798 if (fi->fi_string != NULL)
1799 {
1800 typval_T tv;
1801 int len;
1802
1803 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1804 if (len == 0)
1805 return FALSE;
1806 tv.v_type = VAR_STRING;
1807 tv.v_lock = VAR_FIXED;
1808 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1809 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001810 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001811 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001812 vim_free(tv.vval.v_string);
1813 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001814 }
1815
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 item = fi->fi_lw.lw_item;
1817 if (item == NULL)
1818 result = FALSE;
1819 else
1820 {
1821 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001822 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001823 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 }
1825 return result;
1826}
1827
1828/*
1829 * Free the structure used to store info used by ":for".
1830 */
1831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001832free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833{
Bram Moolenaar33570922005-01-25 22:26:29 +00001834 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001836 if (fi == NULL)
1837 return;
1838 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001839 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001841 list_unref(fi->fi_list);
1842 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001843 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001844 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001845 else
1846 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 vim_free(fi);
1848}
1849
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851set_context_for_expression(
1852 expand_T *xp,
1853 char_u *arg,
1854 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001856 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001860 if (cmdidx == CMD_let || cmdidx == CMD_var
1861 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 {
1863 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001864 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001866 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001867 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 {
1869 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001870 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001871 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 break;
1873 }
1874 return;
1875 }
1876 }
1877 else
1878 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1879 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 while ((xp->xp_pattern = vim_strpbrk(arg,
1881 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1882 {
1883 c = *xp->xp_pattern;
1884 if (c == '&')
1885 {
1886 c = xp->xp_pattern[1];
1887 if (c == '&')
1888 {
1889 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001890 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001895 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1896 xp->xp_pattern += 2;
1897
1898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 else if (c == '$')
1901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001902 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 xp->xp_context = EXPAND_ENV_VARS;
1904 }
1905 else if (c == '=')
1906 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 xp->xp_context = EXPAND_EXPRESSION;
1909 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001910 else if (c == '#'
1911 && xp->xp_context == EXPAND_EXPRESSION)
1912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001914 break;
1915 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001916 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 && xp->xp_context == EXPAND_FUNCTIONS
1918 && vim_strchr(xp->xp_pattern, '(') == NULL)
1919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001920 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 break;
1922 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001923 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1928 if (c == '\\' && xp->xp_pattern[1] != NUL)
1929 ++xp->xp_pattern;
1930 xp->xp_context = EXPAND_NOTHING;
1931 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001932 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001934 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1936 /* skip */ ;
1937 xp->xp_context = EXPAND_NOTHING;
1938 }
1939 else if (c == '|')
1940 {
1941 if (xp->xp_pattern[1] == '|')
1942 {
1943 ++xp->xp_pattern;
1944 xp->xp_context = EXPAND_EXPRESSION;
1945 }
1946 else
1947 xp->xp_context = EXPAND_COMMANDS;
1948 }
1949 else
1950 xp->xp_context = EXPAND_EXPRESSION;
1951 }
1952 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001953 // Doesn't look like something valid, expand as an expression
1954 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 arg = xp->xp_pattern;
1957 if (*arg != NUL)
1958 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1959 /* skip */ ;
1960 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001961
1962 // ":exe one two" completes "two"
1963 if ((cmdidx == CMD_execute
1964 || cmdidx == CMD_echo
1965 || cmdidx == CMD_echon
1966 || cmdidx == CMD_echomsg)
1967 && xp->xp_context == EXPAND_EXPRESSION)
1968 {
1969 for (;;)
1970 {
1971 char_u *n = skiptowhite(arg);
1972
1973 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1974 break;
1975 arg = skipwhite(n);
1976 }
1977 }
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 xp->xp_pattern = arg;
1980}
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001983 * Return TRUE if "pat" matches "text".
1984 * Does not use 'cpo' and always uses 'magic'.
1985 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001986 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001987pattern_match(char_u *pat, char_u *text, int ic)
1988{
1989 int matches = FALSE;
1990 char_u *save_cpo;
1991 regmatch_T regmatch;
1992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001993 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001994 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001995 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001996 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1997 if (regmatch.regprog != NULL)
1998 {
1999 regmatch.rm_ic = ic;
2000 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2001 vim_regfree(regmatch.regprog);
2002 }
2003 p_cpo = save_cpo;
2004 return matches;
2005}
2006
2007/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 * Handle a name followed by "(". Both for just "name(arg)" and for
2009 * "expr->name(arg)".
2010 * Returns OK or FAIL.
2011 */
2012 static int
2013eval_func(
2014 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002015 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 char_u *name,
2017 int name_len,
2018 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002019 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020 typval_T *basetv) // "expr" for "expr->name(arg)"
2021{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 char_u *s = name;
2024 int len = name_len;
2025 partial_T *partial;
2026 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002027 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002028
2029 if (!evaluate)
2030 check_vars(s, len);
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // If "s" is the name of a variable of type VAR_FUNC
2033 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002034 s = deref_func_name(s, &len, &partial,
2035 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002037 // Need to make a copy, in case evaluating the arguments makes
2038 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002040 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 ret = FAIL;
2042 else
2043 {
2044 funcexe_T funcexe;
2045
2046 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002047 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 funcexe.firstline = curwin->w_cursor.lnum;
2049 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002050 funcexe.evaluate = evaluate;
2051 funcexe.partial = partial;
2052 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002053 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002054 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 }
2056 vim_free(s);
2057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002058 // If evaluate is FALSE rettv->v_type was not set in
2059 // get_func_tv, but it's needed in handle_subscript() to parse
2060 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002061 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2062 {
2063 rettv->vval.v_string = NULL;
2064 rettv->v_type = VAR_FUNC;
2065 }
2066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002067 // Stop the expression evaluation when immediately
2068 // aborting on error, or when an interrupt occurred or
2069 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002070 if (evaluate && aborting())
2071 {
2072 if (ret == OK)
2073 clear_tv(rettv);
2074 ret = FAIL;
2075 }
2076 return ret;
2077}
2078
2079/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002080 * Get the next line source line without advancing. But do skip over comment
2081 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002082 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002083 */
2084 static char_u *
2085getline_peek_skip_comments(evalarg_T *evalarg)
2086{
2087 for (;;)
2088 {
2089 char_u *next = getline_peek(evalarg->eval_getline,
2090 evalarg->eval_cookie);
2091 char_u *p;
2092
2093 if (next == NULL)
2094 break;
2095 p = skipwhite(next);
2096 if (*p != NUL && !vim9_comment_start(p))
2097 return next;
2098 (void)eval_next_line(evalarg);
2099 }
2100 return NULL;
2101}
2102
2103/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002104 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2105 * comment) and there is a next line, return the next line (skipping blanks)
2106 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002107 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2108 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109 * "arg" must point somewhere inside a line, not at the start.
2110 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002111 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2113{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002114 char_u *p = skipwhite(arg);
2115
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002117 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002119 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002120 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002123
2124 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002125 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002126 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128
Bram Moolenaar9d489562020-07-30 20:08:50 +02002129 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002130 {
2131 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002132 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 }
2134 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136}
2137
2138/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002139 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002140 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002141 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002142 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002143eval_next_line(evalarg_T *evalarg)
2144{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002145 garray_T *gap = &evalarg->eval_ga;
2146 char_u *line;
2147
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002148 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002149 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2150 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002151 else
2152 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002153 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002154 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2155 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002156 char_u *p = skipwhite(line);
2157
2158 // Going to concatenate the lines after parsing. For an empty or
2159 // comment line use an empty string.
2160 if (*p == NUL || vim9_comment_start(p))
2161 {
2162 vim_free(line);
2163 line = vim_strsave((char_u *)"");
2164 }
2165
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002166 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2167 ++gap->ga_len;
2168 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002169 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002170 {
2171 vim_free(evalarg->eval_tofree);
2172 evalarg->eval_tofree = line;
2173 }
2174 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002175}
2176
Bram Moolenaare6b53242020-07-01 17:28:33 +02002177/*
2178 * Call eval_next_non_blank() and get the next line if needed.
2179 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002180 char_u *
2181skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2182{
2183 int getnext;
2184 char_u *p = skipwhite(arg);
2185
Bram Moolenaar962d7212020-07-04 14:15:00 +02002186 if (evalarg == NULL)
2187 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002188 eval_next_non_blank(p, evalarg, &getnext);
2189 if (getnext)
2190 return eval_next_line(evalarg);
2191 return p;
2192}
2193
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002194/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002195 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002196 */
2197 void
2198clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2199{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002200 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002201 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002202 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002203 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002204 if (eap != NULL)
2205 {
2206 // We may need to keep the original command line, e.g. for
2207 // ":let" it has the variable names. But we may also need the
2208 // new one, "nextcmd" points into it. Keep both.
2209 vim_free(eap->cmdline_tofree);
2210 eap->cmdline_tofree = *eap->cmdlinep;
2211 *eap->cmdlinep = evalarg->eval_tofree;
2212 }
2213 else
2214 vim_free(evalarg->eval_tofree);
2215 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002216 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002217
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002218 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2219 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002220 }
2221}
2222
2223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2227 */
2228
2229/*
2230 * Handle zero level expression.
2231 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002233 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002234 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * Return OK or FAIL.
2236 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238eval0(
2239 char_u *arg,
2240 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002241 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002242 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 int ret;
2245 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002246 int did_emsg_before = did_emsg;
2247 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002248 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002249 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002252 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002253 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002254
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002255 if (ret != FAIL)
2256 end_error = !ends_excmd2(arg, p);
2257 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /*
2262 * Report the invalid expression unless the expression evaluation has
2263 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002264 * exception, or we already gave a more specific error.
2265 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002267 if (!aborting()
2268 && did_emsg == did_emsg_before
2269 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002270 && (flags & EVAL_CONSTANT) == 0
2271 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002272 {
2273 if (end_error)
2274 semsg(_(e_trailing_arg), p);
2275 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002276 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002277 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002278
2279 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002280 // a next command to avoid more errors, unless "|" is following, which
2281 // could only be a command separator.
2282 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2283 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002286
2287 if (eap != NULL)
2288 eap->nextcmd = check_nextcmd(p);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 return ret;
2291}
2292
2293/*
2294 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002295 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002296 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 *
2298 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002301 * Note: "rettv.v_lock" is not set.
2302 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 * Return OK or FAIL.
2304 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002305 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002306eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002308 char_u *p;
2309 int getnext;
2310
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002311 CLEAR_POINTER(rettv);
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 /*
2314 * Get the first variable.
2315 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 return FAIL;
2318
Bram Moolenaar793648f2020-06-26 21:28:25 +02002319 p = eval_next_non_blank(*arg, evalarg, &getnext);
2320 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002322 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002323 int result;
2324 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002325 evalarg_T *evalarg_used = evalarg;
2326 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002328 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002329 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330
2331 if (evalarg == NULL)
2332 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002333 CLEAR_FIELD(local_evalarg);
2334 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002335 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336 orig_flags = evalarg_used->eval_flags;
2337 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002338
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339 if (getnext)
2340 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002341 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002342 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002343 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002344 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002345 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002346 clear_tv(rettv);
2347 return FAIL;
2348 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002349 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002350 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 int error = FALSE;
2356
Bram Moolenaar13106602020-10-04 16:06:05 +02002357 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002358 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002359 else if (vim9script)
2360 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002361 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002363 if (error || !op_falsy || !result)
2364 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 if (error)
2366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368
2369 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002370 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002372 if (op_falsy)
2373 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002374 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002375 {
mityu4ac198c2021-05-28 17:52:40 +02002376 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002377 clear_tv(rettv);
2378 return FAIL;
2379 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002380 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002381 evalarg_used->eval_flags = (op_falsy ? !result : result)
2382 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2383 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002384 {
2385 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002387 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 if (!op_falsy || !result)
2389 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002391 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 /*
2394 * Check for the ":".
2395 */
2396 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2397 if (*p != ':')
2398 {
2399 emsg(_(e_missing_colon));
2400 if (evaluate && result)
2401 clear_tv(rettv);
2402 evalarg_used->eval_flags = orig_flags;
2403 return FAIL;
2404 }
2405 if (getnext)
2406 *arg = eval_next_line(evalarg_used);
2407 else
2408 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002409 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002410 {
2411 error_white_both(p, 1);
2412 clear_tv(rettv);
2413 evalarg_used->eval_flags = orig_flags;
2414 return FAIL;
2415 }
2416 *arg = p;
2417 }
2418
2419 /*
2420 * Get the third variable. Recursive!
2421 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002422 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002423 {
mityu4ac198c2021-05-28 17:52:40 +02002424 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002425 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002426 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002427 return FAIL;
2428 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002429 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2430 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002432 if (eval1(arg, &var2, evalarg_used) == FAIL)
2433 {
2434 if (evaluate && result)
2435 clear_tv(rettv);
2436 evalarg_used->eval_flags = orig_flags;
2437 return FAIL;
2438 }
2439 if (evaluate && !result)
2440 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442
2443 if (evalarg == NULL)
2444 clear_evalarg(&local_evalarg, NULL);
2445 else
2446 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448
2449 return OK;
2450}
2451
2452/*
2453 * Handle first level expression:
2454 * expr2 || expr2 || expr2 logical OR
2455 *
2456 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002457 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 *
2459 * Return OK or FAIL.
2460 */
2461 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002464 char_u *p;
2465 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /*
2468 * Get the first variable.
2469 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002470 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return FAIL;
2472
2473 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002476 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002477 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002479 evalarg_T *evalarg_used = evalarg;
2480 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 int evaluate;
2482 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002483 long result = FALSE;
2484 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002485 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002486 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002487
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488 if (evalarg == NULL)
2489 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490 CLEAR_FIELD(local_evalarg);
2491 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 orig_flags = evalarg_used->eval_flags;
2494 evaluate = orig_flags & EVAL_EVALUATE;
2495 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002497 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002498 result = tv_get_bool_chk(rettv, &error);
2499 else if (tv_get_number_chk(rettv, &error) != 0)
2500 result = TRUE;
2501 clear_tv(rettv);
2502 if (error)
2503 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
2505
2506 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509 while (p[0] == '|' && p[1] == '|')
2510 {
2511 if (getnext)
2512 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002513 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002514 {
2515 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2516 {
2517 error_white_both(p, 2);
2518 clear_tv(rettv);
2519 return FAIL;
2520 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002521 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523
2524 /*
2525 * Get the second variable.
2526 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002527 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2528 {
mityu4ac198c2021-05-28 17:52:40 +02002529 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002530 clear_tv(rettv);
2531 return FAIL;
2532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2534 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538
2539 /*
2540 * Compute the result.
2541 */
2542 if (evaluate && !result)
2543 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002544 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002545 result = tv_get_bool_chk(&var2, &error);
2546 else if (tv_get_number_chk(&var2, &error) != 0)
2547 result = TRUE;
2548 clear_tv(&var2);
2549 if (error)
2550 return FAIL;
2551 }
2552 if (evaluate)
2553 {
2554 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002555 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 rettv->v_type = VAR_BOOL;
2557 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002558 }
2559 else
2560 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002561 rettv->v_type = VAR_NUMBER;
2562 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002563 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565
2566 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002568
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569 if (evalarg == NULL)
2570 clear_evalarg(&local_evalarg, NULL);
2571 else
2572 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
2575 return OK;
2576}
2577
2578/*
2579 * Handle second level expression:
2580 * expr3 && expr3 && expr3 logical AND
2581 *
2582 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002583 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 *
2585 * Return OK or FAIL.
2586 */
2587 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590 char_u *p;
2591 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593 /*
2594 * Get the first variable.
2595 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002596 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return FAIL;
2598
2599 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002602 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002603 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 evalarg_T *evalarg_used = evalarg;
2606 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002607 int orig_flags;
2608 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002609 long result = TRUE;
2610 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002611 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002612 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002613
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 if (evalarg == NULL)
2615 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616 CLEAR_FIELD(local_evalarg);
2617 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002619 orig_flags = evalarg_used->eval_flags;
2620 evaluate = orig_flags & EVAL_EVALUATE;
2621 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002622 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002623 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002624 result = tv_get_bool_chk(rettv, &error);
2625 else if (tv_get_number_chk(rettv, &error) == 0)
2626 result = FALSE;
2627 clear_tv(rettv);
2628 if (error)
2629 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631
2632 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002633 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 while (p[0] == '&' && p[1] == '&')
2636 {
2637 if (getnext)
2638 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002639 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002640 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002641 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002642 {
2643 error_white_both(p, 2);
2644 clear_tv(rettv);
2645 return FAIL;
2646 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002647 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002648 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002649
2650 /*
2651 * Get the second variable.
2652 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002653 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2654 {
mityu4ac198c2021-05-28 17:52:40 +02002655 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002656 clear_tv(rettv);
2657 return FAIL;
2658 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2660 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002661 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002662 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
2666 /*
2667 * Compute the result.
2668 */
2669 if (evaluate && result)
2670 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002671 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002672 result = tv_get_bool_chk(&var2, &error);
2673 else if (tv_get_number_chk(&var2, &error) == 0)
2674 result = FALSE;
2675 clear_tv(&var2);
2676 if (error)
2677 return FAIL;
2678 }
2679 if (evaluate)
2680 {
2681 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 rettv->v_type = VAR_BOOL;
2684 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 }
2686 else
2687 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002688 rettv->v_type = VAR_NUMBER;
2689 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692
2693 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002695
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 if (evalarg == NULL)
2697 clear_evalarg(&local_evalarg, NULL);
2698 else
2699 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701
2702 return OK;
2703}
2704
2705/*
2706 * Handle third level expression:
2707 * var1 == var2
2708 * var1 =~ var2
2709 * var1 != var2
2710 * var1 !~ var2
2711 * var1 > var2
2712 * var1 >= var2
2713 * var1 < var2
2714 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002715 * var1 is var2
2716 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 *
2718 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002719 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 *
2721 * Return OK or FAIL.
2722 */
2723 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002724eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002727 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002728 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002730 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * Get the first variable.
2734 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 return FAIL;
2737
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002738 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002739 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002744 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002746 typval_T var2;
2747 int ic;
2748 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002749 int evaluate = evalarg == NULL
2750 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002751
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002752 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002753 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002754 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002755 p = *arg;
2756 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002757 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2758 {
mityu4ac198c2021-05-28 17:52:40 +02002759 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002760 clear_tv(rettv);
2761 return FAIL;
2762 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002763
Bram Moolenaar696ba232020-07-29 21:20:41 +02002764 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2765 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002766 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002767 clear_tv(rettv);
2768 return FAIL;
2769 }
2770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002771 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (p[len] == '?')
2773 {
2774 ic = TRUE;
2775 ++len;
2776 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else if (p[len] == '#')
2779 {
2780 ic = FALSE;
2781 ++len;
2782 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002783 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002785 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /*
2788 * Get the second variable.
2789 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002790 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2791 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002792 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002793 clear_tv(rettv);
2794 return FAIL;
2795 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002796 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002797 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 return FAIL;
2801 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002802 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002803 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002804 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002805
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002806 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002807 {
2808 ret = FAIL;
2809 clear_tv(rettv);
2810 }
2811 else
2812 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002813 clear_tv(&var2);
2814 return ret;
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817
2818 return OK;
2819}
2820
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002821/*
2822 * Make a copy of blob "tv1" and append blob "tv2".
2823 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 void
2825eval_addblob(typval_T *tv1, typval_T *tv2)
2826{
2827 blob_T *b1 = tv1->vval.v_blob;
2828 blob_T *b2 = tv2->vval.v_blob;
2829 blob_T *b = blob_alloc();
2830 int i;
2831
2832 if (b != NULL)
2833 {
2834 for (i = 0; i < blob_len(b1); i++)
2835 ga_append(&b->bv_ga, blob_get(b1, i));
2836 for (i = 0; i < blob_len(b2); i++)
2837 ga_append(&b->bv_ga, blob_get(b2, i));
2838
2839 clear_tv(tv1);
2840 rettv_blob_set(tv1, b);
2841 }
2842}
2843
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002844/*
2845 * Make a copy of list "tv1" and append list "tv2".
2846 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 int
2848eval_addlist(typval_T *tv1, typval_T *tv2)
2849{
2850 typval_T var3;
2851
2852 // concatenate Lists
2853 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2854 {
2855 clear_tv(tv1);
2856 clear_tv(tv2);
2857 return FAIL;
2858 }
2859 clear_tv(tv1);
2860 *tv1 = var3;
2861 return OK;
2862}
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
2865 * Handle fourth level expression:
2866 * + number addition
2867 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002868 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002869 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 *
2871 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002872 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 *
2874 * Return OK or FAIL.
2875 */
2876 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 /*
2880 * Get the first variable.
2881 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 return FAIL;
2884
2885 /*
2886 * Repeat computing, until no '+', '-' or '.' is following.
2887 */
2888 for (;;)
2889 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002890 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 int getnext;
2892 char_u *p;
2893 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002894 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 int concat;
2896 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002897 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002899 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002900 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002901 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 p = eval_next_non_blank(*arg, evalarg, &getnext);
2903 op = *p;
2904 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002905 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2906 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002908 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2909 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002910
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002911 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002912 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002914 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002915 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002916 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002917 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002918 {
mityu4ac198c2021-05-28 17:52:40 +02002919 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002920 clear_tv(rettv);
2921 return FAIL;
2922 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002923 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002924 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002925 if ((op != '+' || (rettv->v_type != VAR_LIST
2926 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002927#ifdef FEAT_FLOAT
2928 && (op == '.' || rettv->v_type != VAR_FLOAT)
2929#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002930 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002931 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002932 int error = FALSE;
2933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002934 // For "list + ...", an illegal use of the first operand as
2935 // a number cannot be determined before evaluating the 2nd
2936 // operand: if this is also a list, all is ok.
2937 // For "something . ...", "something - ..." or "non-list + ...",
2938 // we know that the first operand needs to be a string or number
2939 // without evaluating the 2nd operand. So check before to avoid
2940 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002941 if (op != '.')
2942 tv_get_number_chk(rettv, &error);
2943 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002944 {
2945 clear_tv(rettv);
2946 return FAIL;
2947 }
2948 }
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 /*
2951 * Get the second variable.
2952 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002953 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002954 {
mityu89dcb4d2021-05-28 13:50:17 +02002955 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002956 clear_tv(rettv);
2957 return FAIL;
2958 }
2959 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002960 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 return FAIL;
2964 }
2965
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 /*
2969 * Compute the result.
2970 */
2971 if (op == '.')
2972 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2974 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002975 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002976
Bram Moolenaar2e086612020-08-25 22:37:48 +02002977 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002978 || var2.v_type == VAR_CHANNEL
2979 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002980 semsg(_(e_using_invalid_value_as_string_str),
2981 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002982#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002983 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002984 {
2985 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2986 var2.vval.v_float);
2987 s2 = buf2;
2988 }
2989#endif
2990 else
2991 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002993 {
2994 clear_tv(rettv);
2995 clear_tv(&var2);
2996 return FAIL;
2997 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999 clear_tv(rettv);
3000 rettv->v_type = VAR_STRING;
3001 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003003 else if (op == '+' && rettv->v_type == VAR_BLOB
3004 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003006 else if (op == '+' && rettv->v_type == VAR_LIST
3007 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003008 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003010 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 else
3013 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003014 int error = FALSE;
3015 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003017 float_T f1 = 0, f2 = 0;
3018
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021 f1 = rettv->vval.v_float;
3022 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003023 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024 else
3025#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003027 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028 if (error)
3029 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003030 // This can only happen for "list + non-list" or
3031 // "blob + non-blob". For "non-list + ..." or
3032 // "something - ...", we returned before evaluating the
3033 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003035 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036 return FAIL;
3037 }
3038#ifdef FEAT_FLOAT
3039 if (var2.v_type == VAR_FLOAT)
3040 f1 = n1;
3041#endif
3042 }
3043#ifdef FEAT_FLOAT
3044 if (var2.v_type == VAR_FLOAT)
3045 {
3046 f2 = var2.vval.v_float;
3047 n2 = 0;
3048 }
3049 else
3050#endif
3051 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003052 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053 if (error)
3054 {
3055 clear_tv(rettv);
3056 clear_tv(&var2);
3057 return FAIL;
3058 }
3059#ifdef FEAT_FLOAT
3060 if (rettv->v_type == VAR_FLOAT)
3061 f2 = n2;
3062#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065
3066#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003068 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3069 {
3070 if (op == '+')
3071 f1 = f1 + f2;
3072 else
3073 f1 = f1 - f2;
3074 rettv->v_type = VAR_FLOAT;
3075 rettv->vval.v_float = f1;
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078#endif
3079 {
3080 if (op == '+')
3081 n1 = n1 + n2;
3082 else
3083 n1 = n1 - n2;
3084 rettv->v_type = VAR_NUMBER;
3085 rettv->vval.v_number = n1;
3086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090 }
3091 return OK;
3092}
3093
3094/*
3095 * Handle fifth level expression:
3096 * * number multiplication
3097 * / number division
3098 * % number modulo
3099 *
3100 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003101 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 *
3103 * Return OK or FAIL.
3104 */
3105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003106eval6(
3107 char_u **arg,
3108 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003109 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003110 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003113 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 /*
3117 * Get the first variable.
3118 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003119 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 return FAIL;
3121
3122 /*
3123 * Repeat computing, until no '*', '/' or '%' is following.
3124 */
3125 for (;;)
3126 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003127 int evaluate;
3128 int getnext;
3129 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003130 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003131 int op;
3132 varnumber_T n1, n2;
3133#ifdef FEAT_FLOAT
3134 float_T f1, f2;
3135#endif
3136 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003137
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003138 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003139 p = eval_next_non_blank(*arg, evalarg, &getnext);
3140 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003141 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003143
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003144 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003146 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003147 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003148 {
3149 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3150 {
mityu4ac198c2021-05-28 17:52:40 +02003151 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003152 clear_tv(rettv);
3153 return FAIL;
3154 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003155 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003158#ifdef FEAT_FLOAT
3159 f1 = 0;
3160 f2 = 0;
3161#endif
3162 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003163 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#ifdef FEAT_FLOAT
3166 if (rettv->v_type == VAR_FLOAT)
3167 {
3168 f1 = rettv->vval.v_float;
3169 use_float = TRUE;
3170 n1 = 0;
3171 }
3172 else
3173#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003174 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003175 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003176 if (error)
3177 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 else
3180 n1 = 0;
3181
3182 /*
3183 * Get the second variable.
3184 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003185 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3186 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003187 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003188 clear_tv(rettv);
3189 return FAIL;
3190 }
3191 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003192 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 return FAIL;
3194
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197#ifdef FEAT_FLOAT
3198 if (var2.v_type == VAR_FLOAT)
3199 {
3200 if (!use_float)
3201 {
3202 f1 = n1;
3203 use_float = TRUE;
3204 }
3205 f2 = var2.vval.v_float;
3206 n2 = 0;
3207 }
3208 else
3209#endif
3210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003211 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212 clear_tv(&var2);
3213 if (error)
3214 return FAIL;
3215#ifdef FEAT_FLOAT
3216 if (use_float)
3217 f2 = n2;
3218#endif
3219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
3221 /*
3222 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225#ifdef FEAT_FLOAT
3226 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228 if (op == '*')
3229 f1 = f1 * f2;
3230 else if (op == '/')
3231 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003232# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003233 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003234 if (f2 == 0.0)
3235 {
3236 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003237 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003238 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003239 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003240 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003241 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003242 }
3243 else
3244 f1 = f1 / f2;
3245# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003246 // We rely on the floating point library to handle divide
3247 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003249# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003252 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003254 return FAIL;
3255 }
3256 rettv->v_type = VAR_FLOAT;
3257 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003262 int failed = FALSE;
3263
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003264 if (op == '*')
3265 n1 = n1 * n2;
3266 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003267 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003269 n1 = num_modulus(n1, n2, &failed);
3270 if (failed)
3271 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003273 rettv->v_type = VAR_NUMBER;
3274 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 }
3278
3279 return OK;
3280}
3281
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003282/*
3283 * Handle a type cast before a base level expression.
3284 * "arg" must point to the first non-white of the expression.
3285 * "arg" is advanced to just after the recognized expression.
3286 * Return OK or FAIL.
3287 */
3288 static int
3289eval7t(
3290 char_u **arg,
3291 typval_T *rettv,
3292 evalarg_T *evalarg,
3293 int want_string) // after "." operator
3294{
3295 type_T *want_type = NULL;
3296 garray_T type_list; // list of pointers to allocated types
3297 int res;
3298 int evaluate = evalarg == NULL ? 0
3299 : (evalarg->eval_flags & EVAL_EVALUATE);
3300
3301 // Recognize <type> in Vim9 script only.
3302 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3303 {
3304 ++*arg;
3305 ga_init2(&type_list, sizeof(type_T *), 10);
3306 want_type = parse_type(arg, &type_list, TRUE);
3307 if (want_type == NULL && (evaluate || **arg != '>'))
3308 {
3309 clear_type_list(&type_list);
3310 return FAIL;
3311 }
3312
3313 if (**arg != '>')
3314 {
3315 if (*skipwhite(*arg) == '>')
3316 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3317 else
3318 emsg(_(e_missing_gt));
3319 clear_type_list(&type_list);
3320 return FAIL;
3321 }
3322 ++*arg;
3323 *arg = skipwhite_and_linebreak(*arg, evalarg);
3324 }
3325
3326 res = eval7(arg, rettv, evalarg, want_string);
3327
3328 if (want_type != NULL && evaluate)
3329 {
3330 if (res == OK)
3331 {
3332 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3333
3334 if (!equal_type(want_type, actual))
3335 {
3336 if (want_type == &t_bool && actual != &t_bool
3337 && (actual->tt_flags & TTFLAG_BOOL_OK))
3338 {
3339 int n = tv2bool(rettv);
3340
3341 // can use "0" and "1" for boolean in some places
3342 clear_tv(rettv);
3343 rettv->v_type = VAR_BOOL;
3344 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3345 }
3346 else
3347 {
3348 where_T where;
3349
3350 where.wt_index = 0;
3351 where.wt_variable = TRUE;
3352 res = check_type(want_type, actual, TRUE, where);
3353 }
3354 }
3355 }
3356 clear_type_list(&type_list);
3357 }
3358
3359 return res;
3360}
3361
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003362 int
3363eval_leader(char_u **arg, int vim9)
3364{
3365 char_u *s = *arg;
3366 char_u *p = *arg;
3367
3368 while (*p == '!' || *p == '-' || *p == '+')
3369 {
3370 char_u *n = skipwhite(p + 1);
3371
3372 // ++, --, -+ and +- are not accepted in Vim9 script
3373 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3374 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003375 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003376 return FAIL;
3377 }
3378 p = n;
3379 }
3380 *arg = p;
3381 return OK;
3382}
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384/*
3385 * Handle sixth level expression:
3386 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003387 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003388 * "string" string constant
3389 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * &option-name option value
3391 * @r register contents
3392 * identifier variable value
3393 * function() function call
3394 * $VAR environment variable
3395 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003396 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003398 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003399 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 *
3401 * Also handle:
3402 * ! in front logical NOT
3403 * - in front unary minus
3404 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 * trailing [] subscript in String or List
3406 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003407 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *
3409 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003410 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 *
3412 * Return OK or FAIL.
3413 */
3414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003415eval7(
3416 char_u **arg,
3417 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003418 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003421 int evaluate = evalarg != NULL
3422 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 int len;
3424 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 char_u *start_leader, *end_leader;
3426 int ret = OK;
3427 char_u *alias;
3428
3429 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003431 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003436 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003439 if (eval_leader(arg, in_vim9script()) == FAIL)
3440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 end_leader = *arg;
3442
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003443 if (**arg == '.' && (!isdigit(*(*arg + 1))
3444#ifdef FEAT_FLOAT
3445 || current_sctx.sc_version < 2
3446#endif
3447 ))
3448 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003449 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003450 ++*arg;
3451 return FAIL;
3452 }
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 switch (**arg)
3455 {
3456 /*
3457 * Number constant.
3458 */
3459 case '0':
3460 case '1':
3461 case '2':
3462 case '3':
3463 case '4':
3464 case '5':
3465 case '6':
3466 case '7':
3467 case '8':
3468 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003469 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003470
3471 // Apply prefixed "-" and "+" now. Matters especially when
3472 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003473 if (ret == OK && evaluate && end_leader > start_leader
3474 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003475 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 break;
3477
3478 /*
3479 * String constant: "string".
3480 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003481 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 break;
3483
3484 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003485 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003487 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003488 break;
3489
3490 /*
3491 * List: [expr, expr]
3492 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003493 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495
3496 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003497 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003498 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003499 case '#': if (in_vim9script())
3500 {
3501 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3502 }
3503 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003504 {
3505 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003506 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003507 }
3508 else
3509 ret = NOTDONE;
3510 break;
3511
3512 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003513 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003514 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003516 case '{': if (in_vim9script())
3517 ret = NOTDONE;
3518 else
3519 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003520 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003521 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 break;
3523
3524 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003525 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003527 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 break;
3529
3530 /*
3531 * Environment variable: $VAR.
3532 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003533 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 break;
3535
3536 /*
3537 * Register contents: @r.
3538 */
3539 case '@': ++*arg;
3540 if (evaluate)
3541 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003542 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3543 semsg(_(e_syntax_error_at_str), *arg);
3544 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3545 emsg_invreg(**arg);
3546 else
3547 {
3548 rettv->v_type = VAR_STRING;
3549 rettv->vval.v_string = get_reg_contents(**arg,
3550 GREG_EXPR_SRC);
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553 if (**arg != NUL)
3554 ++*arg;
3555 break;
3556
3557 /*
3558 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003559 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003561 case '(': ret = NOTDONE;
3562 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003563 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003564 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003565 if (ret == OK && evaluate)
3566 {
3567 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3568
Bram Moolenaara9931532021-06-12 15:58:16 +02003569 // Compile it here to get the return type. The return
3570 // type is optional, when it's missing use t_unknown.
3571 // This is recognized in compile_return().
3572 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3573 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003574 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003575 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003576 {
3577 clear_tv(rettv);
3578 ret = FAIL;
3579 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003580 }
3581 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003582 if (ret == NOTDONE)
3583 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003584 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003585 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003586
Bram Moolenaar9215f012020-06-27 21:18:00 +02003587 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003588 if (**arg == ')')
3589 ++*arg;
3590 else if (ret == OK)
3591 {
3592 emsg(_(e_missing_close));
3593 clear_tv(rettv);
3594 ret = FAIL;
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597 break;
3598
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 break;
3601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602
3603 if (ret == NOTDONE)
3604 {
3605 /*
3606 * Must be a variable or function name.
3607 * Can also be a curly-braces kind of name: {expr}.
3608 */
3609 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003610 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 if (alias != NULL)
3612 s = alias;
3613
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003614 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 ret = FAIL;
3616 else
3617 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003618 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3619
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003620 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003621 {
3622 emsg(_(e_cannot_use_underscore_here));
3623 ret = FAIL;
3624 }
3625 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003626 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003627 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003628 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003629 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003630 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003631 else if (flags & EVAL_CONSTANT)
3632 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003634 {
3635 // get the value of "true", "false" or a variable
3636 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3637 {
3638 rettv->v_type = VAR_BOOL;
3639 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003640 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003641 }
3642 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003643 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003644 {
3645 rettv->v_type = VAR_BOOL;
3646 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003647 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003648 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003649 else if (len == 4 && in_vim9script()
3650 && STRNCMP(s, "null", 4) == 0)
3651 {
3652 rettv->v_type = VAR_SPECIAL;
3653 rettv->vval.v_number = VVAL_NULL;
3654 ret = OK;
3655 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003656 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003657 ret = eval_variable(s, len, rettv, NULL,
3658 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003659 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003660 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003661 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003662 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003663 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003664 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003667 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 }
3669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003670 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3671 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003672 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003673 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 /*
3676 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3677 */
3678 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003679 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003680 return ret;
3681}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003683/*
3684 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003685 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003686 * Adjusts "end_leaderp" until it is at "start_leader".
3687 */
3688 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003689eval7_leader(
3690 typval_T *rettv,
3691 int numeric_only,
3692 char_u *start_leader,
3693 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003694{
3695 char_u *end_leader = *end_leaderp;
3696 int ret = OK;
3697 int error = FALSE;
3698 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003699 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003700#ifdef FEAT_FLOAT
3701 float_T f = 0.0;
3702
3703 if (rettv->v_type == VAR_FLOAT)
3704 f = rettv->vval.v_float;
3705 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003706#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003707 {
3708 while (VIM_ISWHITE(end_leader[-1]))
3709 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003710 if (in_vim9script() && end_leader[-1] == '!')
3711 val = tv2bool(rettv);
3712 else
3713 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003714 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003715 if (error)
3716 {
3717 clear_tv(rettv);
3718 ret = FAIL;
3719 }
3720 else
3721 {
3722 while (end_leader > start_leader)
3723 {
3724 --end_leader;
3725 if (*end_leader == '!')
3726 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003727 if (numeric_only)
3728 {
3729 ++end_leader;
3730 break;
3731 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003732#ifdef FEAT_FLOAT
3733 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003734 {
3735 if (in_vim9script())
3736 {
3737 rettv->v_type = VAR_BOOL;
3738 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3739 }
3740 else
3741 f = !f;
3742 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003743 else
3744#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003745 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003746 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003747 type = VAR_BOOL;
3748 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003749 }
3750 else if (*end_leader == '-')
3751 {
3752#ifdef FEAT_FLOAT
3753 if (rettv->v_type == VAR_FLOAT)
3754 f = -f;
3755 else
3756#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003757 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003759 type = VAR_NUMBER;
3760 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761 }
3762 }
3763#ifdef FEAT_FLOAT
3764 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003767 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003769 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003770#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003771 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003773 if (in_vim9script())
3774 rettv->v_type = type;
3775 else
3776 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003777 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003780 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 return ret;
3782}
3783
3784/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003785 * Call the function referred to in "rettv".
3786 */
3787 static int
3788call_func_rettv(
3789 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003790 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003791 typval_T *rettv,
3792 int evaluate,
3793 dict_T *selfdict,
3794 typval_T *basetv)
3795{
3796 partial_T *pt = NULL;
3797 funcexe_T funcexe;
3798 typval_T functv;
3799 char_u *s;
3800 int ret;
3801
3802 // need to copy the funcref so that we can clear rettv
3803 if (evaluate)
3804 {
3805 functv = *rettv;
3806 rettv->v_type = VAR_UNKNOWN;
3807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003808 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809 if (functv.v_type == VAR_PARTIAL)
3810 {
3811 pt = functv.vval.v_partial;
3812 s = partial_name(pt);
3813 }
3814 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003815 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003816 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003817 if (s == NULL || *s == NUL)
3818 {
3819 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003820 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003821 goto theend;
3822 }
3823 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003824 }
3825 else
3826 s = (char_u *)"";
3827
Bram Moolenaara80faa82020-04-12 19:37:17 +02003828 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003829 funcexe.firstline = curwin->w_cursor.lnum;
3830 funcexe.lastline = curwin->w_cursor.lnum;
3831 funcexe.evaluate = evaluate;
3832 funcexe.partial = pt;
3833 funcexe.selfdict = selfdict;
3834 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003835 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003836
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003837theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003838 // Clear the funcref afterwards, so that deleting it while
3839 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003840 if (evaluate)
3841 clear_tv(&functv);
3842
3843 return ret;
3844}
3845
3846/*
3847 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003848 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003849 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3850 */
3851 static int
3852eval_lambda(
3853 char_u **arg,
3854 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003855 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003856 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003857{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003858 int evaluate = evalarg != NULL
3859 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003860 typval_T base = *rettv;
3861 int ret;
3862
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003863 rettv->v_type = VAR_UNKNOWN;
3864
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003865 if (**arg == '{')
3866 {
3867 // ->{lambda}()
3868 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3869 }
3870 else
3871 {
3872 // ->(lambda)()
3873 ++*arg;
3874 ret = eval1(arg, rettv, evalarg);
3875 *arg = skipwhite_and_linebreak(*arg, evalarg);
3876 if (**arg != ')')
3877 {
3878 emsg(_(e_missing_close));
3879 ret = FAIL;
3880 }
3881 ++*arg;
3882 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003883 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003884 return FAIL;
3885 else if (**arg != '(')
3886 {
3887 if (verbose)
3888 {
3889 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003890 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003891 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003893 }
3894 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003895 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003896 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003897 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003898 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003899
3900 // Clear the funcref afterwards, so that deleting it while
3901 // evaluating the arguments is possible (see test55).
3902 if (evaluate)
3903 clear_tv(&base);
3904
3905 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003906}
3907
3908/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003909 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003910 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003911 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3912 */
3913 static int
3914eval_method(
3915 char_u **arg,
3916 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003917 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003918 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003919{
3920 char_u *name;
3921 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003922 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003923 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003924 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003925 int evaluate = evalarg != NULL
3926 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003927
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003928 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929
Bram Moolenaarac92e252019-08-03 21:58:38 +02003930 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003931 len = get_name_len(arg, &alias, evaluate, TRUE);
3932 if (alias != NULL)
3933 name = alias;
3934
3935 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003936 {
3937 if (verbose)
3938 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003939 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003940 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003941 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003942 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003943 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003944 if (**arg != '(')
3945 {
3946 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003948 ret = FAIL;
3949 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003950 else if (VIM_ISWHITE((*arg)[-1]))
3951 {
3952 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003953 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003954 ret = FAIL;
3955 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003956 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003957 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003958 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003959 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003960
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003961 // Clear the funcref afterwards, so that deleting it while
3962 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003963 if (evaluate)
3964 clear_tv(&base);
3965
Bram Moolenaarac92e252019-08-03 21:58:38 +02003966 return ret;
3967}
3968
3969/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003970 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3971 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003972 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3973 */
3974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975eval_index(
3976 char_u **arg,
3977 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003978 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003979 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003981 int evaluate = evalarg != NULL
3982 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003983 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003985 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003986 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003987 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003988 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003989
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003990 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3991 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003992
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003993 init_tv(&var1);
3994 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003995 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003996 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 /*
3998 * dict.name
3999 */
4000 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004001 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004002 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004003 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004005 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004006 }
4007 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 /*
4010 * something[idx]
4011 *
4012 * Get the (first) variable from inside the [].
4013 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004014 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004015 if (**arg == ':')
4016 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004017 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004019 else if (vim9 && **arg == ':')
4020 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004021 semsg(_(e_white_space_required_before_and_after_str_at_str),
4022 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004023 clear_tv(&var1);
4024 return FAIL;
4025 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004026 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004028#ifdef FEAT_FLOAT
4029 // allow for indexing with float
4030 if (vim9 && rettv->v_type == VAR_DICT
4031 && var1.v_type == VAR_FLOAT)
4032 {
4033 var1.vval.v_string = typval_tostring(&var1, TRUE);
4034 var1.v_type = VAR_STRING;
4035 }
4036#endif
4037 if (tv_get_string_chk(&var1) == NULL)
4038 {
4039 // not a number or string
4040 clear_tv(&var1);
4041 return FAIL;
4042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044
4045 /*
4046 * Get the second variable from inside the [:].
4047 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004048 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049 if (**arg == ':')
4050 {
4051 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004052 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004053 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004054 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004055 semsg(_(e_white_space_required_before_and_after_str_at_str),
4056 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004057 if (!empty1)
4058 clear_tv(&var1);
4059 return FAIL;
4060 }
4061 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004062 if (**arg == ']')
4063 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004064 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 if (!empty1)
4067 clear_tv(&var1);
4068 return FAIL;
4069 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004070 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004072 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073 if (!empty1)
4074 clear_tv(&var1);
4075 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 return FAIL;
4077 }
4078 }
4079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004081 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004082 if (**arg != ']')
4083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004084 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004086 clear_tv(&var1);
4087 if (range)
4088 clear_tv(&var2);
4089 return FAIL;
4090 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004091 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004092 }
4093
4094 if (evaluate)
4095 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004096 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004097 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004098 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004099
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004100 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004102 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004103 clear_tv(&var2);
4104 return res;
4105 }
4106 return OK;
4107}
4108
4109/*
4110 * Check if "rettv" can have an [index] or [sli:ce]
4111 */
4112 int
4113check_can_index(typval_T *rettv, int evaluate, int verbose)
4114{
4115 switch (rettv->v_type)
4116 {
4117 case VAR_FUNC:
4118 case VAR_PARTIAL:
4119 if (verbose)
4120 emsg(_("E695: Cannot index a Funcref"));
4121 return FAIL;
4122 case VAR_FLOAT:
4123#ifdef FEAT_FLOAT
4124 if (verbose)
4125 emsg(_(e_float_as_string));
4126 return FAIL;
4127#endif
4128 case VAR_BOOL:
4129 case VAR_SPECIAL:
4130 case VAR_JOB:
4131 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004132 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004133 if (verbose)
4134 emsg(_(e_cannot_index_special_variable));
4135 return FAIL;
4136 case VAR_UNKNOWN:
4137 case VAR_ANY:
4138 case VAR_VOID:
4139 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004140 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004141 emsg(_(e_cannot_index_special_variable));
4142 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 case VAR_STRING:
4147 case VAR_LIST:
4148 case VAR_DICT:
4149 case VAR_BLOB:
4150 break;
4151 case VAR_NUMBER:
4152 if (in_vim9script())
4153 emsg(_(e_cannot_index_number));
4154 break;
4155 }
4156 return OK;
4157}
4158
4159/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004160 * slice() function
4161 */
4162 void
4163f_slice(typval_T *argvars, typval_T *rettv)
4164{
4165 if (check_can_index(argvars, TRUE, FALSE) == OK)
4166 {
4167 copy_tv(argvars, rettv);
4168 eval_index_inner(rettv, TRUE, argvars + 1,
4169 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4170 TRUE, NULL, 0, FALSE);
4171 }
4172}
4173
4174/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 * Apply index or range to "rettv".
4176 * "var1" is the first index, NULL for [:expr].
4177 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004178 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4179 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004180 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4181 */
4182 int
4183eval_index_inner(
4184 typval_T *rettv,
4185 int is_range,
4186 typval_T *var1,
4187 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 char_u *key,
4190 int keylen,
4191 int verbose)
4192{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004193 varnumber_T n1, n2 = 0;
4194 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004195
4196 n1 = 0;
4197 if (var1 != NULL && rettv->v_type != VAR_DICT)
4198 n1 = tv_get_number(var1);
4199
4200 if (is_range)
4201 {
4202 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004204 if (verbose)
4205 emsg(_(e_cannot_slice_dictionary));
4206 return FAIL;
4207 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004208 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004210 else
4211 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004212 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004213
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004214 switch (rettv->v_type)
4215 {
4216 case VAR_UNKNOWN:
4217 case VAR_ANY:
4218 case VAR_VOID:
4219 case VAR_FUNC:
4220 case VAR_PARTIAL:
4221 case VAR_FLOAT:
4222 case VAR_BOOL:
4223 case VAR_SPECIAL:
4224 case VAR_JOB:
4225 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004226 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004227 break; // not evaluating, skipping over subscript
4228
4229 case VAR_NUMBER:
4230 case VAR_STRING:
4231 {
4232 char_u *s = tv_get_string(rettv);
4233
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004235 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004236 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004238 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004239 else
4240 s = char_from_string(s, n1);
4241 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // The resulting variable is a substring. If the indexes
4245 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246 if (n1 < 0)
4247 {
4248 n1 = len + n1;
4249 if (n1 < 0)
4250 n1 = 0;
4251 }
4252 if (n2 < 0)
4253 n2 = len + n2;
4254 else if (n2 >= len)
4255 n2 = len;
4256 if (n1 >= len || n2 < 0 || n1 > n2)
4257 s = NULL;
4258 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004259 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004260 }
4261 else
4262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004263 // The resulting variable is a string of a single
4264 // character. If the index is too big or negative the
4265 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 if (n1 >= len || n1 < 0)
4267 s = NULL;
4268 else
4269 s = vim_strnsave(s + n1, 1);
4270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(rettv);
4272 rettv->v_type = VAR_STRING;
4273 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004274 }
4275 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004276
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004277 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004278 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4279 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004280 break;
4281
4282 case VAR_LIST:
4283 if (var1 == NULL)
4284 n1 = 0;
4285 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004286 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004287 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004288 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004289 return FAIL;
4290 break;
4291
4292 case VAR_DICT:
4293 {
4294 dictitem_T *item;
4295 typval_T tmp;
4296
4297 if (key == NULL)
4298 {
4299 key = tv_get_string_chk(var1);
4300 if (key == NULL)
4301 return FAIL;
4302 }
4303
4304 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4305
4306 if (item == NULL && verbose)
4307 semsg(_(e_dictkey), key);
4308 if (item == NULL)
4309 return FAIL;
4310
4311 copy_tv(&item->di_tv, &tmp);
4312 clear_tv(rettv);
4313 *rettv = tmp;
4314 }
4315 break;
4316 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004317 return OK;
4318}
4319
4320/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004321 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 */
4323 char_u *
4324partial_name(partial_T *pt)
4325{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004326 if (pt != NULL)
4327 {
4328 if (pt->pt_name != NULL)
4329 return pt->pt_name;
4330 if (pt->pt_func != NULL)
4331 return pt->pt_func->uf_name;
4332 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004333 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004334}
4335
Bram Moolenaarddecc252016-04-06 22:59:37 +02004336 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004337partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004338{
4339 int i;
4340
4341 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004342 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004343 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004344 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004345 if (pt->pt_name != NULL)
4346 {
4347 func_unref(pt->pt_name);
4348 vim_free(pt->pt_name);
4349 }
4350 else
4351 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004352
Bram Moolenaar54656012021-06-09 20:50:46 +02004353 // "out_up" is no longer used, decrement refcount on partial that owns it.
4354 partial_unref(pt->pt_outer.out_up_partial);
4355
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004356 // Decrease the reference count for the context of a closure. If down
4357 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004358 if (pt->pt_funcstack != NULL)
4359 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004360 --pt->pt_funcstack->fs_refcount;
4361 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004362 }
4363
Bram Moolenaarddecc252016-04-06 22:59:37 +02004364 vim_free(pt);
4365}
4366
4367/*
4368 * Unreference a closure: decrement the reference count and free it when it
4369 * becomes zero.
4370 */
4371 void
4372partial_unref(partial_T *pt)
4373{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004374 if (pt != NULL)
4375 {
4376 if (--pt->pt_refcount <= 0)
4377 partial_free(pt);
4378
4379 // If the reference count goes down to one, the funcstack may be the
4380 // only reference and can be freed if no other partials reference it.
4381 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4382 funcstack_check_refcount(pt->pt_funcstack);
4383 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004384}
4385
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004387 * Return the next (unique) copy ID.
4388 * Used for serializing nested structures.
4389 */
4390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004391get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004392{
4393 current_copyID += COPYID_INC;
4394 return current_copyID;
4395}
4396
4397/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004398 * Garbage collection for lists and dictionaries.
4399 *
4400 * We use reference counts to be able to free most items right away when they
4401 * are no longer used. But for composite items it's possible that it becomes
4402 * unused while the reference count is > 0: When there is a recursive
4403 * reference. Example:
4404 * :let l = [1, 2, 3]
4405 * :let d = {9: l}
4406 * :let l[1] = d
4407 *
4408 * Since this is quite unusual we handle this with garbage collection: every
4409 * once in a while find out which lists and dicts are not referenced from any
4410 * variable.
4411 *
4412 * Here is a good reference text about garbage collection (refers to Python
4413 * but it applies to all reference-counting mechanisms):
4414 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004415 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004416
4417/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004418 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004419 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004420 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004421 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004422 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004423garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004424{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004425 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004427 buf_T *buf;
4428 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004429 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004430 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004431
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004432 if (!testing)
4433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004434 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004435 want_garbage_collect = FALSE;
4436 may_garbage_collect = FALSE;
4437 garbage_collect_at_exit = FALSE;
4438 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004439
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004440 // The execution stack can grow big, limit the size.
4441 if (exestack.ga_maxlen - exestack.ga_len > 500)
4442 {
4443 size_t new_len;
4444 char_u *pp;
4445 int n;
4446
4447 // Keep 150% of the current size, with a minimum of the growth size.
4448 n = exestack.ga_len / 2;
4449 if (n < exestack.ga_growsize)
4450 n = exestack.ga_growsize;
4451
4452 // Don't make it bigger though.
4453 if (exestack.ga_len + n < exestack.ga_maxlen)
4454 {
4455 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4456 pp = vim_realloc(exestack.ga_data, new_len);
4457 if (pp == NULL)
4458 return FAIL;
4459 exestack.ga_maxlen = exestack.ga_len + n;
4460 exestack.ga_data = pp;
4461 }
4462 }
4463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // We advance by two because we add one for items referenced through
4465 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004466 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004467
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004468 /*
4469 * 1. Go through all accessible variables and mark all lists and dicts
4470 * with copyID.
4471 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // Don't free variables in the previous_funccal list unless they are only
4474 // referenced through previous_funccal. This must be first, because if
4475 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004479 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004482 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004483 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4484 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004487 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004488 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4489 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004490 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004491 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4492 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004493#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004494 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004495 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4496 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004497 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004498 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004499 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4500 NULL, NULL);
4501#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004503 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004504 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004505 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4506 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004507 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004508 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004514 abort = abort || set_ref_in_functions(copyID);
4515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004517 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004520 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004521
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004522 // callbacks in buffers
4523 abort = abort || set_ref_in_buffers(copyID);
4524
Bram Moolenaar1dced572012-04-05 16:54:08 +02004525#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004527#endif
4528
Bram Moolenaardb913952012-06-29 12:54:53 +02004529#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004531#endif
4532
4533#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004534 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004535#endif
4536
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004538 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004539 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004540#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004541#ifdef FEAT_NETBEANS_INTG
4542 abort = abort || set_ref_in_nb_channel(copyID);
4543#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004544
Bram Moolenaare3188e22016-05-31 21:13:04 +02004545#ifdef FEAT_TIMERS
4546 abort = abort || set_ref_in_timer(copyID);
4547#endif
4548
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004549#ifdef FEAT_QUICKFIX
4550 abort = abort || set_ref_in_quickfix(copyID);
4551#endif
4552
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004553#ifdef FEAT_TERMINAL
4554 abort = abort || set_ref_in_term(copyID);
4555#endif
4556
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004557#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004558 abort = abort || set_ref_in_popups(copyID);
4559#endif
4560
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004561 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004562 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004563 /*
4564 * 2. Free lists and dictionaries that are not referenced.
4565 */
4566 did_free = free_unref_items(copyID);
4567
4568 /*
4569 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004570 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004572 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004573 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 else if (p_verbose > 0)
4575 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004576 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004577 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004578
4579 return did_free;
4580}
4581
4582/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004583 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004584 */
4585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004586free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004587{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004588 int did_free = FALSE;
4589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 // Let all "free" functions know that we are here. This means no
4591 // dictionaries, lists, channels or jobs are to be freed, because we will
4592 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004593 in_free_unref_items = TRUE;
4594
4595 /*
4596 * PASS 1: free the contents of the items. We don't free the items
4597 * themselves yet, so that it is possible to decrement refcount counters
4598 */
4599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004600 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004601 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004604 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605
4606#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // Go through the list of jobs and free items without the copyID. This
4608 // must happen before doing channels, because jobs refer to channels, but
4609 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004610 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004612 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004613 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4614#endif
4615
4616 /*
4617 * PASS 2: free the items themselves.
4618 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004619 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004620 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004621
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004622#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // Go through the list of jobs and free items without the copyID. This
4624 // must happen before doing channels, because jobs refer to channels, but
4625 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004626 free_unused_jobs(copyID, COPYID_MASK);
4627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004629 free_unused_channels(copyID, COPYID_MASK);
4630#endif
4631
4632 in_free_unref_items = FALSE;
4633
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004634 return did_free;
4635}
4636
4637/*
4638 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004639 * "list_stack" is used to add lists to be marked. Can be NULL.
4640 *
4641 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004642 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004645{
4646 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004647 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004649 hashtab_T *cur_ht;
4650 ht_stack_T *ht_stack = NULL;
4651 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004652
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004653 cur_ht = ht;
4654 for (;;)
4655 {
4656 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 // Mark each item in the hashtab. If the item contains a hashtab
4659 // it is added to ht_stack, if it contains a list it is added to
4660 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004661 todo = (int)cur_ht->ht_used;
4662 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4663 if (!HASHITEM_EMPTY(hi))
4664 {
4665 --todo;
4666 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4667 &ht_stack, list_stack);
4668 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004669 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670
4671 if (ht_stack == NULL)
4672 break;
4673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004675 cur_ht = ht_stack->ht;
4676 tempitem = ht_stack;
4677 ht_stack = ht_stack->prev;
4678 free(tempitem);
4679 }
4680
4681 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004682}
4683
4684/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004685 * Mark a dict and its items with "copyID".
4686 * Returns TRUE if setting references failed somehow.
4687 */
4688 int
4689set_ref_in_dict(dict_T *d, int copyID)
4690{
4691 if (d != NULL && d->dv_copyID != copyID)
4692 {
4693 d->dv_copyID = copyID;
4694 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4695 }
4696 return FALSE;
4697}
4698
4699/*
4700 * Mark a list and its items with "copyID".
4701 * Returns TRUE if setting references failed somehow.
4702 */
4703 int
4704set_ref_in_list(list_T *ll, int copyID)
4705{
4706 if (ll != NULL && ll->lv_copyID != copyID)
4707 {
4708 ll->lv_copyID = copyID;
4709 return set_ref_in_list_items(ll, copyID, NULL);
4710 }
4711 return FALSE;
4712}
4713
4714/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004715 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004716 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4717 *
4718 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004719 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004720 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004721set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004722{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004723 listitem_T *li;
4724 int abort = FALSE;
4725 list_T *cur_l;
4726 list_stack_T *list_stack = NULL;
4727 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004728
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004729 cur_l = l;
4730 for (;;)
4731 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004732 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004733 // Mark each item in the list. If the item contains a hashtab
4734 // it is added to ht_stack, if it contains a list it is added to
4735 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004736 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4737 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4738 ht_stack, &list_stack);
4739 if (list_stack == NULL)
4740 break;
4741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004743 cur_l = list_stack->list;
4744 tempitem = list_stack;
4745 list_stack = list_stack->prev;
4746 free(tempitem);
4747 }
4748
4749 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004750}
4751
4752/*
4753 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 * "list_stack" is used to add lists to be marked. Can be NULL.
4755 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4756 *
4757 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004758 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004760set_ref_in_item(
4761 typval_T *tv,
4762 int copyID,
4763 ht_stack_T **ht_stack,
4764 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004765{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004766 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004767
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004769 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004770 dict_T *dd = tv->vval.v_dict;
4771
Bram Moolenaara03f2332016-02-06 18:09:59 +01004772 if (dd != NULL && dd->dv_copyID != copyID)
4773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004774 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004775 dd->dv_copyID = copyID;
4776 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004777 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004778 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4779 }
4780 else
4781 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004782 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4783
Bram Moolenaara03f2332016-02-06 18:09:59 +01004784 if (newitem == NULL)
4785 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004786 else
4787 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004788 newitem->ht = &dd->dv_hashtab;
4789 newitem->prev = *ht_stack;
4790 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004791 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004792 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004793 }
4794 }
4795 else if (tv->v_type == VAR_LIST)
4796 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 list_T *ll = tv->vval.v_list;
4798
Bram Moolenaara03f2332016-02-06 18:09:59 +01004799 if (ll != NULL && ll->lv_copyID != copyID)
4800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004802 ll->lv_copyID = copyID;
4803 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004804 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004805 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004806 }
4807 else
4808 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004809 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4810
Bram Moolenaara03f2332016-02-06 18:09:59 +01004811 if (newitem == NULL)
4812 abort = TRUE;
4813 else
4814 {
4815 newitem->list = ll;
4816 newitem->prev = *list_stack;
4817 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004819 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004821 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004822 else if (tv->v_type == VAR_FUNC)
4823 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004824 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004825 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004826 else if (tv->v_type == VAR_PARTIAL)
4827 {
4828 partial_T *pt = tv->vval.v_partial;
4829 int i;
4830
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004831 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004832 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004833 // Didn't see this partial yet.
4834 pt->pt_copyID = copyID;
4835
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004836 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004837
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 if (pt->pt_dict != NULL)
4839 {
4840 typval_T dtv;
4841
4842 dtv.v_type = VAR_DICT;
4843 dtv.vval.v_dict = pt->pt_dict;
4844 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4845 }
4846
4847 for (i = 0; i < pt->pt_argc; ++i)
4848 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4849 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004850 if (pt->pt_funcstack != NULL)
4851 {
4852 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4853
4854 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4855 abort = abort || set_ref_in_item(stack + i, copyID,
4856 ht_stack, list_stack);
4857 }
4858
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004859 }
4860 }
4861#ifdef FEAT_JOB_CHANNEL
4862 else if (tv->v_type == VAR_JOB)
4863 {
4864 job_T *job = tv->vval.v_job;
4865 typval_T dtv;
4866
4867 if (job != NULL && job->jv_copyID != copyID)
4868 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004869 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 if (job->jv_channel != NULL)
4871 {
4872 dtv.v_type = VAR_CHANNEL;
4873 dtv.vval.v_channel = job->jv_channel;
4874 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4875 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004876 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004877 {
4878 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004879 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4881 }
4882 }
4883 }
4884 else if (tv->v_type == VAR_CHANNEL)
4885 {
4886 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004887 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004888 typval_T dtv;
4889 jsonq_T *jq;
4890 cbq_T *cq;
4891
4892 if (ch != NULL && ch->ch_copyID != copyID)
4893 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004894 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004895 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004896 {
4897 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4898 jq = jq->jq_next)
4899 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4900 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4901 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004902 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004903 {
4904 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004905 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004906 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4907 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004908 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004909 {
4910 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004911 dtv.vval.v_partial =
4912 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004913 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4914 }
4915 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004916 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004917 {
4918 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004919 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4921 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004922 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004923 {
4924 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004925 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004926 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4927 }
4928 }
4929 }
4930#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004931 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004932}
4933
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 * Return a string with the string representation of a variable.
4936 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004937 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004938 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004939 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004940 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004941 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004942 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4943 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004944 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004945 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004946 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004947echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948 typval_T *tv,
4949 char_u **tofree,
4950 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004951 int copyID,
4952 int echo_style,
4953 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004954 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004955{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004956 static int recurse = 0;
4957 char_u *r = NULL;
4958
Bram Moolenaar33570922005-01-25 22:26:29 +00004959 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004960 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004961 if (!did_echo_string_emsg)
4962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // Only give this message once for a recursive call to avoid
4964 // flooding the user with errors. And stop iterating over lists
4965 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004966 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004967 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004969 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004970 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 }
4972 ++recurse;
4973
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004974 switch (tv->v_type)
4975 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004976 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004977 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004978 {
4979 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004980 r = tv->vval.v_string;
4981 if (r == NULL)
4982 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004983 }
4984 else
4985 {
4986 *tofree = string_quote(tv->vval.v_string, FALSE);
4987 r = *tofree;
4988 }
4989 break;
4990
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004992 if (echo_style)
4993 {
4994 *tofree = NULL;
4995 r = tv->vval.v_string;
4996 }
4997 else
4998 {
4999 *tofree = string_quote(tv->vval.v_string, TRUE);
5000 r = *tofree;
5001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005002 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005003
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005004 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005005 {
5006 partial_T *pt = tv->vval.v_partial;
5007 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005008 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005009 garray_T ga;
5010 int i;
5011 char_u *tf;
5012
5013 ga_init2(&ga, 1, 100);
5014 ga_concat(&ga, (char_u *)"function(");
5015 if (fname != NULL)
5016 {
5017 ga_concat(&ga, fname);
5018 vim_free(fname);
5019 }
5020 if (pt != NULL && pt->pt_argc > 0)
5021 {
5022 ga_concat(&ga, (char_u *)", [");
5023 for (i = 0; i < pt->pt_argc; ++i)
5024 {
5025 if (i > 0)
5026 ga_concat(&ga, (char_u *)", ");
5027 ga_concat(&ga,
5028 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5029 vim_free(tf);
5030 }
5031 ga_concat(&ga, (char_u *)"]");
5032 }
5033 if (pt != NULL && pt->pt_dict != NULL)
5034 {
5035 typval_T dtv;
5036
5037 ga_concat(&ga, (char_u *)", ");
5038 dtv.v_type = VAR_DICT;
5039 dtv.vval.v_dict = pt->pt_dict;
5040 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5041 vim_free(tf);
5042 }
5043 ga_concat(&ga, (char_u *)")");
5044
5045 *tofree = ga.ga_data;
5046 r = *tofree;
5047 break;
5048 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005049
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005050 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005051 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005052 break;
5053
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005054 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005055 if (tv->vval.v_list == NULL)
5056 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005057 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005059 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005060 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005061 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5062 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005063 {
5064 *tofree = NULL;
5065 r = (char_u *)"[...]";
5066 }
5067 else
5068 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005069 int old_copyID = tv->vval.v_list->lv_copyID;
5070
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005071 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005072 *tofree = list2string(tv, copyID, restore_copyID);
5073 if (restore_copyID)
5074 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005075 r = *tofree;
5076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005080 if (tv->vval.v_dict == NULL)
5081 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005082 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005083 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005084 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005085 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005086 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5087 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005088 {
5089 *tofree = NULL;
5090 r = (char_u *)"{...}";
5091 }
5092 else
5093 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005094 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005095
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005096 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005097 *tofree = dict2string(tv, copyID, restore_copyID);
5098 if (restore_copyID)
5099 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005100 r = *tofree;
5101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005105 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005106 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005108 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005110 break;
5111
Bram Moolenaar835dc632016-02-07 14:27:38 +01005112 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005113 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005114#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005116 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5117 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005118 if (composite_val)
5119 {
5120 *tofree = string_quote(r, FALSE);
5121 r = *tofree;
5122 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005123#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005125
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005126 case VAR_INSTR:
5127 *tofree = NULL;
5128 r = (char_u *)"instructions";
5129 break;
5130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005132#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 *tofree = NULL;
5134 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5135 r = numbuf;
5136 break;
5137#endif
5138
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005139 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005140 case VAR_SPECIAL:
5141 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005142 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005143 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145
Bram Moolenaar8502c702014-06-17 12:51:16 +02005146 if (--recurse == 0)
5147 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005148 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149}
5150
5151/*
5152 * Return a string with the string representation of a variable.
5153 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5154 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005155 * Does not put quotes around strings, as ":echo" displays values.
5156 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5157 * May return NULL.
5158 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005159 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005160echo_string(
5161 typval_T *tv,
5162 char_u **tofree,
5163 char_u *numbuf,
5164 int copyID)
5165{
5166 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5167}
5168
5169/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005170 * Return string "str" in ' quotes, doubling ' characters.
5171 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005174 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005175string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005176{
Bram Moolenaar33570922005-01-25 22:26:29 +00005177 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 char_u *p, *r, *s;
5179
Bram Moolenaar33570922005-01-25 22:26:29 +00005180 len = (function ? 13 : 3);
5181 if (str != NULL)
5182 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005183 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005184 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 if (*p == '\'')
5186 ++len;
5187 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005188 s = r = alloc(len);
5189 if (r != NULL)
5190 {
5191 if (function)
5192 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005194 r += 10;
5195 }
5196 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005198 if (str != NULL)
5199 for (p = str; *p != NUL; )
5200 {
5201 if (*p == '\'')
5202 *r++ = '\'';
5203 MB_COPY_CHAR(p, r);
5204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005206 if (function)
5207 *r++ = ')';
5208 *r++ = NUL;
5209 }
5210 return s;
5211}
5212
5213/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5215 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005216 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005217 */
5218 int
5219buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5220{
5221 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005222 char_u *t;
5223 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005224
5225 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5226 return -1;
5227
5228 if (lnum > buf->b_ml.ml_line_count)
5229 lnum = buf->b_ml.ml_line_count;
5230
5231 str = ml_get_buf(buf, lnum, FALSE);
5232 if (str == NULL)
5233 return -1;
5234
5235 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005236 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237
Bram Moolenaar91458462021-01-13 20:08:38 +01005238 // count the number of characters
5239 t = str;
5240 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5241 t += mb_ptr2len(t);
5242
5243 // In insert mode, when the cursor is at the end of a non-empty line,
5244 // byteidx points to the NUL character immediately past the end of the
5245 // string. In this case, add one to the character count.
5246 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5247 count++;
5248
5249 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005250}
5251
5252/*
5253 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 * byte index. Works only for loaded buffers. Returns -1 on failure.
5255 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 */
5257 int
5258buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5259{
5260 char_u *str;
5261 char_u *t;
5262
5263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5264 return -1;
5265
5266 if (lnum > buf->b_ml.ml_line_count)
5267 lnum = buf->b_ml.ml_line_count;
5268
5269 str = ml_get_buf(buf, lnum, FALSE);
5270 if (str == NULL)
5271 return -1;
5272
5273 // Convert the character offset to a byte offset
5274 t = str;
5275 while (*t != NUL && --charidx > 0)
5276 t += mb_ptr2len(t);
5277
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005279}
5280
5281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005283 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286var2fpos(
5287 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 int *fnum, // set to fnum for '0, 'A, etc.
5290 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005292 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005294 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005296 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005297 if (varp->v_type == VAR_LIST)
5298 {
5299 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005300 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005301 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005302 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005303
5304 l = varp->vval.v_list;
5305 if (l == NULL)
5306 return NULL;
5307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005308 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005309 pos.lnum = list_find_nr(l, 0L, &error);
5310 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005312 if (charcol)
5313 len = (long)mb_charlen(ml_get(pos.lnum));
5314 else
5315 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005316
Bram Moolenaarec65d772020-08-20 22:29:12 +02005317 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005319 li = list_find(l, 1L);
5320 if (li != NULL && li->li_tv.v_type == VAR_STRING
5321 && li->li_tv.vval.v_string != NULL
5322 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005323 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005324 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005325 }
5326 else
5327 {
5328 pos.col = list_find_nr(l, 1L, &error);
5329 if (error)
5330 return NULL;
5331 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005334 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005336 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005339 pos.coladd = list_find_nr(l, 2L, &error);
5340 if (error)
5341 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005343 return &pos;
5344 }
5345
Bram Moolenaarc5809432021-03-27 21:23:30 +01005346 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5347 return NULL;
5348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005349 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 if (name == NULL)
5351 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005353 {
5354 pos = curwin->w_cursor;
5355 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005356 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 return &pos;
5358 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005360 {
5361 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 pos = VIsual;
5363 else
5364 pos = curwin->w_cursor;
5365 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005366 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005367 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005368 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005371 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5373 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005374 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005375 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 return pp;
5377 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005378
Bram Moolenaara5525202006-03-02 22:52:09 +00005379 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005380
Bram Moolenaar477933c2007-07-17 14:32:23 +00005381 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005382 {
5383 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 // In silent Ex mode topline is zero, but that's not a valid line
5388 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005389 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005390 return &pos;
5391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005393 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005394 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005396 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005397 return &pos;
5398 }
5399 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005402 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404 pos.lnum = curbuf->b_ml.ml_line_count;
5405 pos.col = 0;
5406 }
5407 else
5408 {
5409 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005410 if (charcol)
5411 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5412 else
5413 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 }
5415 return &pos;
5416 }
5417 return NULL;
5418}
5419
5420/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005421 * Convert list in "arg" into a position and optional file number.
5422 * When "fnump" is NULL there is no file number, only 3 items.
5423 * Note that the column is passed on as-is, the caller may want to decrement
5424 * it to use 1 for the first column.
5425 * Return FAIL when conversion is not possible, doesn't check the position for
5426 * validity.
5427 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429list2fpos(
5430 typval_T *arg,
5431 pos_T *posp,
5432 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005433 colnr_T *curswantp,
5434 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005435{
5436 list_T *l = arg->vval.v_list;
5437 long i = 0;
5438 long n;
5439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5441 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005442 if (arg->v_type != VAR_LIST
5443 || l == NULL
5444 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005445 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005446 return FAIL;
5447
5448 if (fnump != NULL)
5449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451 if (n < 0)
5452 return FAIL;
5453 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005455 *fnump = n;
5456 }
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005459 if (n < 0)
5460 return FAIL;
5461 posp->lnum = n;
5462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 if (n < 0)
5465 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005466 // If character position is specified, then convert to byte position
5467 if (charcol)
5468 {
5469 buf_T *buf;
5470
5471 // Get the text for the specified line in a loaded buffer
5472 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5473 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5474 return FAIL;
5475
Bram Moolenaar91458462021-01-13 20:08:38 +01005476 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005477 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005478 posp->col = n;
5479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005480 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005481 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005482 posp->coladd = 0;
5483 else
5484 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005485
Bram Moolenaar493c1782014-05-28 14:34:46 +02005486 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005488
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005489 return OK;
5490}
5491
5492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 * Get the length of an environment variable name.
5494 * Advance "arg" to the first character after the name.
5495 * Return 0 for error.
5496 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 char_u *p;
5501 int len;
5502
5503 for (p = *arg; vim_isIDc(*p); ++p)
5504 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return 0;
5507
5508 len = (int)(p - *arg);
5509 *arg = p;
5510 return len;
5511}
5512
5513/*
5514 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005515 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * Return 0 if something is wrong.
5517 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005519get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 char_u *p;
5522 int len;
5523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005524 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005526 {
5527 if (*p == ':')
5528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 // "s:" is start of "s:var", but "n:" is not and can be used in
5530 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005531 len = (int)(p - *arg);
5532 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5533 || len > 1)
5534 break;
5535 }
5536 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005537 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return 0;
5539
5540 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005541 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 return len;
5544}
5545
5546/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005547 * Get the length of the name of a variable or function.
5548 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005550 * Return -1 if curly braces expansion failed.
5551 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 * If the name contains 'magic' {}'s, expand them and return the
5553 * expanded name in an allocated string via 'alias' - caller must free.
5554 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005556get_name_len(
5557 char_u **arg,
5558 char_u **alias,
5559 int evaluate,
5560 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 char_u *p;
5564 char_u *expr_start;
5565 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5570 && (*arg)[2] == (int)KE_SNR)
5571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 *arg += 3;
5574 return get_id_len(arg) + 3;
5575 }
5576 len = eval_fname_script(*arg);
5577 if (len > 0)
5578 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005579 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 *arg += len;
5581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005586 p = find_name_end(*arg, &expr_start, &expr_end,
5587 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (expr_start != NULL)
5589 {
5590 char_u *temp_string;
5591
5592 if (!evaluate)
5593 {
5594 len += (int)(p - *arg);
5595 *arg = skipwhite(p);
5596 return len;
5597 }
5598
5599 /*
5600 * Include any <SID> etc in the expanded string:
5601 * Thus the -len here.
5602 */
5603 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5604 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 *alias = temp_string;
5607 *arg = skipwhite(p);
5608 return (int)STRLEN(temp_string);
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005612 // Only give an error when there is something, otherwise it will be
5613 // reported at a higher level.
5614 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005615 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
5617 return len;
5618}
5619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620/*
5621 * Find the end of a variable or function name, taking care of magic braces.
5622 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5623 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005624 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625 * Return a pointer to just after the name. Equal to "arg" if there is no
5626 * valid name.
5627 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629find_name_end(
5630 char_u *arg,
5631 char_u **expr_start,
5632 char_u **expr_end,
5633 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 int mb_nest = 0;
5636 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005638 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005639 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 *expr_start = NULL;
5644 *expr_end = NULL;
5645 }
5646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005648 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5649 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005650 return arg;
5651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 for (p = arg; *p != NUL
5653 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005654 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005655 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005656 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005658 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005660 if (*p == '\'')
5661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // skip over 'string' 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 ;
5665 if (*p == NUL)
5666 break;
5667 }
5668 else if (*p == '"')
5669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005670 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005671 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005672 if (*p == '\\' && p[1] != NUL)
5673 ++p;
5674 if (*p == NUL)
5675 break;
5676 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005677 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005679 // "s:" is start of "s:var", but "n:" is not and can be used in
5680 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005681 len = (int)(p - arg);
5682 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005683 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005684 break;
5685 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005686
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 if (*p == '[')
5690 ++br_nest;
5691 else if (*p == ']')
5692 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005694
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005695 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 if (*p == '{')
5698 {
5699 mb_nest++;
5700 if (expr_start != NULL && *expr_start == NULL)
5701 *expr_start = p;
5702 }
5703 else if (*p == '}')
5704 {
5705 mb_nest--;
5706 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5707 *expr_end = p;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711
5712 return p;
5713}
5714
5715/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005716 * Expands out the 'magic' {}'s in a variable/function name.
5717 * Note that this can call itself recursively, to deal with
5718 * constructs like foo{bar}{baz}{bam}
5719 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5720 * "in_start" ^
5721 * "expr_start" ^
5722 * "expr_end" ^
5723 * "in_end" ^
5724 *
5725 * Returns a new allocated string, which the caller must free.
5726 * Returns NULL for failure.
5727 */
5728 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729make_expanded_name(
5730 char_u *in_start,
5731 char_u *expr_start,
5732 char_u *expr_end,
5733 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734{
5735 char_u c1;
5736 char_u *retval = NULL;
5737 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005738
5739 if (expr_end == NULL || in_end == NULL)
5740 return NULL;
5741 *expr_start = NUL;
5742 *expr_end = NUL;
5743 c1 = *in_end;
5744 *in_end = NUL;
5745
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005746 temp_result = eval_to_string(expr_start + 1, FALSE);
5747 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005749 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5750 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 if (retval != NULL)
5752 {
5753 STRCPY(retval, in_start);
5754 STRCAT(retval, temp_result);
5755 STRCAT(retval, expr_end + 1);
5756 }
5757 }
5758 vim_free(temp_result);
5759
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005760 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005761 *expr_start = '{';
5762 *expr_end = '}';
5763
5764 if (retval != NULL)
5765 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005766 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005767 if (expr_start != NULL)
5768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005769 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005770 temp_result = make_expanded_name(retval, expr_start,
5771 expr_end, temp_result);
5772 vim_free(retval);
5773 retval = temp_result;
5774 }
5775 }
5776
5777 return retval;
5778}
5779
5780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005782 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005787 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005788}
5789
5790/*
5791 * Return TRUE if character "c" can be used as the first character in a
5792 * variable or function name (excluding '{' and '}').
5793 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005795eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005796{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005797 return ASCII_ISALPHA(c) || c == '_';
5798}
5799
5800/*
5801 * Return TRUE if character "c" can be used as the first character of a
5802 * dictionary key.
5803 */
5804 int
5805eval_isdictc(int c)
5806{
5807 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808}
5809
5810/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005811 * Handle:
5812 * - expr[expr], expr[expr:expr] subscript
5813 * - ".name" lookup
5814 * - function call with Funcref variable: func(expr)
5815 * - method call: var->method()
5816 *
5817 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820handle_subscript(
5821 char_u **arg,
5822 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005823 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005824 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005825{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005826 int evaluate = evalarg != NULL
5827 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005828 int ret = OK;
5829 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005830 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005831 int getnext;
5832 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005833
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005834 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005835 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005836 // When at the end of the line and ".name" or "->{" or "->X" follows in
5837 // the next line then consume the line break.
5838 p = eval_next_non_blank(*arg, evalarg, &getnext);
5839 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005840 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005841 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5842 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5843 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005844 {
5845 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005846 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005847 check_white = FALSE;
5848 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005849
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005850 if (rettv->v_type == VAR_ANY)
5851 {
5852 char_u *exp_name;
5853 int cc;
5854 int idx;
5855 ufunc_T *ufunc;
5856 type_T *type;
5857
5858 // Found script from "import * as {name}", script item name must
5859 // follow.
5860 if (**arg != '.')
5861 {
5862 if (verbose)
5863 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5864 ret = FAIL;
5865 break;
5866 }
5867 ++*arg;
5868 if (IS_WHITE_OR_NUL(**arg))
5869 {
5870 if (verbose)
5871 emsg(_(e_no_white_space_allowed_after_dot));
5872 ret = FAIL;
5873 break;
5874 }
5875
5876 // isolate the name
5877 exp_name = *arg;
5878 while (eval_isnamec(**arg))
5879 ++*arg;
5880 cc = **arg;
5881 **arg = NUL;
5882
5883 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5884 evalarg->eval_cctx, verbose);
5885 **arg = cc;
5886 *arg = skipwhite(*arg);
5887
5888 if (idx < 0 && ufunc == NULL)
5889 {
5890 ret = FAIL;
5891 break;
5892 }
5893 if (idx >= 0)
5894 {
5895 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5896 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5897
5898 copy_tv(sv->sv_tv, rettv);
5899 }
5900 else
5901 {
5902 rettv->v_type = VAR_FUNC;
5903 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5904 }
5905 }
5906
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005907 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5908 || rettv->v_type == VAR_PARTIAL))
5909 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005910 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005911 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5912 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005913
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005914 // Stop the expression evaluation when immediately aborting on
5915 // error, or when an interrupt occurred or an exception was thrown
5916 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005917 if (aborting())
5918 {
5919 if (ret == OK)
5920 clear_tv(rettv);
5921 ret = FAIL;
5922 }
5923 dict_unref(selfdict);
5924 selfdict = NULL;
5925 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005926 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005927 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005928 if (in_vim9script())
5929 *arg = skipwhite(p + 2);
5930 else
5931 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005932 if (ret == OK)
5933 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005934 if (VIM_ISWHITE(**arg))
5935 {
5936 emsg(_(e_nowhitespace));
5937 ret = FAIL;
5938 }
5939 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005940 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005941 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005942 else
5943 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005944 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005945 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005946 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005947 // "." is ".name" lookup when we found a dict or when evaluating and
5948 // scriptversion is at least 2, where string concatenation is "..".
5949 else if (**arg == '['
5950 || (**arg == '.' && (rettv->v_type == VAR_DICT
5951 || (!evaluate
5952 && (*arg)[1] != '.'
5953 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005954 {
5955 dict_unref(selfdict);
5956 if (rettv->v_type == VAR_DICT)
5957 {
5958 selfdict = rettv->vval.v_dict;
5959 if (selfdict != NULL)
5960 ++selfdict->dv_refcount;
5961 }
5962 else
5963 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005964 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005965 {
5966 clear_tv(rettv);
5967 ret = FAIL;
5968 }
5969 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005970 else
5971 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005972 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5975 // Don't do this when "Func" is already a partial that was bound
5976 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005977 if (selfdict != NULL
5978 && (rettv->v_type == VAR_FUNC
5979 || (rettv->v_type == VAR_PARTIAL
5980 && (rettv->vval.v_partial->pt_auto
5981 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005982 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005983
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005984 dict_unref(selfdict);
5985 return ret;
5986}
5987
5988/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 * Make a copy of an item.
5990 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005991 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5992 * reference to an already copied list/dict can be used.
5993 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996item_copy(
5997 typval_T *from,
5998 typval_T *to,
5999 int deep,
6000 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001{
6002 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006007 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009 }
6010 ++recurse;
6011
6012 switch (from->v_type)
6013 {
6014 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006015 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006016 case VAR_STRING:
6017 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006018 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006019 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006020 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006021 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006022 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006023 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024 copy_tv(from, to);
6025 break;
6026 case VAR_LIST:
6027 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006028 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 if (from->vval.v_list == NULL)
6030 to->vval.v_list = NULL;
6031 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 to->vval.v_list = from->vval.v_list->lv_copylist;
6035 ++to->vval.v_list->lv_refcount;
6036 }
6037 else
6038 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6039 if (to->vval.v_list == NULL)
6040 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006042 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006043 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006044 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 case VAR_DICT:
6046 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006047 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006048 if (from->vval.v_dict == NULL)
6049 to->vval.v_dict = NULL;
6050 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6054 ++to->vval.v_dict->dv_refcount;
6055 }
6056 else
6057 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6058 if (to->vval.v_dict == NULL)
6059 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006061 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006062 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006064 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006065 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006066 }
6067 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006068 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006069}
6070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006071 void
6072echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6073{
6074 char_u *tofree;
6075 char_u numbuf[NUMBUFLEN];
6076 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6077
6078 if (*atstart)
6079 {
6080 *atstart = FALSE;
6081 // Call msg_start() after eval1(), evaluating the expression
6082 // may cause a message to appear.
6083 if (with_space)
6084 {
6085 // Mark the saved text as finishing the line, so that what
6086 // follows is displayed on a new line when scrolling back
6087 // at the more prompt.
6088 msg_sb_eol();
6089 msg_start();
6090 }
6091 }
6092 else if (with_space)
6093 msg_puts_attr(" ", echo_attr);
6094
6095 if (p != NULL)
6096 for ( ; *p != NUL && !got_int; ++p)
6097 {
6098 if (*p == '\n' || *p == '\r' || *p == TAB)
6099 {
6100 if (*p != TAB && *needclr)
6101 {
6102 // remove any text still there from the command
6103 msg_clr_eos();
6104 *needclr = FALSE;
6105 }
6106 msg_putchar_attr(*p, echo_attr);
6107 }
6108 else
6109 {
6110 if (has_mbyte)
6111 {
6112 int i = (*mb_ptr2len)(p);
6113
6114 (void)msg_outtrans_len_attr(p, i, echo_attr);
6115 p += i - 1;
6116 }
6117 else
6118 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6119 }
6120 }
6121 vim_free(tofree);
6122}
6123
Bram Moolenaare9a41262005-01-15 22:18:47 +00006124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 * ":echo expr1 ..." print each argument separated with a space, add a
6126 * newline at the end.
6127 * ":echon expr1 ..." print each argument plain.
6128 */
6129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006130ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006134 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 int needclr = TRUE;
6136 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006137 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006138 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006139 evalarg_T evalarg;
6140
Bram Moolenaare707c882020-07-01 13:07:37 +02006141 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 if (eap->skip)
6144 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006145 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006147 // If eval1() causes an error message the text from the command may
6148 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006149 need_clr_eos = needclr;
6150
Bram Moolenaar68db9962021-05-09 23:19:22 +02006151 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006152 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 /*
6155 * Report the invalid expression unless the expression evaluation
6156 * has been cancelled due to an aborting error, an interrupt, or an
6157 * exception.
6158 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006159 if (!aborting() && did_emsg == did_emsg_before
6160 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006161 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006162 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 break;
6164 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 need_clr_eos = FALSE;
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006168 {
6169 if (rettv.v_type == VAR_VOID)
6170 {
6171 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6172 break;
6173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006175 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 arg = skipwhite(arg);
6179 }
6180 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006181 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 if (eap->skip)
6184 --emsg_skip;
6185 else
6186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (needclr)
6189 msg_clr_eos();
6190 if (eap->cmdidx == CMD_echo)
6191 msg_end();
6192 }
6193}
6194
6195/*
6196 * ":echohl {name}".
6197 */
6198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006201 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202}
6203
6204/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006205 * Returns the :echo attribute
6206 */
6207 int
6208get_echo_attr(void)
6209{
6210 return echo_attr;
6211}
6212
6213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 * ":execute expr1 ..." execute the result of an expression.
6215 * ":echomsg expr1 ..." Print a message
6216 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006217 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 * Each gets spaces around each argument and a newline at the end for
6219 * echo commands
6220 */
6221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006222ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 int ret = OK;
6227 char_u *p;
6228 garray_T ga;
6229 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 ga_init2(&ga, 1, 80);
6232
6233 if (eap->skip)
6234 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006235 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006237 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006238 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (!eap->skip)
6242 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006243 char_u buf[NUMBUFLEN];
6244
6245 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006246 {
6247 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6248 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006249 semsg(_(e_using_invalid_value_as_string_str),
6250 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006251 p = NULL;
6252 }
6253 else
6254 p = tv_get_string_buf(&rettv, buf);
6255 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006256 else
6257 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006258 if (p == NULL)
6259 {
6260 clear_tv(&rettv);
6261 ret = FAIL;
6262 break;
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 len = (int)STRLEN(p);
6265 if (ga_grow(&ga, len + 2) == FAIL)
6266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 ret = FAIL;
6269 break;
6270 }
6271 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 ga.ga_len += len;
6275 }
6276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006277 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 arg = skipwhite(arg);
6279 }
6280
6281 if (ret != FAIL && ga.ga_data != NULL)
6282 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006283 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 // Mark the already saved text as finishing the line, so that what
6286 // follows is displayed on a new line when scrolling back at the
6287 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006288 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006289 }
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006292 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006293 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006294 out_flush();
6295 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006296 else if (eap->cmdidx == CMD_echoconsole)
6297 {
6298 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6299 ui_write((char_u *)"\r\n", 2, TRUE);
6300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 else if (eap->cmdidx == CMD_echoerr)
6302 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006303 int save_did_emsg = did_emsg;
6304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006306 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (!force_abort)
6308 did_emsg = save_did_emsg;
6309 }
6310 else if (eap->cmdidx == CMD_execute)
6311 do_cmdline((char_u *)ga.ga_data,
6312 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6313 }
6314
6315 ga_clear(&ga);
6316
6317 if (eap->skip)
6318 --emsg_skip;
6319
6320 eap->nextcmd = check_nextcmd(arg);
6321}
6322
6323/*
6324 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6325 * "arg" points to the "&" or '+' when called, to "option" when returning.
6326 * Returns NULL when no option name found. Otherwise pointer to the char
6327 * after the option name.
6328 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *p = *arg;
6333
6334 ++p;
6335 if (*p == 'g' && p[1] == ':')
6336 {
6337 *opt_flags = OPT_GLOBAL;
6338 p += 2;
6339 }
6340 else if (*p == 'l' && p[1] == ':')
6341 {
6342 *opt_flags = OPT_LOCAL;
6343 p += 2;
6344 }
6345 else
6346 *opt_flags = 0;
6347
6348 if (!ASCII_ISALPHA(*p))
6349 return NULL;
6350 *arg = p;
6351
6352 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006353 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 else
6355 while (ASCII_ISALPHA(*p))
6356 ++p;
6357 return p;
6358}
6359
6360/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006361 * Display script name where an item was last set.
6362 * Should only be invoked when 'verbose' is non-zero.
6363 */
6364 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006365last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006366{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006367 char_u *p;
6368
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006369 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006370 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006371 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006372 if (p != NULL)
6373 {
6374 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006375 msg_puts(_("\n\tLast set from "));
6376 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006377 if (script_ctx.sc_lnum > 0)
6378 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006379 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006380 msg_outnum((long)script_ctx.sc_lnum);
6381 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006382 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006383 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006384 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006385 }
6386}
6387
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006388#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390/*
6391 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006392 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * "flags" can be "g" to do a global substitute.
6394 * Returns an allocated string, NULL for error.
6395 */
6396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397do_string_sub(
6398 char_u *str,
6399 char_u *pat,
6400 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006401 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 int sublen;
6405 regmatch_T regmatch;
6406 int i;
6407 int do_all;
6408 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006409 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 garray_T ga;
6411 char_u *ret;
6412 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006413 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006415 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006417 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 ga_init2(&ga, 1, 200);
6420
6421 do_all = (flags[0] == 'g');
6422
6423 regmatch.rm_ic = p_ic;
6424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6425 if (regmatch.regprog != NULL)
6426 {
6427 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006428 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006432 if (regmatch.startp[0] == regmatch.endp[0])
6433 {
6434 if (zero_width == regmatch.startp[0])
6435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006437 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006438 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6439 (size_t)i);
6440 ga.ga_len += i;
6441 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006442 continue;
6443 }
6444 zero_width = regmatch.startp[0];
6445 }
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 /*
6448 * Get some space for a temporary buffer to do the substitution
6449 * into. It will contain:
6450 * - The text up to where the match is.
6451 * - The substituted text.
6452 * - The text after the match.
6453 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006454 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006455 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6457 {
6458 ga_clear(&ga);
6459 break;
6460 }
6461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 i = (int)(regmatch.startp[0] - tail);
6464 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006466 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 + ga.ga_len + i, TRUE, TRUE, FALSE);
6468 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006469 tail = regmatch.endp[0];
6470 if (*tail == NUL)
6471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (!do_all)
6473 break;
6474 }
6475
6476 if (ga.ga_data != NULL)
6477 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6478
Bram Moolenaar473de612013-06-08 18:19:48 +02006479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481
6482 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6483 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006484 if (p_cpo == empty_option)
6485 p_cpo = save_cpo;
6486 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006489 // If it's still empty it was changed and restored, need to restore in
6490 // the complicated way.
6491 if (*p_cpo == NUL)
6492 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006493 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496 return ret;
6497}