blob: e0f0816030de021ec05af062f189580a777d1696 [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 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200962 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200963
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()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001780 ? (ASSIGN_FINAL
1781 // first round: error if variable exists
1782 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1783 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001784 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001787 if (fi->fi_blob != NULL)
1788 {
1789 typval_T tv;
1790
1791 if (fi->fi_bi >= blob_len(fi->fi_blob))
1792 return FALSE;
1793 tv.v_type = VAR_NUMBER;
1794 tv.v_lock = VAR_FIXED;
1795 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1796 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001797 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001798 fi->fi_varcount, flag, NULL) == OK;
1799 }
1800
1801 if (fi->fi_string != NULL)
1802 {
1803 typval_T tv;
1804 int len;
1805
1806 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1807 if (len == 0)
1808 return FALSE;
1809 tv.v_type = VAR_STRING;
1810 tv.v_lock = VAR_FIXED;
1811 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1812 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001813 ++fi->fi_bi;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001814 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001815 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001816 vim_free(tv.vval.v_string);
1817 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001818 }
1819
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 item = fi->fi_lw.lw_item;
1821 if (item == NULL)
1822 result = FALSE;
1823 else
1824 {
1825 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001826 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001827 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001828 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 }
1830 return result;
1831}
1832
1833/*
1834 * Free the structure used to store info used by ":for".
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838{
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001841 if (fi == NULL)
1842 return;
1843 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001844 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001846 list_unref(fi->fi_list);
1847 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001848 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001849 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001850 else
1851 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 vim_free(fi);
1853}
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001856set_context_for_expression(
1857 expand_T *xp,
1858 char_u *arg,
1859 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001861 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001865 if (cmdidx == CMD_let || cmdidx == CMD_var
1866 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 {
1868 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001869 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001872 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 {
1874 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001875 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001876 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 break;
1878 }
1879 return;
1880 }
1881 }
1882 else
1883 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1884 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 while ((xp->xp_pattern = vim_strpbrk(arg,
1886 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1887 {
1888 c = *xp->xp_pattern;
1889 if (c == '&')
1890 {
1891 c = xp->xp_pattern[1];
1892 if (c == '&')
1893 {
1894 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001895 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001900 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1901 xp->xp_pattern += 2;
1902
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 else if (c == '$')
1906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001907 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 xp->xp_context = EXPAND_ENV_VARS;
1909 }
1910 else if (c == '=')
1911 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001912 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 xp->xp_context = EXPAND_EXPRESSION;
1914 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001915 else if (c == '#'
1916 && xp->xp_context == EXPAND_EXPRESSION)
1917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001919 break;
1920 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001921 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 && xp->xp_context == EXPAND_FUNCTIONS
1923 && vim_strchr(xp->xp_pattern, '(') == NULL)
1924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 break;
1927 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001928 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1933 if (c == '\\' && xp->xp_pattern[1] != NUL)
1934 ++xp->xp_pattern;
1935 xp->xp_context = EXPAND_NOTHING;
1936 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001939 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1941 /* skip */ ;
1942 xp->xp_context = EXPAND_NOTHING;
1943 }
1944 else if (c == '|')
1945 {
1946 if (xp->xp_pattern[1] == '|')
1947 {
1948 ++xp->xp_pattern;
1949 xp->xp_context = EXPAND_EXPRESSION;
1950 }
1951 else
1952 xp->xp_context = EXPAND_COMMANDS;
1953 }
1954 else
1955 xp->xp_context = EXPAND_EXPRESSION;
1956 }
1957 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // Doesn't look like something valid, expand as an expression
1959 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 arg = xp->xp_pattern;
1962 if (*arg != NUL)
1963 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1964 /* skip */ ;
1965 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001966
1967 // ":exe one two" completes "two"
1968 if ((cmdidx == CMD_execute
1969 || cmdidx == CMD_echo
1970 || cmdidx == CMD_echon
1971 || cmdidx == CMD_echomsg)
1972 && xp->xp_context == EXPAND_EXPRESSION)
1973 {
1974 for (;;)
1975 {
1976 char_u *n = skiptowhite(arg);
1977
1978 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1979 break;
1980 arg = skipwhite(n);
1981 }
1982 }
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 xp->xp_pattern = arg;
1985}
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001988 * Return TRUE if "pat" matches "text".
1989 * Does not use 'cpo' and always uses 'magic'.
1990 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001991 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001992pattern_match(char_u *pat, char_u *text, int ic)
1993{
1994 int matches = FALSE;
1995 char_u *save_cpo;
1996 regmatch_T regmatch;
1997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001998 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001999 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002000 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002001 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2002 if (regmatch.regprog != NULL)
2003 {
2004 regmatch.rm_ic = ic;
2005 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2006 vim_regfree(regmatch.regprog);
2007 }
2008 p_cpo = save_cpo;
2009 return matches;
2010}
2011
2012/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 * Handle a name followed by "(". Both for just "name(arg)" and for
2014 * "expr->name(arg)".
2015 * Returns OK or FAIL.
2016 */
2017 static int
2018eval_func(
2019 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002020 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 char_u *name,
2022 int name_len,
2023 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002024 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 typval_T *basetv) // "expr" for "expr->name(arg)"
2026{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002027 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002028 char_u *s = name;
2029 int len = name_len;
2030 partial_T *partial;
2031 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002032 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033
2034 if (!evaluate)
2035 check_vars(s, len);
2036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002037 // If "s" is the name of a variable of type VAR_FUNC
2038 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002039 s = deref_func_name(s, &len, &partial,
2040 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002042 // Need to make a copy, in case evaluating the arguments makes
2043 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002045 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 ret = FAIL;
2047 else
2048 {
2049 funcexe_T funcexe;
2050
2051 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002052 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002053 funcexe.firstline = curwin->w_cursor.lnum;
2054 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 funcexe.evaluate = evaluate;
2056 funcexe.partial = partial;
2057 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002058 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002059 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002060 }
2061 vim_free(s);
2062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002063 // If evaluate is FALSE rettv->v_type was not set in
2064 // get_func_tv, but it's needed in handle_subscript() to parse
2065 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2067 {
2068 rettv->vval.v_string = NULL;
2069 rettv->v_type = VAR_FUNC;
2070 }
2071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002072 // Stop the expression evaluation when immediately
2073 // aborting on error, or when an interrupt occurred or
2074 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002075 if (evaluate && aborting())
2076 {
2077 if (ret == OK)
2078 clear_tv(rettv);
2079 ret = FAIL;
2080 }
2081 return ret;
2082}
2083
2084/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002085 * Get the next line source line without advancing. But do skip over comment
2086 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002087 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002088 */
2089 static char_u *
2090getline_peek_skip_comments(evalarg_T *evalarg)
2091{
2092 for (;;)
2093 {
2094 char_u *next = getline_peek(evalarg->eval_getline,
2095 evalarg->eval_cookie);
2096 char_u *p;
2097
2098 if (next == NULL)
2099 break;
2100 p = skipwhite(next);
2101 if (*p != NUL && !vim9_comment_start(p))
2102 return next;
2103 (void)eval_next_line(evalarg);
2104 }
2105 return NULL;
2106}
2107
2108/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002109 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2110 * comment) and there is a next line, return the next line (skipping blanks)
2111 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002112 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2113 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 * "arg" must point somewhere inside a line, not at the start.
2115 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002116 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2118{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002119 char_u *p = skipwhite(arg);
2120
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002122 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002124 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002125 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002128
2129 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002130 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002131 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002132 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133
Bram Moolenaar9d489562020-07-30 20:08:50 +02002134 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135 {
2136 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002137 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 }
2139 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002140 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002141}
2142
2143/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002144 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002145 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002146 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002147 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002148eval_next_line(evalarg_T *evalarg)
2149{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002150 garray_T *gap = &evalarg->eval_ga;
2151 char_u *line;
2152
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002153 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002154 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2155 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002156 else
2157 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002158 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002159 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2160 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002161 char_u *p = skipwhite(line);
2162
2163 // Going to concatenate the lines after parsing. For an empty or
2164 // comment line use an empty string.
2165 if (*p == NUL || vim9_comment_start(p))
2166 {
2167 vim_free(line);
2168 line = vim_strsave((char_u *)"");
2169 }
2170
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002171 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2172 ++gap->ga_len;
2173 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002175 {
2176 vim_free(evalarg->eval_tofree);
2177 evalarg->eval_tofree = line;
2178 }
2179 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002180}
2181
Bram Moolenaare6b53242020-07-01 17:28:33 +02002182/*
2183 * Call eval_next_non_blank() and get the next line if needed.
2184 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002185 char_u *
2186skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2187{
2188 int getnext;
2189 char_u *p = skipwhite(arg);
2190
Bram Moolenaar962d7212020-07-04 14:15:00 +02002191 if (evalarg == NULL)
2192 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002193 eval_next_non_blank(p, evalarg, &getnext);
2194 if (getnext)
2195 return eval_next_line(evalarg);
2196 return p;
2197}
2198
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002199/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002200 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002201 */
2202 void
2203clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2204{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002205 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002206 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002207 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002208 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002209 if (eap != NULL)
2210 {
2211 // We may need to keep the original command line, e.g. for
2212 // ":let" it has the variable names. But we may also need the
2213 // new one, "nextcmd" points into it. Keep both.
2214 vim_free(eap->cmdline_tofree);
2215 eap->cmdline_tofree = *eap->cmdlinep;
2216 *eap->cmdlinep = evalarg->eval_tofree;
2217 }
2218 else
2219 vim_free(evalarg->eval_tofree);
2220 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002221 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002222
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002223 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2224 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002225 }
2226}
2227
2228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2232 */
2233
2234/*
2235 * Handle zero level expression.
2236 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002238 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002239 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 * Return OK or FAIL.
2241 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243eval0(
2244 char_u *arg,
2245 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002246 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 int ret;
2250 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002251 int did_emsg_before = did_emsg;
2252 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002254 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002258 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002259
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002260 if (ret != FAIL)
2261 end_error = !ends_excmd2(arg, p);
2262 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /*
2267 * Report the invalid expression unless the expression evaluation has
2268 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002269 * exception, or we already gave a more specific error.
2270 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002272 if (!aborting()
2273 && did_emsg == did_emsg_before
2274 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002275 && (flags & EVAL_CONSTANT) == 0
2276 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002277 {
2278 if (end_error)
2279 semsg(_(e_trailing_arg), p);
2280 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002281 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002282 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002283
2284 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002285 // a next command to avoid more errors, unless "|" is following, which
2286 // could only be a command separator.
2287 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2288 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002291
2292 if (eap != NULL)
2293 eap->nextcmd = check_nextcmd(p);
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 return ret;
2296}
2297
2298/*
2299 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002300 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002301 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 *
2303 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002304 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002306 * Note: "rettv.v_lock" is not set.
2307 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * Return OK or FAIL.
2309 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002310 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002313 char_u *p;
2314 int getnext;
2315
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002316 CLEAR_POINTER(rettv);
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /*
2319 * Get the first variable.
2320 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002321 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return FAIL;
2323
Bram Moolenaar793648f2020-06-26 21:28:25 +02002324 p = eval_next_non_blank(*arg, evalarg, &getnext);
2325 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002327 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002328 int result;
2329 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002330 evalarg_T *evalarg_used = evalarg;
2331 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002333 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002334 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002335
2336 if (evalarg == NULL)
2337 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002338 CLEAR_FIELD(local_evalarg);
2339 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002340 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002341 orig_flags = evalarg_used->eval_flags;
2342 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002343
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002344 if (getnext)
2345 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002346 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002347 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002348 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002349 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002350 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002351 clear_tv(rettv);
2352 return FAIL;
2353 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002354 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002355 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002358 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 int error = FALSE;
2361
Bram Moolenaar13106602020-10-04 16:06:05 +02002362 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002363 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002364 else if (vim9script)
2365 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002366 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002368 if (error || !op_falsy || !result)
2369 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 if (error)
2371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373
2374 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002375 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002377 if (op_falsy)
2378 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002379 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002380 {
mityu4ac198c2021-05-28 17:52:40 +02002381 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 clear_tv(rettv);
2383 return FAIL;
2384 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002385 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 evalarg_used->eval_flags = (op_falsy ? !result : result)
2387 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2388 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002389 {
2390 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002392 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 if (!op_falsy || !result)
2394 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002396 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002398 /*
2399 * Check for the ":".
2400 */
2401 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2402 if (*p != ':')
2403 {
2404 emsg(_(e_missing_colon));
2405 if (evaluate && result)
2406 clear_tv(rettv);
2407 evalarg_used->eval_flags = orig_flags;
2408 return FAIL;
2409 }
2410 if (getnext)
2411 *arg = eval_next_line(evalarg_used);
2412 else
2413 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002414 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002415 {
2416 error_white_both(p, 1);
2417 clear_tv(rettv);
2418 evalarg_used->eval_flags = orig_flags;
2419 return FAIL;
2420 }
2421 *arg = p;
2422 }
2423
2424 /*
2425 * Get the third variable. Recursive!
2426 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002427 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002428 {
mityu4ac198c2021-05-28 17:52:40 +02002429 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002430 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002431 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002432 return FAIL;
2433 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002434 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2435 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002437 if (eval1(arg, &var2, evalarg_used) == FAIL)
2438 {
2439 if (evaluate && result)
2440 clear_tv(rettv);
2441 evalarg_used->eval_flags = orig_flags;
2442 return FAIL;
2443 }
2444 if (evaluate && !result)
2445 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447
2448 if (evalarg == NULL)
2449 clear_evalarg(&local_evalarg, NULL);
2450 else
2451 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
2454 return OK;
2455}
2456
2457/*
2458 * Handle first level expression:
2459 * expr2 || expr2 || expr2 logical OR
2460 *
2461 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002462 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 *
2464 * Return OK or FAIL.
2465 */
2466 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002467eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002469 char_u *p;
2470 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 /*
2473 * Get the first variable.
2474 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002475 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return FAIL;
2477
2478 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002479 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002481 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 evalarg_T *evalarg_used = evalarg;
2485 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486 int evaluate;
2487 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 long result = FALSE;
2489 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002490 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002491 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002492
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002493 if (evalarg == NULL)
2494 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 CLEAR_FIELD(local_evalarg);
2496 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 orig_flags = evalarg_used->eval_flags;
2499 evaluate = orig_flags & EVAL_EVALUATE;
2500 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002501 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002502 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002503 result = tv_get_bool_chk(rettv, &error);
2504 else if (tv_get_number_chk(rettv, &error) != 0)
2505 result = TRUE;
2506 clear_tv(rettv);
2507 if (error)
2508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510
2511 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002512 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 while (p[0] == '|' && p[1] == '|')
2515 {
2516 if (getnext)
2517 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002518 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002519 {
2520 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2521 {
2522 error_white_both(p, 2);
2523 clear_tv(rettv);
2524 return FAIL;
2525 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002526 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528
2529 /*
2530 * Get the second variable.
2531 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002532 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2533 {
mityu4ac198c2021-05-28 17:52:40 +02002534 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002535 clear_tv(rettv);
2536 return FAIL;
2537 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2539 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543
2544 /*
2545 * Compute the result.
2546 */
2547 if (evaluate && !result)
2548 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002549 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002550 result = tv_get_bool_chk(&var2, &error);
2551 else if (tv_get_number_chk(&var2, &error) != 0)
2552 result = TRUE;
2553 clear_tv(&var2);
2554 if (error)
2555 return FAIL;
2556 }
2557 if (evaluate)
2558 {
2559 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002560 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002561 rettv->v_type = VAR_BOOL;
2562 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002563 }
2564 else
2565 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002566 rettv->v_type = VAR_NUMBER;
2567 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002568 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570
2571 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002573
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 if (evalarg == NULL)
2575 clear_evalarg(&local_evalarg, NULL);
2576 else
2577 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
2580 return OK;
2581}
2582
2583/*
2584 * Handle second level expression:
2585 * expr3 && expr3 && expr3 logical AND
2586 *
2587 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002588 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 *
2590 * Return OK or FAIL.
2591 */
2592 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002595 char_u *p;
2596 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 /*
2599 * Get the first variable.
2600 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002601 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
2603
2604 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002607 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 evalarg_T *evalarg_used = evalarg;
2611 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002612 int orig_flags;
2613 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 long result = TRUE;
2615 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002616 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002617 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002618
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 if (evalarg == NULL)
2620 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 CLEAR_FIELD(local_evalarg);
2622 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 orig_flags = evalarg_used->eval_flags;
2625 evaluate = orig_flags & EVAL_EVALUATE;
2626 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002628 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 result = tv_get_bool_chk(rettv, &error);
2630 else if (tv_get_number_chk(rettv, &error) == 0)
2631 result = FALSE;
2632 clear_tv(rettv);
2633 if (error)
2634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636
2637 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640 while (p[0] == '&' && p[1] == '&')
2641 {
2642 if (getnext)
2643 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002644 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002645 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002646 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002647 {
2648 error_white_both(p, 2);
2649 clear_tv(rettv);
2650 return FAIL;
2651 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002652 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654
2655 /*
2656 * Get the second variable.
2657 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002658 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2659 {
mityu4ac198c2021-05-28 17:52:40 +02002660 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002661 clear_tv(rettv);
2662 return FAIL;
2663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2665 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002667 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670
2671 /*
2672 * Compute the result.
2673 */
2674 if (evaluate && result)
2675 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002676 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002677 result = tv_get_bool_chk(&var2, &error);
2678 else if (tv_get_number_chk(&var2, &error) == 0)
2679 result = FALSE;
2680 clear_tv(&var2);
2681 if (error)
2682 return FAIL;
2683 }
2684 if (evaluate)
2685 {
2686 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002687 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002688 rettv->v_type = VAR_BOOL;
2689 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 }
2691 else
2692 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002693 rettv->v_type = VAR_NUMBER;
2694 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002695 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002697
2698 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002700
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002701 if (evalarg == NULL)
2702 clear_evalarg(&local_evalarg, NULL);
2703 else
2704 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706
2707 return OK;
2708}
2709
2710/*
2711 * Handle third level expression:
2712 * var1 == var2
2713 * var1 =~ var2
2714 * var1 != var2
2715 * var1 !~ var2
2716 * var1 > var2
2717 * var1 >= var2
2718 * var1 < var2
2719 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002720 * var1 is var2
2721 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *
2723 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002724 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 *
2726 * Return OK or FAIL.
2727 */
2728 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002729eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002732 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002733 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002735 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 /*
2738 * Get the first variable.
2739 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return FAIL;
2742
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002743 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002744 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002747 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002749 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002751 typval_T var2;
2752 int ic;
2753 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002754 int evaluate = evalarg == NULL
2755 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002756
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002757 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002758 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002759 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002760 p = *arg;
2761 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002762 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2763 {
mityu4ac198c2021-05-28 17:52:40 +02002764 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002765 clear_tv(rettv);
2766 return FAIL;
2767 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002768
Bram Moolenaar696ba232020-07-29 21:20:41 +02002769 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2770 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002771 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002772 clear_tv(rettv);
2773 return FAIL;
2774 }
2775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002776 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (p[len] == '?')
2778 {
2779 ic = TRUE;
2780 ++len;
2781 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002782 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 else if (p[len] == '#')
2784 {
2785 ic = FALSE;
2786 ++len;
2787 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002788 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002790 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
2792 /*
2793 * Get the second variable.
2794 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002795 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2796 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002797 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002798 clear_tv(rettv);
2799 return FAIL;
2800 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002801 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002804 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 return FAIL;
2806 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002807 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002808 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002809 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002810
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002811 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002812 {
2813 ret = FAIL;
2814 clear_tv(rettv);
2815 }
2816 else
2817 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002818 clear_tv(&var2);
2819 return ret;
2820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822
2823 return OK;
2824}
2825
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002826/*
2827 * Make a copy of blob "tv1" and append blob "tv2".
2828 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 void
2830eval_addblob(typval_T *tv1, typval_T *tv2)
2831{
2832 blob_T *b1 = tv1->vval.v_blob;
2833 blob_T *b2 = tv2->vval.v_blob;
2834 blob_T *b = blob_alloc();
2835 int i;
2836
2837 if (b != NULL)
2838 {
2839 for (i = 0; i < blob_len(b1); i++)
2840 ga_append(&b->bv_ga, blob_get(b1, i));
2841 for (i = 0; i < blob_len(b2); i++)
2842 ga_append(&b->bv_ga, blob_get(b2, i));
2843
2844 clear_tv(tv1);
2845 rettv_blob_set(tv1, b);
2846 }
2847}
2848
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002849/*
2850 * Make a copy of list "tv1" and append list "tv2".
2851 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 int
2853eval_addlist(typval_T *tv1, typval_T *tv2)
2854{
2855 typval_T var3;
2856
2857 // concatenate Lists
2858 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2859 {
2860 clear_tv(tv1);
2861 clear_tv(tv2);
2862 return FAIL;
2863 }
2864 clear_tv(tv1);
2865 *tv1 = var3;
2866 return OK;
2867}
2868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869/*
2870 * Handle fourth level expression:
2871 * + number addition
2872 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002873 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002874 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 *
2876 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002877 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 *
2879 * Return OK or FAIL.
2880 */
2881 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 /*
2885 * Get the first variable.
2886 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 return FAIL;
2889
2890 /*
2891 * Repeat computing, until no '+', '-' or '.' is following.
2892 */
2893 for (;;)
2894 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002895 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 int getnext;
2897 char_u *p;
2898 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002899 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 int concat;
2901 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002902 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002903
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002904 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002905 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002906 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002907 p = eval_next_non_blank(*arg, evalarg, &getnext);
2908 op = *p;
2909 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002910 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2911 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002913 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2914 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002915
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002916 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002917 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002918 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002919 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002920 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002921 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002922 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002923 {
mityu4ac198c2021-05-28 17:52:40 +02002924 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002925 clear_tv(rettv);
2926 return FAIL;
2927 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002928 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002929 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002930 if ((op != '+' || (rettv->v_type != VAR_LIST
2931 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002932#ifdef FEAT_FLOAT
2933 && (op == '.' || rettv->v_type != VAR_FLOAT)
2934#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002935 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002936 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002937 int error = FALSE;
2938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002939 // For "list + ...", an illegal use of the first operand as
2940 // a number cannot be determined before evaluating the 2nd
2941 // operand: if this is also a list, all is ok.
2942 // For "something . ...", "something - ..." or "non-list + ...",
2943 // we know that the first operand needs to be a string or number
2944 // without evaluating the 2nd operand. So check before to avoid
2945 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002946 if (op != '.')
2947 tv_get_number_chk(rettv, &error);
2948 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002949 {
2950 clear_tv(rettv);
2951 return FAIL;
2952 }
2953 }
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 /*
2956 * Get the second variable.
2957 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002958 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002959 {
mityu89dcb4d2021-05-28 13:50:17 +02002960 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002961 clear_tv(rettv);
2962 return FAIL;
2963 }
2964 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002965 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 return FAIL;
2969 }
2970
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
2973 /*
2974 * Compute the result.
2975 */
2976 if (op == '.')
2977 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002978 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2979 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002980 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981
Bram Moolenaar2e086612020-08-25 22:37:48 +02002982 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002983 || var2.v_type == VAR_CHANNEL
2984 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002985 semsg(_(e_using_invalid_value_as_string_str),
2986 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002987#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002988 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002989 {
2990 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2991 var2.vval.v_float);
2992 s2 = buf2;
2993 }
2994#endif
2995 else
2996 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 {
2999 clear_tv(rettv);
3000 clear_tv(&var2);
3001 return FAIL;
3002 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 clear_tv(rettv);
3005 rettv->v_type = VAR_STRING;
3006 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003008 else if (op == '+' && rettv->v_type == VAR_BLOB
3009 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003011 else if (op == '+' && rettv->v_type == VAR_LIST
3012 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003013 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003015 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 else
3018 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003019 int error = FALSE;
3020 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003022 float_T f1 = 0, f2 = 0;
3023
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003025 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026 f1 = rettv->vval.v_float;
3027 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003028 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029 else
3030#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003032 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033 if (error)
3034 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003035 // This can only happen for "list + non-list" or
3036 // "blob + non-blob". For "non-list + ..." or
3037 // "something - ...", we returned before evaluating the
3038 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003040 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003041 return FAIL;
3042 }
3043#ifdef FEAT_FLOAT
3044 if (var2.v_type == VAR_FLOAT)
3045 f1 = n1;
3046#endif
3047 }
3048#ifdef FEAT_FLOAT
3049 if (var2.v_type == VAR_FLOAT)
3050 {
3051 f2 = var2.vval.v_float;
3052 n2 = 0;
3053 }
3054 else
3055#endif
3056 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003057 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058 if (error)
3059 {
3060 clear_tv(rettv);
3061 clear_tv(&var2);
3062 return FAIL;
3063 }
3064#ifdef FEAT_FLOAT
3065 if (rettv->v_type == VAR_FLOAT)
3066 f2 = n2;
3067#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070
3071#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003072 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3074 {
3075 if (op == '+')
3076 f1 = f1 + f2;
3077 else
3078 f1 = f1 - f2;
3079 rettv->v_type = VAR_FLOAT;
3080 rettv->vval.v_float = f1;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003083#endif
3084 {
3085 if (op == '+')
3086 n1 = n1 + n2;
3087 else
3088 n1 = n1 - n2;
3089 rettv->v_type = VAR_NUMBER;
3090 rettv->vval.v_number = n1;
3091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
3095 }
3096 return OK;
3097}
3098
3099/*
3100 * Handle fifth level expression:
3101 * * number multiplication
3102 * / number division
3103 * % number modulo
3104 *
3105 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003106 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 *
3108 * Return OK or FAIL.
3109 */
3110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003111eval6(
3112 char_u **arg,
3113 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003118 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /*
3122 * Get the first variable.
3123 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003124 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 return FAIL;
3126
3127 /*
3128 * Repeat computing, until no '*', '/' or '%' is following.
3129 */
3130 for (;;)
3131 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003132 int evaluate;
3133 int getnext;
3134 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003135 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003136 int op;
3137 varnumber_T n1, n2;
3138#ifdef FEAT_FLOAT
3139 float_T f1, f2;
3140#endif
3141 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003143 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003144 p = eval_next_non_blank(*arg, evalarg, &getnext);
3145 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003146 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003148
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003149 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003150 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003151 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003152 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003153 {
3154 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3155 {
mityu4ac198c2021-05-28 17:52:40 +02003156 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003157 clear_tv(rettv);
3158 return FAIL;
3159 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003160 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003163#ifdef FEAT_FLOAT
3164 f1 = 0;
3165 f2 = 0;
3166#endif
3167 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003168 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#ifdef FEAT_FLOAT
3171 if (rettv->v_type == VAR_FLOAT)
3172 {
3173 f1 = rettv->vval.v_float;
3174 use_float = TRUE;
3175 n1 = 0;
3176 }
3177 else
3178#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003181 if (error)
3182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 else
3185 n1 = 0;
3186
3187 /*
3188 * Get the second variable.
3189 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003190 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3191 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003192 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003193 clear_tv(rettv);
3194 return FAIL;
3195 }
3196 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003197 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 return FAIL;
3199
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202#ifdef FEAT_FLOAT
3203 if (var2.v_type == VAR_FLOAT)
3204 {
3205 if (!use_float)
3206 {
3207 f1 = n1;
3208 use_float = TRUE;
3209 }
3210 f2 = var2.vval.v_float;
3211 n2 = 0;
3212 }
3213 else
3214#endif
3215 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003216 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003217 clear_tv(&var2);
3218 if (error)
3219 return FAIL;
3220#ifdef FEAT_FLOAT
3221 if (use_float)
3222 f2 = n2;
3223#endif
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 /*
3227 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230#ifdef FEAT_FLOAT
3231 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 if (op == '*')
3234 f1 = f1 * f2;
3235 else if (op == '/')
3236 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003237# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003238 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003239 if (f2 == 0.0)
3240 {
3241 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003242 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003243 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003244 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003245 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003246 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003247 }
3248 else
3249 f1 = f1 / f2;
3250# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 // We rely on the floating point library to handle divide
3252 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003253 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003254# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003257 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003259 return FAIL;
3260 }
3261 rettv->v_type = VAR_FLOAT;
3262 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003267 int failed = FALSE;
3268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003269 if (op == '*')
3270 n1 = n1 * n2;
3271 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003272 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003274 n1 = num_modulus(n1, n2, &failed);
3275 if (failed)
3276 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003278 rettv->v_type = VAR_NUMBER;
3279 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 }
3283
3284 return OK;
3285}
3286
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003287/*
3288 * Handle a type cast before a base level expression.
3289 * "arg" must point to the first non-white of the expression.
3290 * "arg" is advanced to just after the recognized expression.
3291 * Return OK or FAIL.
3292 */
3293 static int
3294eval7t(
3295 char_u **arg,
3296 typval_T *rettv,
3297 evalarg_T *evalarg,
3298 int want_string) // after "." operator
3299{
3300 type_T *want_type = NULL;
3301 garray_T type_list; // list of pointers to allocated types
3302 int res;
3303 int evaluate = evalarg == NULL ? 0
3304 : (evalarg->eval_flags & EVAL_EVALUATE);
3305
3306 // Recognize <type> in Vim9 script only.
3307 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3308 {
3309 ++*arg;
3310 ga_init2(&type_list, sizeof(type_T *), 10);
3311 want_type = parse_type(arg, &type_list, TRUE);
3312 if (want_type == NULL && (evaluate || **arg != '>'))
3313 {
3314 clear_type_list(&type_list);
3315 return FAIL;
3316 }
3317
3318 if (**arg != '>')
3319 {
3320 if (*skipwhite(*arg) == '>')
3321 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3322 else
3323 emsg(_(e_missing_gt));
3324 clear_type_list(&type_list);
3325 return FAIL;
3326 }
3327 ++*arg;
3328 *arg = skipwhite_and_linebreak(*arg, evalarg);
3329 }
3330
3331 res = eval7(arg, rettv, evalarg, want_string);
3332
3333 if (want_type != NULL && evaluate)
3334 {
3335 if (res == OK)
3336 {
3337 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3338
3339 if (!equal_type(want_type, actual))
3340 {
3341 if (want_type == &t_bool && actual != &t_bool
3342 && (actual->tt_flags & TTFLAG_BOOL_OK))
3343 {
3344 int n = tv2bool(rettv);
3345
3346 // can use "0" and "1" for boolean in some places
3347 clear_tv(rettv);
3348 rettv->v_type = VAR_BOOL;
3349 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3350 }
3351 else
3352 {
3353 where_T where;
3354
3355 where.wt_index = 0;
3356 where.wt_variable = TRUE;
3357 res = check_type(want_type, actual, TRUE, where);
3358 }
3359 }
3360 }
3361 clear_type_list(&type_list);
3362 }
3363
3364 return res;
3365}
3366
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003367 int
3368eval_leader(char_u **arg, int vim9)
3369{
3370 char_u *s = *arg;
3371 char_u *p = *arg;
3372
3373 while (*p == '!' || *p == '-' || *p == '+')
3374 {
3375 char_u *n = skipwhite(p + 1);
3376
3377 // ++, --, -+ and +- are not accepted in Vim9 script
3378 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3379 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003380 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003381 return FAIL;
3382 }
3383 p = n;
3384 }
3385 *arg = p;
3386 return OK;
3387}
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389/*
3390 * Handle sixth level expression:
3391 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003392 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003393 * "string" string constant
3394 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 * &option-name option value
3396 * @r register contents
3397 * identifier variable value
3398 * function() function call
3399 * $VAR environment variable
3400 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003403 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003404 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 *
3406 * Also handle:
3407 * ! in front logical NOT
3408 * - in front unary minus
3409 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 * trailing [] subscript in String or List
3411 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003412 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 *
3414 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003415 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 *
3417 * Return OK or FAIL.
3418 */
3419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003420eval7(
3421 char_u **arg,
3422 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003423 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003426 int evaluate = evalarg != NULL
3427 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 int len;
3429 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *start_leader, *end_leader;
3431 int ret = OK;
3432 char_u *alias;
3433
3434 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003436 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003438 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003441 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
3443 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003444 if (eval_leader(arg, in_vim9script()) == FAIL)
3445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 end_leader = *arg;
3447
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003448 if (**arg == '.' && (!isdigit(*(*arg + 1))
3449#ifdef FEAT_FLOAT
3450 || current_sctx.sc_version < 2
3451#endif
3452 ))
3453 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003454 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003455 ++*arg;
3456 return FAIL;
3457 }
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 switch (**arg)
3460 {
3461 /*
3462 * Number constant.
3463 */
3464 case '0':
3465 case '1':
3466 case '2':
3467 case '3':
3468 case '4':
3469 case '5':
3470 case '6':
3471 case '7':
3472 case '8':
3473 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003474 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003475
3476 // Apply prefixed "-" and "+" now. Matters especially when
3477 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003478 if (ret == OK && evaluate && end_leader > start_leader
3479 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003480 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 break;
3482
3483 /*
3484 * String constant: "string".
3485 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003486 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 break;
3488
3489 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003490 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 break;
3494
3495 /*
3496 * List: [expr, expr]
3497 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003498 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
3501 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003502 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003503 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003504 case '#': if (in_vim9script())
3505 {
3506 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3507 }
3508 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003509 {
3510 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003511 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003512 }
3513 else
3514 ret = NOTDONE;
3515 break;
3516
3517 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003518 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003519 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003520 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003521 case '{': if (in_vim9script())
3522 ret = NOTDONE;
3523 else
3524 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003525 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003526 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 break;
3528
3529 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003530 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003532 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534
3535 /*
3536 * Environment variable: $VAR.
3537 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003538 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 break;
3540
3541 /*
3542 * Register contents: @r.
3543 */
3544 case '@': ++*arg;
3545 if (evaluate)
3546 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003547 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3548 semsg(_(e_syntax_error_at_str), *arg);
3549 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3550 emsg_invreg(**arg);
3551 else
3552 {
3553 rettv->v_type = VAR_STRING;
3554 rettv->vval.v_string = get_reg_contents(**arg,
3555 GREG_EXPR_SRC);
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 if (**arg != NUL)
3559 ++*arg;
3560 break;
3561
3562 /*
3563 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003564 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003566 case '(': ret = NOTDONE;
3567 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003568 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003569 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003570 if (ret == OK && evaluate)
3571 {
3572 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3573
Bram Moolenaara9931532021-06-12 15:58:16 +02003574 // Compile it here to get the return type. The return
3575 // type is optional, when it's missing use t_unknown.
3576 // This is recognized in compile_return().
3577 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3578 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003579 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003580 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003581 {
3582 clear_tv(rettv);
3583 ret = FAIL;
3584 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003585 }
3586 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003587 if (ret == NOTDONE)
3588 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003589 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003590 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003591
Bram Moolenaar9215f012020-06-27 21:18:00 +02003592 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003593 if (**arg == ')')
3594 ++*arg;
3595 else if (ret == OK)
3596 {
3597 emsg(_(e_missing_close));
3598 clear_tv(rettv);
3599 ret = FAIL;
3600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 break;
3603
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 break;
3606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607
3608 if (ret == NOTDONE)
3609 {
3610 /*
3611 * Must be a variable or function name.
3612 * Can also be a curly-braces kind of name: {expr}.
3613 */
3614 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003615 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 if (alias != NULL)
3617 s = alias;
3618
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003619 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 ret = FAIL;
3621 else
3622 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003623 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3624
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003625 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003626 {
3627 emsg(_(e_cannot_use_underscore_here));
3628 ret = FAIL;
3629 }
3630 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003631 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003633 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003634 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003635 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003636 else if (flags & EVAL_CONSTANT)
3637 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003639 {
3640 // get the value of "true", "false" or a variable
3641 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3642 {
3643 rettv->v_type = VAR_BOOL;
3644 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003645 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003646 }
3647 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003648 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003649 {
3650 rettv->v_type = VAR_BOOL;
3651 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003652 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003653 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003654 else if (len == 4 && in_vim9script()
3655 && STRNCMP(s, "null", 4) == 0)
3656 {
3657 rettv->v_type = VAR_SPECIAL;
3658 rettv->vval.v_number = VVAL_NULL;
3659 ret = OK;
3660 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003661 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003662 ret = eval_variable(s, len, rettv, NULL,
3663 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003664 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003665 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003666 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003667 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003668 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003669 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003672 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
3674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003675 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3676 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003677 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003678 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /*
3681 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3682 */
3683 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003684 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003685 return ret;
3686}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003688/*
3689 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003690 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003691 * Adjusts "end_leaderp" until it is at "start_leader".
3692 */
3693 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003694eval7_leader(
3695 typval_T *rettv,
3696 int numeric_only,
3697 char_u *start_leader,
3698 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003699{
3700 char_u *end_leader = *end_leaderp;
3701 int ret = OK;
3702 int error = FALSE;
3703 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003704 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003705#ifdef FEAT_FLOAT
3706 float_T f = 0.0;
3707
3708 if (rettv->v_type == VAR_FLOAT)
3709 f = rettv->vval.v_float;
3710 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003711#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003712 {
3713 while (VIM_ISWHITE(end_leader[-1]))
3714 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003715 if (in_vim9script() && end_leader[-1] == '!')
3716 val = tv2bool(rettv);
3717 else
3718 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003719 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003720 if (error)
3721 {
3722 clear_tv(rettv);
3723 ret = FAIL;
3724 }
3725 else
3726 {
3727 while (end_leader > start_leader)
3728 {
3729 --end_leader;
3730 if (*end_leader == '!')
3731 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003732 if (numeric_only)
3733 {
3734 ++end_leader;
3735 break;
3736 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737#ifdef FEAT_FLOAT
3738 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003739 {
3740 if (in_vim9script())
3741 {
3742 rettv->v_type = VAR_BOOL;
3743 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3744 }
3745 else
3746 f = !f;
3747 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748 else
3749#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003750 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003751 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003752 type = VAR_BOOL;
3753 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003754 }
3755 else if (*end_leader == '-')
3756 {
3757#ifdef FEAT_FLOAT
3758 if (rettv->v_type == VAR_FLOAT)
3759 f = -f;
3760 else
3761#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003762 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003763 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003764 type = VAR_NUMBER;
3765 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003766 }
3767 }
3768#ifdef FEAT_FLOAT
3769 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003771 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003775#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003777 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003778 if (in_vim9script())
3779 rettv->v_type = type;
3780 else
3781 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003785 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return ret;
3787}
3788
3789/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003790 * Call the function referred to in "rettv".
3791 */
3792 static int
3793call_func_rettv(
3794 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003795 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003796 typval_T *rettv,
3797 int evaluate,
3798 dict_T *selfdict,
3799 typval_T *basetv)
3800{
3801 partial_T *pt = NULL;
3802 funcexe_T funcexe;
3803 typval_T functv;
3804 char_u *s;
3805 int ret;
3806
3807 // need to copy the funcref so that we can clear rettv
3808 if (evaluate)
3809 {
3810 functv = *rettv;
3811 rettv->v_type = VAR_UNKNOWN;
3812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003813 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814 if (functv.v_type == VAR_PARTIAL)
3815 {
3816 pt = functv.vval.v_partial;
3817 s = partial_name(pt);
3818 }
3819 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003820 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003821 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003822 if (s == NULL || *s == NUL)
3823 {
3824 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003825 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003826 goto theend;
3827 }
3828 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003829 }
3830 else
3831 s = (char_u *)"";
3832
Bram Moolenaara80faa82020-04-12 19:37:17 +02003833 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003834 funcexe.firstline = curwin->w_cursor.lnum;
3835 funcexe.lastline = curwin->w_cursor.lnum;
3836 funcexe.evaluate = evaluate;
3837 funcexe.partial = pt;
3838 funcexe.selfdict = selfdict;
3839 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003840 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003841
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003842theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003843 // Clear the funcref afterwards, so that deleting it while
3844 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 if (evaluate)
3846 clear_tv(&functv);
3847
3848 return ret;
3849}
3850
3851/*
3852 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003853 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003854 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3855 */
3856 static int
3857eval_lambda(
3858 char_u **arg,
3859 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003860 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003863 int evaluate = evalarg != NULL
3864 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003865 typval_T base = *rettv;
3866 int ret;
3867
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003868 rettv->v_type = VAR_UNKNOWN;
3869
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003870 if (**arg == '{')
3871 {
3872 // ->{lambda}()
3873 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3874 }
3875 else
3876 {
3877 // ->(lambda)()
3878 ++*arg;
3879 ret = eval1(arg, rettv, evalarg);
3880 *arg = skipwhite_and_linebreak(*arg, evalarg);
3881 if (**arg != ')')
3882 {
3883 emsg(_(e_missing_close));
3884 ret = FAIL;
3885 }
3886 ++*arg;
3887 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003888 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003889 return FAIL;
3890 else if (**arg != '(')
3891 {
3892 if (verbose)
3893 {
3894 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003895 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003896 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003898 }
3899 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003900 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003901 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003902 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003903 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003904
3905 // Clear the funcref afterwards, so that deleting it while
3906 // evaluating the arguments is possible (see test55).
3907 if (evaluate)
3908 clear_tv(&base);
3909
3910 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003911}
3912
3913/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003915 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003916 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3917 */
3918 static int
3919eval_method(
3920 char_u **arg,
3921 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003922 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003923 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003924{
3925 char_u *name;
3926 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003927 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003928 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003929 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003930 int evaluate = evalarg != NULL
3931 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003933 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003936 len = get_name_len(arg, &alias, evaluate, TRUE);
3937 if (alias != NULL)
3938 name = alias;
3939
3940 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 {
3942 if (verbose)
3943 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003944 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003945 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003946 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003947 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003948 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003949 if (**arg != '(')
3950 {
3951 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003953 ret = FAIL;
3954 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003955 else if (VIM_ISWHITE((*arg)[-1]))
3956 {
3957 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003958 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003959 ret = FAIL;
3960 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003961 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003962 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003963 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003964 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003965
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003966 // Clear the funcref afterwards, so that deleting it while
3967 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003968 if (evaluate)
3969 clear_tv(&base);
3970
Bram Moolenaarac92e252019-08-03 21:58:38 +02003971 return ret;
3972}
3973
3974/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003975 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3976 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003977 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3978 */
3979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980eval_index(
3981 char_u **arg,
3982 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003983 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003984 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003985{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003986 int evaluate = evalarg != NULL
3987 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003988 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003991 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003992 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003993 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003995 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3996 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003997
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003998 init_tv(&var1);
3999 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004000 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004002 /*
4003 * dict.name
4004 */
4005 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004006 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004007 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004009 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004010 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004011 }
4012 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004013 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004014 /*
4015 * something[idx]
4016 *
4017 * Get the (first) variable from inside the [].
4018 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004019 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 if (**arg == ':')
4021 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004022 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004024 else if (vim9 && **arg == ':')
4025 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004026 semsg(_(e_white_space_required_before_and_after_str_at_str),
4027 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004028 clear_tv(&var1);
4029 return FAIL;
4030 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004031 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004033#ifdef FEAT_FLOAT
4034 // allow for indexing with float
4035 if (vim9 && rettv->v_type == VAR_DICT
4036 && var1.v_type == VAR_FLOAT)
4037 {
4038 var1.vval.v_string = typval_tostring(&var1, TRUE);
4039 var1.v_type = VAR_STRING;
4040 }
4041#endif
4042 if (tv_get_string_chk(&var1) == NULL)
4043 {
4044 // not a number or string
4045 clear_tv(&var1);
4046 return FAIL;
4047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049
4050 /*
4051 * Get the second variable from inside the [:].
4052 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004053 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 if (**arg == ':')
4055 {
4056 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004057 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004058 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004059 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004060 semsg(_(e_white_space_required_before_and_after_str_at_str),
4061 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004062 if (!empty1)
4063 clear_tv(&var1);
4064 return FAIL;
4065 }
4066 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 if (**arg == ']')
4068 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004069 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 if (!empty1)
4072 clear_tv(&var1);
4073 return FAIL;
4074 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004075 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 if (!empty1)
4079 clear_tv(&var1);
4080 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004081 return FAIL;
4082 }
4083 }
4084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004085 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004086 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004087 if (**arg != ']')
4088 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004089 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004090 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004091 clear_tv(&var1);
4092 if (range)
4093 clear_tv(&var2);
4094 return FAIL;
4095 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004096 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 }
4098
4099 if (evaluate)
4100 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004102 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004103 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004104
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004105 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004107 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004108 clear_tv(&var2);
4109 return res;
4110 }
4111 return OK;
4112}
4113
4114/*
4115 * Check if "rettv" can have an [index] or [sli:ce]
4116 */
4117 int
4118check_can_index(typval_T *rettv, int evaluate, int verbose)
4119{
4120 switch (rettv->v_type)
4121 {
4122 case VAR_FUNC:
4123 case VAR_PARTIAL:
4124 if (verbose)
4125 emsg(_("E695: Cannot index a Funcref"));
4126 return FAIL;
4127 case VAR_FLOAT:
4128#ifdef FEAT_FLOAT
4129 if (verbose)
4130 emsg(_(e_float_as_string));
4131 return FAIL;
4132#endif
4133 case VAR_BOOL:
4134 case VAR_SPECIAL:
4135 case VAR_JOB:
4136 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004137 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004138 if (verbose)
4139 emsg(_(e_cannot_index_special_variable));
4140 return FAIL;
4141 case VAR_UNKNOWN:
4142 case VAR_ANY:
4143 case VAR_VOID:
4144 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 emsg(_(e_cannot_index_special_variable));
4147 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004148 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004150
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004151 case VAR_STRING:
4152 case VAR_LIST:
4153 case VAR_DICT:
4154 case VAR_BLOB:
4155 break;
4156 case VAR_NUMBER:
4157 if (in_vim9script())
4158 emsg(_(e_cannot_index_number));
4159 break;
4160 }
4161 return OK;
4162}
4163
4164/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004165 * slice() function
4166 */
4167 void
4168f_slice(typval_T *argvars, typval_T *rettv)
4169{
4170 if (check_can_index(argvars, TRUE, FALSE) == OK)
4171 {
4172 copy_tv(argvars, rettv);
4173 eval_index_inner(rettv, TRUE, argvars + 1,
4174 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4175 TRUE, NULL, 0, FALSE);
4176 }
4177}
4178
4179/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004180 * Apply index or range to "rettv".
4181 * "var1" is the first index, NULL for [:expr].
4182 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004183 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4184 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004185 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4186 */
4187 int
4188eval_index_inner(
4189 typval_T *rettv,
4190 int is_range,
4191 typval_T *var1,
4192 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004193 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004194 char_u *key,
4195 int keylen,
4196 int verbose)
4197{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004198 varnumber_T n1, n2 = 0;
4199 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004200
4201 n1 = 0;
4202 if (var1 != NULL && rettv->v_type != VAR_DICT)
4203 n1 = tv_get_number(var1);
4204
4205 if (is_range)
4206 {
4207 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 if (verbose)
4210 emsg(_(e_cannot_slice_dictionary));
4211 return FAIL;
4212 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004213 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004214 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004215 else
4216 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004217 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004218
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004219 switch (rettv->v_type)
4220 {
4221 case VAR_UNKNOWN:
4222 case VAR_ANY:
4223 case VAR_VOID:
4224 case VAR_FUNC:
4225 case VAR_PARTIAL:
4226 case VAR_FLOAT:
4227 case VAR_BOOL:
4228 case VAR_SPECIAL:
4229 case VAR_JOB:
4230 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004231 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 break; // not evaluating, skipping over subscript
4233
4234 case VAR_NUMBER:
4235 case VAR_STRING:
4236 {
4237 char_u *s = tv_get_string(rettv);
4238
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004240 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004241 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004243 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004244 else
4245 s = char_from_string(s, n1);
4246 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 // The resulting variable is a substring. If the indexes
4250 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251 if (n1 < 0)
4252 {
4253 n1 = len + n1;
4254 if (n1 < 0)
4255 n1 = 0;
4256 }
4257 if (n2 < 0)
4258 n2 = len + n2;
4259 else if (n2 >= len)
4260 n2 = len;
4261 if (n1 >= len || n2 < 0 || n1 > n2)
4262 s = NULL;
4263 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004264 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 }
4266 else
4267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004268 // The resulting variable is a string of a single
4269 // character. If the index is too big or negative the
4270 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 if (n1 >= len || n1 < 0)
4272 s = NULL;
4273 else
4274 s = vim_strnsave(s + n1, 1);
4275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(rettv);
4277 rettv->v_type = VAR_STRING;
4278 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004279 }
4280 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004282 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004283 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4284 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004285 break;
4286
4287 case VAR_LIST:
4288 if (var1 == NULL)
4289 n1 = 0;
4290 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004291 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004292 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004293 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004294 return FAIL;
4295 break;
4296
4297 case VAR_DICT:
4298 {
4299 dictitem_T *item;
4300 typval_T tmp;
4301
4302 if (key == NULL)
4303 {
4304 key = tv_get_string_chk(var1);
4305 if (key == NULL)
4306 return FAIL;
4307 }
4308
4309 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4310
4311 if (item == NULL && verbose)
4312 semsg(_(e_dictkey), key);
4313 if (item == NULL)
4314 return FAIL;
4315
4316 copy_tv(&item->di_tv, &tmp);
4317 clear_tv(rettv);
4318 *rettv = tmp;
4319 }
4320 break;
4321 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004322 return OK;
4323}
4324
4325/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004326 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004327 */
4328 char_u *
4329partial_name(partial_T *pt)
4330{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004331 if (pt != NULL)
4332 {
4333 if (pt->pt_name != NULL)
4334 return pt->pt_name;
4335 if (pt->pt_func != NULL)
4336 return pt->pt_func->uf_name;
4337 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004338 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004339}
4340
Bram Moolenaarddecc252016-04-06 22:59:37 +02004341 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004342partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004343{
4344 int i;
4345
4346 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004347 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004348 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004349 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004350 if (pt->pt_name != NULL)
4351 {
4352 func_unref(pt->pt_name);
4353 vim_free(pt->pt_name);
4354 }
4355 else
4356 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004357
Bram Moolenaar54656012021-06-09 20:50:46 +02004358 // "out_up" is no longer used, decrement refcount on partial that owns it.
4359 partial_unref(pt->pt_outer.out_up_partial);
4360
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004361 // Decrease the reference count for the context of a closure. If down
4362 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004363 if (pt->pt_funcstack != NULL)
4364 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004365 --pt->pt_funcstack->fs_refcount;
4366 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004367 }
4368
Bram Moolenaarddecc252016-04-06 22:59:37 +02004369 vim_free(pt);
4370}
4371
4372/*
4373 * Unreference a closure: decrement the reference count and free it when it
4374 * becomes zero.
4375 */
4376 void
4377partial_unref(partial_T *pt)
4378{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004379 if (pt != NULL)
4380 {
4381 if (--pt->pt_refcount <= 0)
4382 partial_free(pt);
4383
4384 // If the reference count goes down to one, the funcstack may be the
4385 // only reference and can be freed if no other partials reference it.
4386 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4387 funcstack_check_refcount(pt->pt_funcstack);
4388 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004389}
4390
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004392 * Return the next (unique) copy ID.
4393 * Used for serializing nested structures.
4394 */
4395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004397{
4398 current_copyID += COPYID_INC;
4399 return current_copyID;
4400}
4401
4402/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004403 * Garbage collection for lists and dictionaries.
4404 *
4405 * We use reference counts to be able to free most items right away when they
4406 * are no longer used. But for composite items it's possible that it becomes
4407 * unused while the reference count is > 0: When there is a recursive
4408 * reference. Example:
4409 * :let l = [1, 2, 3]
4410 * :let d = {9: l}
4411 * :let l[1] = d
4412 *
4413 * Since this is quite unusual we handle this with garbage collection: every
4414 * once in a while find out which lists and dicts are not referenced from any
4415 * variable.
4416 *
4417 * Here is a good reference text about garbage collection (refers to Python
4418 * but it applies to all reference-counting mechanisms):
4419 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004420 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004421
4422/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004423 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004424 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004425 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004426 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004427 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004428garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004429{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004430 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004431 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004432 buf_T *buf;
4433 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004434 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004435 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004436
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004437 if (!testing)
4438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004439 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004440 want_garbage_collect = FALSE;
4441 may_garbage_collect = FALSE;
4442 garbage_collect_at_exit = FALSE;
4443 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004444
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004445 // The execution stack can grow big, limit the size.
4446 if (exestack.ga_maxlen - exestack.ga_len > 500)
4447 {
4448 size_t new_len;
4449 char_u *pp;
4450 int n;
4451
4452 // Keep 150% of the current size, with a minimum of the growth size.
4453 n = exestack.ga_len / 2;
4454 if (n < exestack.ga_growsize)
4455 n = exestack.ga_growsize;
4456
4457 // Don't make it bigger though.
4458 if (exestack.ga_len + n < exestack.ga_maxlen)
4459 {
4460 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4461 pp = vim_realloc(exestack.ga_data, new_len);
4462 if (pp == NULL)
4463 return FAIL;
4464 exestack.ga_maxlen = exestack.ga_len + n;
4465 exestack.ga_data = pp;
4466 }
4467 }
4468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 // We advance by two because we add one for items referenced through
4470 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004471 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004473 /*
4474 * 1. Go through all accessible variables and mark all lists and dicts
4475 * with copyID.
4476 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // Don't free variables in the previous_funccal list unless they are only
4479 // referenced through previous_funccal. This must be first, because if
4480 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004484 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004487 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004488 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4489 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004492 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004493 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4494 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004495 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004496 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4497 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004498#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004499 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004500 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4501 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004502 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004503 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004504 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4505 NULL, NULL);
4506#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004508 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004509 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004510 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4511 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004513 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004515 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004519 abort = abort || set_ref_in_functions(copyID);
4520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004522 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004525 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004526
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004527 // callbacks in buffers
4528 abort = abort || set_ref_in_buffers(copyID);
4529
Bram Moolenaar1dced572012-04-05 16:54:08 +02004530#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004531 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004532#endif
4533
Bram Moolenaardb913952012-06-29 12:54:53 +02004534#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004536#endif
4537
4538#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004540#endif
4541
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004542#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004543 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004544 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004545#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004546#ifdef FEAT_NETBEANS_INTG
4547 abort = abort || set_ref_in_nb_channel(copyID);
4548#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004549
Bram Moolenaare3188e22016-05-31 21:13:04 +02004550#ifdef FEAT_TIMERS
4551 abort = abort || set_ref_in_timer(copyID);
4552#endif
4553
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004554#ifdef FEAT_QUICKFIX
4555 abort = abort || set_ref_in_quickfix(copyID);
4556#endif
4557
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004558#ifdef FEAT_TERMINAL
4559 abort = abort || set_ref_in_term(copyID);
4560#endif
4561
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004562#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004563 abort = abort || set_ref_in_popups(copyID);
4564#endif
4565
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004566 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004567 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004568 /*
4569 * 2. Free lists and dictionaries that are not referenced.
4570 */
4571 did_free = free_unref_items(copyID);
4572
4573 /*
4574 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004575 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004576 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004578 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004579 else if (p_verbose > 0)
4580 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004581 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004582 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004583
4584 return did_free;
4585}
4586
4587/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004589 */
4590 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004591free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004592{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004593 int did_free = FALSE;
4594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004595 // Let all "free" functions know that we are here. This means no
4596 // dictionaries, lists, channels or jobs are to be freed, because we will
4597 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004598 in_free_unref_items = TRUE;
4599
4600 /*
4601 * PASS 1: free the contents of the items. We don't free the items
4602 * themselves yet, so that it is possible to decrement refcount counters
4603 */
4604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004605 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004606 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004608 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004609 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004610
4611#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004612 // Go through the list of jobs and free items without the copyID. This
4613 // must happen before doing channels, because jobs refer to channels, but
4614 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004615 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004617 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004618 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4619#endif
4620
4621 /*
4622 * PASS 2: free the items themselves.
4623 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004624 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004625 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004626
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // Go through the list of jobs and free items without the copyID. This
4629 // must happen before doing channels, because jobs refer to channels, but
4630 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631 free_unused_jobs(copyID, COPYID_MASK);
4632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004634 free_unused_channels(copyID, COPYID_MASK);
4635#endif
4636
4637 in_free_unref_items = FALSE;
4638
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004639 return did_free;
4640}
4641
4642/*
4643 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644 * "list_stack" is used to add lists to be marked. Can be NULL.
4645 *
4646 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004647 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004649set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004650{
4651 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004652 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004653 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004654 hashtab_T *cur_ht;
4655 ht_stack_T *ht_stack = NULL;
4656 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004657
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004658 cur_ht = ht;
4659 for (;;)
4660 {
4661 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 // Mark each item in the hashtab. If the item contains a hashtab
4664 // it is added to ht_stack, if it contains a list it is added to
4665 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004666 todo = (int)cur_ht->ht_used;
4667 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4668 if (!HASHITEM_EMPTY(hi))
4669 {
4670 --todo;
4671 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4672 &ht_stack, list_stack);
4673 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004674 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004675
4676 if (ht_stack == NULL)
4677 break;
4678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 cur_ht = ht_stack->ht;
4681 tempitem = ht_stack;
4682 ht_stack = ht_stack->prev;
4683 free(tempitem);
4684 }
4685
4686 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004687}
4688
4689/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004690 * Mark a dict and its items with "copyID".
4691 * Returns TRUE if setting references failed somehow.
4692 */
4693 int
4694set_ref_in_dict(dict_T *d, int copyID)
4695{
4696 if (d != NULL && d->dv_copyID != copyID)
4697 {
4698 d->dv_copyID = copyID;
4699 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4700 }
4701 return FALSE;
4702}
4703
4704/*
4705 * Mark a list and its items with "copyID".
4706 * Returns TRUE if setting references failed somehow.
4707 */
4708 int
4709set_ref_in_list(list_T *ll, int copyID)
4710{
4711 if (ll != NULL && ll->lv_copyID != copyID)
4712 {
4713 ll->lv_copyID = copyID;
4714 return set_ref_in_list_items(ll, copyID, NULL);
4715 }
4716 return FALSE;
4717}
4718
4719/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004720 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004721 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4722 *
4723 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004724 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004726set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004727{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004728 listitem_T *li;
4729 int abort = FALSE;
4730 list_T *cur_l;
4731 list_stack_T *list_stack = NULL;
4732 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004733
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004734 cur_l = l;
4735 for (;;)
4736 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004737 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004738 // Mark each item in the list. If the item contains a hashtab
4739 // it is added to ht_stack, if it contains a list it is added to
4740 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4742 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4743 ht_stack, &list_stack);
4744 if (list_stack == NULL)
4745 break;
4746
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004748 cur_l = list_stack->list;
4749 tempitem = list_stack;
4750 list_stack = list_stack->prev;
4751 free(tempitem);
4752 }
4753
4754 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004755}
4756
4757/*
4758 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004759 * "list_stack" is used to add lists to be marked. Can be NULL.
4760 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4761 *
4762 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004763 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004765set_ref_in_item(
4766 typval_T *tv,
4767 int copyID,
4768 ht_stack_T **ht_stack,
4769 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004770{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004771 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004772
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004773 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004774 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 dict_T *dd = tv->vval.v_dict;
4776
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 if (dd != NULL && dd->dv_copyID != copyID)
4778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 dd->dv_copyID = copyID;
4781 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004782 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004783 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4784 }
4785 else
4786 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004787 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4788
Bram Moolenaara03f2332016-02-06 18:09:59 +01004789 if (newitem == NULL)
4790 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004791 else
4792 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004793 newitem->ht = &dd->dv_hashtab;
4794 newitem->prev = *ht_stack;
4795 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004796 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004797 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004798 }
4799 }
4800 else if (tv->v_type == VAR_LIST)
4801 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004802 list_T *ll = tv->vval.v_list;
4803
Bram Moolenaara03f2332016-02-06 18:09:59 +01004804 if (ll != NULL && ll->lv_copyID != copyID)
4805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004807 ll->lv_copyID = copyID;
4808 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004809 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004810 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004811 }
4812 else
4813 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004814 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4815
Bram Moolenaara03f2332016-02-06 18:09:59 +01004816 if (newitem == NULL)
4817 abort = TRUE;
4818 else
4819 {
4820 newitem->list = ll;
4821 newitem->prev = *list_stack;
4822 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004823 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004824 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004825 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004826 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004827 else if (tv->v_type == VAR_FUNC)
4828 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004829 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004830 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831 else if (tv->v_type == VAR_PARTIAL)
4832 {
4833 partial_T *pt = tv->vval.v_partial;
4834 int i;
4835
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004836 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004837 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004838 // Didn't see this partial yet.
4839 pt->pt_copyID = copyID;
4840
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004841 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004842
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 if (pt->pt_dict != NULL)
4844 {
4845 typval_T dtv;
4846
4847 dtv.v_type = VAR_DICT;
4848 dtv.vval.v_dict = pt->pt_dict;
4849 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4850 }
4851
4852 for (i = 0; i < pt->pt_argc; ++i)
4853 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4854 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004855 if (pt->pt_funcstack != NULL)
4856 {
4857 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4858
4859 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4860 abort = abort || set_ref_in_item(stack + i, copyID,
4861 ht_stack, list_stack);
4862 }
4863
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 }
4865 }
4866#ifdef FEAT_JOB_CHANNEL
4867 else if (tv->v_type == VAR_JOB)
4868 {
4869 job_T *job = tv->vval.v_job;
4870 typval_T dtv;
4871
4872 if (job != NULL && job->jv_copyID != copyID)
4873 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004874 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 if (job->jv_channel != NULL)
4876 {
4877 dtv.v_type = VAR_CHANNEL;
4878 dtv.vval.v_channel = job->jv_channel;
4879 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4880 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004881 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004882 {
4883 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004884 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004885 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4886 }
4887 }
4888 }
4889 else if (tv->v_type == VAR_CHANNEL)
4890 {
4891 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004892 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004893 typval_T dtv;
4894 jsonq_T *jq;
4895 cbq_T *cq;
4896
4897 if (ch != NULL && ch->ch_copyID != copyID)
4898 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004899 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004900 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 {
4902 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4903 jq = jq->jq_next)
4904 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4905 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4906 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004907 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 {
4909 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004910 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004911 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4912 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004913 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004914 {
4915 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004916 dtv.vval.v_partial =
4917 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004918 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4919 }
4920 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004921 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004922 {
4923 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004924 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004925 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4926 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004927 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004928 {
4929 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004930 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004931 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4932 }
4933 }
4934 }
4935#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004936 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004937}
4938
Bram Moolenaar8c711452005-01-14 21:53:12 +00004939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 * Return a string with the string representation of a variable.
4941 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004942 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004943 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004944 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004945 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004946 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004947 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4948 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004949 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004951 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953 typval_T *tv,
4954 char_u **tofree,
4955 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004956 int copyID,
4957 int echo_style,
4958 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004959 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004961 static int recurse = 0;
4962 char_u *r = NULL;
4963
Bram Moolenaar33570922005-01-25 22:26:29 +00004964 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004965 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004966 if (!did_echo_string_emsg)
4967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // Only give this message once for a recursive call to avoid
4969 // flooding the user with errors. And stop iterating over lists
4970 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004971 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004972 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004975 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 }
4977 ++recurse;
4978
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979 switch (tv->v_type)
4980 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004981 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004982 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004983 {
4984 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004985 r = tv->vval.v_string;
4986 if (r == NULL)
4987 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004988 }
4989 else
4990 {
4991 *tofree = string_quote(tv->vval.v_string, FALSE);
4992 r = *tofree;
4993 }
4994 break;
4995
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004997 if (echo_style)
4998 {
4999 *tofree = NULL;
5000 r = tv->vval.v_string;
5001 }
5002 else
5003 {
5004 *tofree = string_quote(tv->vval.v_string, TRUE);
5005 r = *tofree;
5006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005007 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005008
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005009 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005010 {
5011 partial_T *pt = tv->vval.v_partial;
5012 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005013 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005014 garray_T ga;
5015 int i;
5016 char_u *tf;
5017
5018 ga_init2(&ga, 1, 100);
5019 ga_concat(&ga, (char_u *)"function(");
5020 if (fname != NULL)
5021 {
5022 ga_concat(&ga, fname);
5023 vim_free(fname);
5024 }
5025 if (pt != NULL && pt->pt_argc > 0)
5026 {
5027 ga_concat(&ga, (char_u *)", [");
5028 for (i = 0; i < pt->pt_argc; ++i)
5029 {
5030 if (i > 0)
5031 ga_concat(&ga, (char_u *)", ");
5032 ga_concat(&ga,
5033 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5034 vim_free(tf);
5035 }
5036 ga_concat(&ga, (char_u *)"]");
5037 }
5038 if (pt != NULL && pt->pt_dict != NULL)
5039 {
5040 typval_T dtv;
5041
5042 ga_concat(&ga, (char_u *)", ");
5043 dtv.v_type = VAR_DICT;
5044 dtv.vval.v_dict = pt->pt_dict;
5045 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5046 vim_free(tf);
5047 }
5048 ga_concat(&ga, (char_u *)")");
5049
5050 *tofree = ga.ga_data;
5051 r = *tofree;
5052 break;
5053 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005054
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005055 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005056 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005057 break;
5058
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005059 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005060 if (tv->vval.v_list == NULL)
5061 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005062 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005063 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005064 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005065 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005066 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5067 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005068 {
5069 *tofree = NULL;
5070 r = (char_u *)"[...]";
5071 }
5072 else
5073 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005074 int old_copyID = tv->vval.v_list->lv_copyID;
5075
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005076 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005077 *tofree = list2string(tv, copyID, restore_copyID);
5078 if (restore_copyID)
5079 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005080 r = *tofree;
5081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005082 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005083
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005085 if (tv->vval.v_dict == NULL)
5086 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005087 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005088 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005089 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005090 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005091 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5092 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005093 {
5094 *tofree = NULL;
5095 r = (char_u *)"{...}";
5096 }
5097 else
5098 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005099 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005100
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005101 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005102 *tofree = dict2string(tv, copyID, restore_copyID);
5103 if (restore_copyID)
5104 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005105 r = *tofree;
5106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005107 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005110 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005111 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005113 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005114 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005115 break;
5116
Bram Moolenaar835dc632016-02-07 14:27:38 +01005117 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005118 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005119#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005120 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005121 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5122 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005123 if (composite_val)
5124 {
5125 *tofree = string_quote(r, FALSE);
5126 r = *tofree;
5127 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005128#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005130
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005131 case VAR_INSTR:
5132 *tofree = NULL;
5133 r = (char_u *)"instructions";
5134 break;
5135
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005136 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005137#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *tofree = NULL;
5139 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5140 r = numbuf;
5141 break;
5142#endif
5143
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005144 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005145 case VAR_SPECIAL:
5146 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005147 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005148 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005150
Bram Moolenaar8502c702014-06-17 12:51:16 +02005151 if (--recurse == 0)
5152 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154}
5155
5156/*
5157 * Return a string with the string representation of a variable.
5158 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5159 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005160 * Does not put quotes around strings, as ":echo" displays values.
5161 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5162 * May return NULL.
5163 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005165echo_string(
5166 typval_T *tv,
5167 char_u **tofree,
5168 char_u *numbuf,
5169 int copyID)
5170{
5171 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5172}
5173
5174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 * Return string "str" in ' quotes, doubling ' characters.
5176 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005179 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181{
Bram Moolenaar33570922005-01-25 22:26:29 +00005182 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 char_u *p, *r, *s;
5184
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 len = (function ? 13 : 3);
5186 if (str != NULL)
5187 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005188 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005189 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005190 if (*p == '\'')
5191 ++len;
5192 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005193 s = r = alloc(len);
5194 if (r != NULL)
5195 {
5196 if (function)
5197 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005199 r += 10;
5200 }
5201 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005203 if (str != NULL)
5204 for (p = str; *p != NUL; )
5205 {
5206 if (*p == '\'')
5207 *r++ = '\'';
5208 MB_COPY_CHAR(p, r);
5209 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005211 if (function)
5212 *r++ = ')';
5213 *r++ = NUL;
5214 }
5215 return s;
5216}
5217
5218/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005219 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5220 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005221 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005222 */
5223 int
5224buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5225{
5226 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005227 char_u *t;
5228 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005229
5230 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5231 return -1;
5232
5233 if (lnum > buf->b_ml.ml_line_count)
5234 lnum = buf->b_ml.ml_line_count;
5235
5236 str = ml_get_buf(buf, lnum, FALSE);
5237 if (str == NULL)
5238 return -1;
5239
5240 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005241 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005242
Bram Moolenaar91458462021-01-13 20:08:38 +01005243 // count the number of characters
5244 t = str;
5245 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5246 t += mb_ptr2len(t);
5247
5248 // In insert mode, when the cursor is at the end of a non-empty line,
5249 // byteidx points to the NUL character immediately past the end of the
5250 // string. In this case, add one to the character count.
5251 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5252 count++;
5253
5254 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255}
5256
5257/*
5258 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005259 * byte index. Works only for loaded buffers. Returns -1 on failure.
5260 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005261 */
5262 int
5263buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5264{
5265 char_u *str;
5266 char_u *t;
5267
5268 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5269 return -1;
5270
5271 if (lnum > buf->b_ml.ml_line_count)
5272 lnum = buf->b_ml.ml_line_count;
5273
5274 str = ml_get_buf(buf, lnum, FALSE);
5275 if (str == NULL)
5276 return -1;
5277
5278 // Convert the character offset to a byte offset
5279 t = str;
5280 while (*t != NUL && --charidx > 0)
5281 t += mb_ptr2len(t);
5282
Bram Moolenaar91458462021-01-13 20:08:38 +01005283 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005284}
5285
5286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005288 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291var2fpos(
5292 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005294 int *fnum, // set to fnum for '0, 'A, etc.
5295 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005297 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005299 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005302 if (varp->v_type == VAR_LIST)
5303 {
5304 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005305 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005306 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005307 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005308
5309 l = varp->vval.v_list;
5310 if (l == NULL)
5311 return NULL;
5312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005314 pos.lnum = list_find_nr(l, 0L, &error);
5315 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005317 if (charcol)
5318 len = (long)mb_charlen(ml_get(pos.lnum));
5319 else
5320 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005321
Bram Moolenaarec65d772020-08-20 22:29:12 +02005322 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005324 li = list_find(l, 1L);
5325 if (li != NULL && li->li_tv.v_type == VAR_STRING
5326 && li->li_tv.vval.v_string != NULL
5327 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005328 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005329 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005330 }
5331 else
5332 {
5333 pos.col = list_find_nr(l, 1L, &error);
5334 if (error)
5335 return NULL;
5336 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005339 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005341 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005344 pos.coladd = list_find_nr(l, 2L, &error);
5345 if (error)
5346 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005347
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005348 return &pos;
5349 }
5350
Bram Moolenaarc5809432021-03-27 21:23:30 +01005351 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5352 return NULL;
5353
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005354 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 if (name == NULL)
5356 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005358 {
5359 pos = curwin->w_cursor;
5360 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005361 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 return &pos;
5363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005365 {
5366 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005367 pos = VIsual;
5368 else
5369 pos = curwin->w_cursor;
5370 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005371 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005372 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005373 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005376 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5378 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005379 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005380 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 return pp;
5382 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005383
Bram Moolenaara5525202006-03-02 22:52:09 +00005384 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005385
Bram Moolenaar477933c2007-07-17 14:32:23 +00005386 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005387 {
5388 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005389 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005390 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005391 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 // In silent Ex mode topline is zero, but that's not a valid line
5393 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005394 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005395 return &pos;
5396 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005398 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005401 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005402 return &pos;
5403 }
5404 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005407 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 pos.lnum = curbuf->b_ml.ml_line_count;
5410 pos.col = 0;
5411 }
5412 else
5413 {
5414 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005415 if (charcol)
5416 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5417 else
5418 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 return &pos;
5421 }
5422 return NULL;
5423}
5424
5425/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005426 * Convert list in "arg" into a position and optional file number.
5427 * When "fnump" is NULL there is no file number, only 3 items.
5428 * Note that the column is passed on as-is, the caller may want to decrement
5429 * it to use 1 for the first column.
5430 * Return FAIL when conversion is not possible, doesn't check the position for
5431 * validity.
5432 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434list2fpos(
5435 typval_T *arg,
5436 pos_T *posp,
5437 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005438 colnr_T *curswantp,
5439 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005440{
5441 list_T *l = arg->vval.v_list;
5442 long i = 0;
5443 long n;
5444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5446 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005447 if (arg->v_type != VAR_LIST
5448 || l == NULL
5449 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005450 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451 return FAIL;
5452
5453 if (fnump != NULL)
5454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005455 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005456 if (n < 0)
5457 return FAIL;
5458 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005460 *fnump = n;
5461 }
5462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 if (n < 0)
5465 return FAIL;
5466 posp->lnum = n;
5467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005468 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005469 if (n < 0)
5470 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005471 // If character position is specified, then convert to byte position
5472 if (charcol)
5473 {
5474 buf_T *buf;
5475
5476 // Get the text for the specified line in a loaded buffer
5477 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5478 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5479 return FAIL;
5480
Bram Moolenaar91458462021-01-13 20:08:38 +01005481 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005482 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005483 posp->col = n;
5484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005486 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005487 posp->coladd = 0;
5488 else
5489 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005490
Bram Moolenaar493c1782014-05-28 14:34:46 +02005491 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005492 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005493
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005494 return OK;
5495}
5496
5497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 * Get the length of an environment variable name.
5499 * Advance "arg" to the first character after the name.
5500 * Return 0 for error.
5501 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005503get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 char_u *p;
5506 int len;
5507
5508 for (p = *arg; vim_isIDc(*p); ++p)
5509 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return 0;
5512
5513 len = (int)(p - *arg);
5514 *arg = p;
5515 return len;
5516}
5517
5518/*
5519 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005520 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * Return 0 if something is wrong.
5522 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005524get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 char_u *p;
5527 int len;
5528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005529 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005531 {
5532 if (*p == ':')
5533 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 // "s:" is start of "s:var", but "n:" is not and can be used in
5535 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005536 len = (int)(p - *arg);
5537 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5538 || len > 1)
5539 break;
5540 }
5541 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return 0;
5544
5545 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005546 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 return len;
5549}
5550
5551/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005552 * Get the length of the name of a variable or function.
5553 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005555 * Return -1 if curly braces expansion failed.
5556 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * If the name contains 'magic' {}'s, expand them and return the
5558 * expanded name in an allocated string via 'alias' - caller must free.
5559 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005561get_name_len(
5562 char_u **arg,
5563 char_u **alias,
5564 int evaluate,
5565 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *p;
5569 char_u *expr_start;
5570 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5575 && (*arg)[2] == (int)KE_SNR)
5576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005577 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 *arg += 3;
5579 return get_id_len(arg) + 3;
5580 }
5581 len = eval_fname_script(*arg);
5582 if (len > 0)
5583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 *arg += len;
5586 }
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005591 p = find_name_end(*arg, &expr_start, &expr_end,
5592 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (expr_start != NULL)
5594 {
5595 char_u *temp_string;
5596
5597 if (!evaluate)
5598 {
5599 len += (int)(p - *arg);
5600 *arg = skipwhite(p);
5601 return len;
5602 }
5603
5604 /*
5605 * Include any <SID> etc in the expanded string:
5606 * Thus the -len here.
5607 */
5608 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5609 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 *alias = temp_string;
5612 *arg = skipwhite(p);
5613 return (int)STRLEN(temp_string);
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005617 // Only give an error when there is something, otherwise it will be
5618 // reported at a higher level.
5619 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005620 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 return len;
5623}
5624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625/*
5626 * Find the end of a variable or function name, taking care of magic braces.
5627 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5628 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005629 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 * Return a pointer to just after the name. Equal to "arg" if there is no
5631 * valid name.
5632 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005634find_name_end(
5635 char_u *arg,
5636 char_u **expr_start,
5637 char_u **expr_end,
5638 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 int mb_nest = 0;
5641 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005643 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005644 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 *expr_start = NULL;
5649 *expr_end = NULL;
5650 }
5651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005652 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005653 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5654 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005655 return arg;
5656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 for (p = arg; *p != NUL
5658 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005659 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005660 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005661 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005663 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005665 if (*p == '\'')
5666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005668 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005669 ;
5670 if (*p == NUL)
5671 break;
5672 }
5673 else if (*p == '"')
5674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005676 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005677 if (*p == '\\' && p[1] != NUL)
5678 ++p;
5679 if (*p == NUL)
5680 break;
5681 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005682 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 // "s:" is start of "s:var", but "n:" is not and can be used in
5685 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005686 len = (int)(p - arg);
5687 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005688 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005689 break;
5690 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 if (*p == '[')
5695 ++br_nest;
5696 else if (*p == ']')
5697 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005699
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005700 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 if (*p == '{')
5703 {
5704 mb_nest++;
5705 if (expr_start != NULL && *expr_start == NULL)
5706 *expr_start = p;
5707 }
5708 else if (*p == '}')
5709 {
5710 mb_nest--;
5711 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5712 *expr_end = p;
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716
5717 return p;
5718}
5719
5720/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005721 * Expands out the 'magic' {}'s in a variable/function name.
5722 * Note that this can call itself recursively, to deal with
5723 * constructs like foo{bar}{baz}{bam}
5724 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5725 * "in_start" ^
5726 * "expr_start" ^
5727 * "expr_end" ^
5728 * "in_end" ^
5729 *
5730 * Returns a new allocated string, which the caller must free.
5731 * Returns NULL for failure.
5732 */
5733 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734make_expanded_name(
5735 char_u *in_start,
5736 char_u *expr_start,
5737 char_u *expr_end,
5738 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005739{
5740 char_u c1;
5741 char_u *retval = NULL;
5742 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743
5744 if (expr_end == NULL || in_end == NULL)
5745 return NULL;
5746 *expr_start = NUL;
5747 *expr_end = NUL;
5748 c1 = *in_end;
5749 *in_end = NUL;
5750
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005751 temp_result = eval_to_string(expr_start + 1, FALSE);
5752 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005753 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005754 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5755 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005756 if (retval != NULL)
5757 {
5758 STRCPY(retval, in_start);
5759 STRCAT(retval, temp_result);
5760 STRCAT(retval, expr_end + 1);
5761 }
5762 }
5763 vim_free(temp_result);
5764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005765 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005766 *expr_start = '{';
5767 *expr_end = '}';
5768
5769 if (retval != NULL)
5770 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005771 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005772 if (expr_start != NULL)
5773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005774 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005775 temp_result = make_expanded_name(retval, expr_start,
5776 expr_end, temp_result);
5777 vim_free(retval);
5778 retval = temp_result;
5779 }
5780 }
5781
5782 return retval;
5783}
5784
5785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005787 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005790eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005792 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005793}
5794
5795/*
5796 * Return TRUE if character "c" can be used as the first character in a
5797 * variable or function name (excluding '{' and '}').
5798 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005800eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005801{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005802 return ASCII_ISALPHA(c) || c == '_';
5803}
5804
5805/*
5806 * Return TRUE if character "c" can be used as the first character of a
5807 * dictionary key.
5808 */
5809 int
5810eval_isdictc(int c)
5811{
5812 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005816 * Handle:
5817 * - expr[expr], expr[expr:expr] subscript
5818 * - ".name" lookup
5819 * - function call with Funcref variable: func(expr)
5820 * - method call: var->method()
5821 *
5822 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005823 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005825handle_subscript(
5826 char_u **arg,
5827 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005828 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005829 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005830{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005831 int evaluate = evalarg != NULL
5832 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005833 int ret = OK;
5834 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005835 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005836 int getnext;
5837 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005838
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005839 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005840 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005841 // When at the end of the line and ".name" or "->{" or "->X" follows in
5842 // the next line then consume the line break.
5843 p = eval_next_non_blank(*arg, evalarg, &getnext);
5844 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005845 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005846 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5847 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5848 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005849 {
5850 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005851 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005852 check_white = FALSE;
5853 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005854
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005855 if (rettv->v_type == VAR_ANY)
5856 {
5857 char_u *exp_name;
5858 int cc;
5859 int idx;
5860 ufunc_T *ufunc;
5861 type_T *type;
5862
5863 // Found script from "import * as {name}", script item name must
5864 // follow.
5865 if (**arg != '.')
5866 {
5867 if (verbose)
5868 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5869 ret = FAIL;
5870 break;
5871 }
5872 ++*arg;
5873 if (IS_WHITE_OR_NUL(**arg))
5874 {
5875 if (verbose)
5876 emsg(_(e_no_white_space_allowed_after_dot));
5877 ret = FAIL;
5878 break;
5879 }
5880
5881 // isolate the name
5882 exp_name = *arg;
5883 while (eval_isnamec(**arg))
5884 ++*arg;
5885 cc = **arg;
5886 **arg = NUL;
5887
5888 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5889 evalarg->eval_cctx, verbose);
5890 **arg = cc;
5891 *arg = skipwhite(*arg);
5892
5893 if (idx < 0 && ufunc == NULL)
5894 {
5895 ret = FAIL;
5896 break;
5897 }
5898 if (idx >= 0)
5899 {
5900 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5901 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5902
5903 copy_tv(sv->sv_tv, rettv);
5904 }
5905 else
5906 {
5907 rettv->v_type = VAR_FUNC;
5908 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5909 }
5910 }
5911
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005912 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5913 || rettv->v_type == VAR_PARTIAL))
5914 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005915 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005916 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5917 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005918
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005919 // Stop the expression evaluation when immediately aborting on
5920 // error, or when an interrupt occurred or an exception was thrown
5921 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005922 if (aborting())
5923 {
5924 if (ret == OK)
5925 clear_tv(rettv);
5926 ret = FAIL;
5927 }
5928 dict_unref(selfdict);
5929 selfdict = NULL;
5930 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005931 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005932 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005933 if (in_vim9script())
5934 *arg = skipwhite(p + 2);
5935 else
5936 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005937 if (ret == OK)
5938 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005939 if (VIM_ISWHITE(**arg))
5940 {
5941 emsg(_(e_nowhitespace));
5942 ret = FAIL;
5943 }
5944 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005945 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005946 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005947 else
5948 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005949 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005950 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005951 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005952 // "." is ".name" lookup when we found a dict or when evaluating and
5953 // scriptversion is at least 2, where string concatenation is "..".
5954 else if (**arg == '['
5955 || (**arg == '.' && (rettv->v_type == VAR_DICT
5956 || (!evaluate
5957 && (*arg)[1] != '.'
5958 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005959 {
5960 dict_unref(selfdict);
5961 if (rettv->v_type == VAR_DICT)
5962 {
5963 selfdict = rettv->vval.v_dict;
5964 if (selfdict != NULL)
5965 ++selfdict->dv_refcount;
5966 }
5967 else
5968 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005969 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005970 {
5971 clear_tv(rettv);
5972 ret = FAIL;
5973 }
5974 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005975 else
5976 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005977 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5980 // Don't do this when "Func" is already a partial that was bound
5981 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005982 if (selfdict != NULL
5983 && (rettv->v_type == VAR_FUNC
5984 || (rettv->v_type == VAR_PARTIAL
5985 && (rettv->vval.v_partial->pt_auto
5986 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005988
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005989 dict_unref(selfdict);
5990 return ret;
5991}
5992
5993/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 * Make a copy of an item.
5995 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5997 * reference to an already copied list/dict can be used.
5998 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006001item_copy(
6002 typval_T *from,
6003 typval_T *to,
6004 int deep,
6005 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006{
6007 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006009
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006012 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 }
6015 ++recurse;
6016
6017 switch (from->v_type)
6018 {
6019 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006020 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 case VAR_STRING:
6022 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006023 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006024 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006025 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006026 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006027 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006028 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029 copy_tv(from, to);
6030 break;
6031 case VAR_LIST:
6032 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006033 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006034 if (from->vval.v_list == NULL)
6035 to->vval.v_list = NULL;
6036 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006038 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006039 to->vval.v_list = from->vval.v_list->lv_copylist;
6040 ++to->vval.v_list->lv_refcount;
6041 }
6042 else
6043 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6044 if (to->vval.v_list == NULL)
6045 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006046 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006047 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006049 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050 case VAR_DICT:
6051 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006052 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053 if (from->vval.v_dict == NULL)
6054 to->vval.v_dict = NULL;
6055 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6059 ++to->vval.v_dict->dv_refcount;
6060 }
6061 else
6062 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6063 if (to->vval.v_dict == NULL)
6064 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006066 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006067 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006068 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006069 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006070 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006071 }
6072 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006074}
6075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006076 void
6077echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6078{
6079 char_u *tofree;
6080 char_u numbuf[NUMBUFLEN];
6081 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6082
6083 if (*atstart)
6084 {
6085 *atstart = FALSE;
6086 // Call msg_start() after eval1(), evaluating the expression
6087 // may cause a message to appear.
6088 if (with_space)
6089 {
6090 // Mark the saved text as finishing the line, so that what
6091 // follows is displayed on a new line when scrolling back
6092 // at the more prompt.
6093 msg_sb_eol();
6094 msg_start();
6095 }
6096 }
6097 else if (with_space)
6098 msg_puts_attr(" ", echo_attr);
6099
6100 if (p != NULL)
6101 for ( ; *p != NUL && !got_int; ++p)
6102 {
6103 if (*p == '\n' || *p == '\r' || *p == TAB)
6104 {
6105 if (*p != TAB && *needclr)
6106 {
6107 // remove any text still there from the command
6108 msg_clr_eos();
6109 *needclr = FALSE;
6110 }
6111 msg_putchar_attr(*p, echo_attr);
6112 }
6113 else
6114 {
6115 if (has_mbyte)
6116 {
6117 int i = (*mb_ptr2len)(p);
6118
6119 (void)msg_outtrans_len_attr(p, i, echo_attr);
6120 p += i - 1;
6121 }
6122 else
6123 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6124 }
6125 }
6126 vim_free(tofree);
6127}
6128
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 * ":echo expr1 ..." print each argument separated with a space, add a
6131 * newline at the end.
6132 * ":echon expr1 ..." print each argument plain.
6133 */
6134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006139 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int needclr = TRUE;
6141 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006142 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006143 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006144 evalarg_T evalarg;
6145
Bram Moolenaare707c882020-07-01 13:07:37 +02006146 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 if (eap->skip)
6149 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006150 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006152 // If eval1() causes an error message the text from the command may
6153 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 need_clr_eos = needclr;
6155
Bram Moolenaar68db9962021-05-09 23:19:22 +02006156 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006157 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
6159 /*
6160 * Report the invalid expression unless the expression evaluation
6161 * has been cancelled due to an aborting error, an interrupt, or an
6162 * exception.
6163 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006164 if (!aborting() && did_emsg == did_emsg_before
6165 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006166 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 break;
6169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170 need_clr_eos = FALSE;
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006173 {
6174 if (rettv.v_type == VAR_VOID)
6175 {
6176 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6177 break;
6178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006179 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006180 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006181
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 arg = skipwhite(arg);
6184 }
6185 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006186 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (eap->skip)
6189 --emsg_skip;
6190 else
6191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (needclr)
6194 msg_clr_eos();
6195 if (eap->cmdidx == CMD_echo)
6196 msg_end();
6197 }
6198}
6199
6200/*
6201 * ":echohl {name}".
6202 */
6203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006204ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006206 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207}
6208
6209/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006210 * Returns the :echo attribute
6211 */
6212 int
6213get_echo_attr(void)
6214{
6215 return echo_attr;
6216}
6217
6218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * ":execute expr1 ..." execute the result of an expression.
6220 * ":echomsg expr1 ..." Print a message
6221 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006222 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 * Each gets spaces around each argument and a newline at the end for
6224 * echo commands
6225 */
6226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006227ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 int ret = OK;
6232 char_u *p;
6233 garray_T ga;
6234 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236 ga_init2(&ga, 1, 80);
6237
6238 if (eap->skip)
6239 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006240 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006242 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006243 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 if (!eap->skip)
6247 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006248 char_u buf[NUMBUFLEN];
6249
6250 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006251 {
6252 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6253 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006254 semsg(_(e_using_invalid_value_as_string_str),
6255 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006256 p = NULL;
6257 }
6258 else
6259 p = tv_get_string_buf(&rettv, buf);
6260 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006261 else
6262 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006263 if (p == NULL)
6264 {
6265 clear_tv(&rettv);
6266 ret = FAIL;
6267 break;
6268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 len = (int)STRLEN(p);
6270 if (ga_grow(&ga, len + 2) == FAIL)
6271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ret = FAIL;
6274 break;
6275 }
6276 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 ga.ga_len += len;
6280 }
6281
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 arg = skipwhite(arg);
6284 }
6285
6286 if (ret != FAIL && ga.ga_data != NULL)
6287 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006288 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006290 // Mark the already saved text as finishing the line, so that what
6291 // follows is displayed on a new line when scrolling back at the
6292 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006293 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006294 }
6295
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006297 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006298 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006299 out_flush();
6300 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006301 else if (eap->cmdidx == CMD_echoconsole)
6302 {
6303 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6304 ui_write((char_u *)"\r\n", 2, TRUE);
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 else if (eap->cmdidx == CMD_echoerr)
6307 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006308 int save_did_emsg = did_emsg;
6309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006310 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006311 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (!force_abort)
6313 did_emsg = save_did_emsg;
6314 }
6315 else if (eap->cmdidx == CMD_execute)
6316 do_cmdline((char_u *)ga.ga_data,
6317 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6318 }
6319
6320 ga_clear(&ga);
6321
6322 if (eap->skip)
6323 --emsg_skip;
6324
6325 eap->nextcmd = check_nextcmd(arg);
6326}
6327
6328/*
6329 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6330 * "arg" points to the "&" or '+' when called, to "option" when returning.
6331 * Returns NULL when no option name found. Otherwise pointer to the char
6332 * after the option name.
6333 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 char_u *p = *arg;
6338
6339 ++p;
6340 if (*p == 'g' && p[1] == ':')
6341 {
6342 *opt_flags = OPT_GLOBAL;
6343 p += 2;
6344 }
6345 else if (*p == 'l' && p[1] == ':')
6346 {
6347 *opt_flags = OPT_LOCAL;
6348 p += 2;
6349 }
6350 else
6351 *opt_flags = 0;
6352
6353 if (!ASCII_ISALPHA(*p))
6354 return NULL;
6355 *arg = p;
6356
6357 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006358 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 else
6360 while (ASCII_ISALPHA(*p))
6361 ++p;
6362 return p;
6363}
6364
6365/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006366 * Display script name where an item was last set.
6367 * Should only be invoked when 'verbose' is non-zero.
6368 */
6369 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006370last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006371{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006372 char_u *p;
6373
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006374 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006375 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006376 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006377 if (p != NULL)
6378 {
6379 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006380 msg_puts(_("\n\tLast set from "));
6381 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006382 if (script_ctx.sc_lnum > 0)
6383 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006384 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006385 msg_outnum((long)script_ctx.sc_lnum);
6386 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006387 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006388 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006389 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006390 }
6391}
6392
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006393#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395/*
6396 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006397 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * "flags" can be "g" to do a global substitute.
6399 * Returns an allocated string, NULL for error.
6400 */
6401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402do_string_sub(
6403 char_u *str,
6404 char_u *pat,
6405 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006406 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 int sublen;
6410 regmatch_T regmatch;
6411 int i;
6412 int do_all;
6413 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006414 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 garray_T ga;
6416 char_u *ret;
6417 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006418 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006422 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424 ga_init2(&ga, 1, 200);
6425
6426 do_all = (flags[0] == 'g');
6427
6428 regmatch.rm_ic = p_ic;
6429 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6430 if (regmatch.regprog != NULL)
6431 {
6432 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006433 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006437 if (regmatch.startp[0] == regmatch.endp[0])
6438 {
6439 if (zero_width == regmatch.startp[0])
6440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006441 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006442 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006443 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6444 (size_t)i);
6445 ga.ga_len += i;
6446 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006447 continue;
6448 }
6449 zero_width = regmatch.startp[0];
6450 }
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 /*
6453 * Get some space for a temporary buffer to do the substitution
6454 * into. It will contain:
6455 * - The text up to where the match is.
6456 * - The substituted text.
6457 * - The text after the match.
6458 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006459 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006460 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6462 {
6463 ga_clear(&ga);
6464 break;
6465 }
6466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006467 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 i = (int)(regmatch.startp[0] - tail);
6469 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006471 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 + ga.ga_len + i, TRUE, TRUE, FALSE);
6473 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006474 tail = regmatch.endp[0];
6475 if (*tail == NUL)
6476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (!do_all)
6478 break;
6479 }
6480
6481 if (ga.ga_data != NULL)
6482 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6483
Bram Moolenaar473de612013-06-08 18:19:48 +02006484 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486
6487 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6488 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006489 if (p_cpo == empty_option)
6490 p_cpo = save_cpo;
6491 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006493 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 // If it's still empty it was changed and restored, need to restore in
6495 // the complicated way.
6496 if (*p_cpo == NUL)
6497 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006498 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 return ret;
6502}