blob: f57001647c5271dbc29bc378e37cbceb569b6ede [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020054static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020055static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020056static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020060
Bram Moolenaare21c1582019-03-02 11:57:09 +010061/*
62 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010064 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020065 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010066num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010067{
68 varnumber_T result;
69
Bram Moolenaar99880f92021-01-20 21:23:14 +010070 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010071 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010072 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010073 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010074 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010075 if (failed != NULL)
76 *failed = TRUE;
77 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 if (n1 == 0)
79 result = VARNUM_MIN; // similar to NaN
80 else if (n1 < 0)
81 result = -VARNUM_MAX;
82 else
83 result = VARNUM_MAX;
84 }
85 else
86 result = n1 / n2;
87
88 return result;
89}
90
91/*
92 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020095 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010097{
Bram Moolenaar99880f92021-01-20 21:23:14 +010098 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010099 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100100 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100101 if (failed != NULL)
102 *failed = TRUE;
103 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100104 return (n2 == 0) ? 0 : (n1 % n2);
105}
106
Bram Moolenaar33570922005-01-25 22:26:29 +0000107/*
108 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000109 */
110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100111eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000112{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200113 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200114 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000115
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200116#ifdef EBCDIC
117 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100118 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200119 */
120 sortFunctions();
121#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000122}
123
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000124#if defined(EXITFREE) || defined(PROTO)
125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100126eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000127{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100129 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200130 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000131
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200132 // autoloaded script names
133 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000134
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200135 // unreferenced lists and dicts
136 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200137
138 // functions not garbage collected
139 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140}
141#endif
142
Bram Moolenaare6b53242020-07-01 17:28:33 +0200143 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200144fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
145{
146 CLEAR_FIELD(*evalarg);
147 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
148 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
149 {
150 evalarg->eval_getline = eap->getline;
151 evalarg->eval_cookie = eap->cookie;
152 }
153}
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200164 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200169 evalarg_T evalarg;
170
Bram Moolenaar37c83712020-06-30 21:18:36 +0200171 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173 if (skip)
174 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200175 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 else
178 {
179 *error = FALSE;
180 if (!skip)
181 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200182 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200183 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200184 else
185 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000186 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188 }
189 if (skip)
190 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200191 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200193 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194}
195
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196/*
197 * Call eval1() and give an error message if not done at a lower level.
198 */
199 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200200eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100202 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 int ret;
204 int did_emsg_before = did_emsg;
205 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200206 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200208 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
209
210 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211 if (ret == FAIL)
212 {
213 // Report the invalid expression unless the expression evaluation has
214 // been cancelled due to an aborting error, an interrupt, or an
215 // exception, or we already gave a more specific error.
216 // Also check called_emsg for when using assert_fails().
217 if (!aborting() && did_emsg == did_emsg_before
218 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200219 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200221 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 return ret;
223}
224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200226 * Return whether a typval is a valid expression to pass to eval_expr_typval()
227 * or eval_expr_to_bool(). An empty string returns FALSE;
228 */
229 int
230eval_expr_valid_arg(typval_T *tv)
231{
232 return tv->v_type != VAR_UNKNOWN
233 && (tv->v_type != VAR_STRING
234 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
235}
236
237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Evaluate an expression, which can be a function, partial or string.
239 * Pass arguments "argv[argc]".
240 * Return the result in "rettv" and OK or FAIL.
241 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200242 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100243eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
244{
245 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200247 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100248
249 if (expr->v_type == VAR_FUNC)
250 {
251 s = expr->vval.v_string;
252 if (s == NULL || *s == NUL)
253 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200254 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200255 funcexe.evaluate = TRUE;
256 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 return FAIL;
258 }
259 else if (expr->v_type == VAR_PARTIAL)
260 {
261 partial_T *partial = expr->vval.v_partial;
262
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200263 if (partial == NULL)
264 return FAIL;
265
Bram Moolenaar822ba242020-05-24 23:00:18 +0200266 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200267 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200269 if (call_def_function(partial->pt_func, argc, argv,
270 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 return FAIL;
272 }
273 else
274 {
275 s = partial_name(partial);
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 funcexe.evaluate = TRUE;
280 funcexe.partial = partial;
281 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
282 return FAIL;
283 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100284 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200285 else if (expr->v_type == VAR_INSTR)
286 {
287 return exe_typval_instr(expr, rettv);
288 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 else
290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100291 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 if (s == NULL)
293 return FAIL;
294 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200295 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200297 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200299 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200300 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 return FAIL;
302 }
303 }
304 return OK;
305}
306
307/*
308 * Like eval_to_bool() but using a typval_T instead of a string.
309 * Works for string, funcref and partial.
310 */
311 int
312eval_expr_to_bool(typval_T *expr, int *error)
313{
314 typval_T rettv;
315 int res;
316
317 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
318 {
319 *error = TRUE;
320 return FALSE;
321 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200322 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 clear_tv(&rettv);
324 return res;
325}
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327/*
328 * Top level evaluation function, returning a string. If "skip" is TRUE,
329 * only parsing to "nextcmd" is done, without reporting errors. Return
330 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
331 */
332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100333eval_to_string_skip(
334 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200335 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100336 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
Bram Moolenaar33570922005-01-25 22:26:29 +0000338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200340 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341
Bram Moolenaar37c83712020-06-30 21:18:36 +0200342 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (skip)
344 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200345 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 retval = NULL;
347 else
348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100349 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000350 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352 if (skip)
353 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200354 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 return retval;
357}
358
359/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000360 * Skip over an expression at "*pp".
361 * Return FAIL for an error, OK otherwise.
362 */
363 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200364skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000365{
Bram Moolenaar33570922005-01-25 22:26:29 +0000366 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367
368 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200369 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370}
371
372/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200373 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200374 * If in Vim9 script and line breaks are encountered, the lines are
375 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200376 * "arg" is advanced to just after the expression.
377 * "start" is set to the start of the expression, "end" to just after the end.
378 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Return FAIL for an error, OK otherwise.
380 */
381 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200382skip_expr_concatenate(
383 char_u **arg,
384 char_u **start,
385 char_u **end,
386 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200387{
388 typval_T rettv;
389 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200390 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200391 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200392 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200394 int evaluate = evalarg == NULL
395 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200398 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 {
400 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200404 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200406 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407
408 // Don't evaluate the expression.
409 if (evalarg != NULL)
410 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *arg = skipwhite(*arg);
412 res = eval1(arg, &rettv, evalarg);
413 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414 if (evalarg != NULL)
415 evalarg->eval_flags = save_flags;
416
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200417 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200418 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 if (evalarg->eval_ga.ga_len == 1)
421 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200422 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 ga_clear(gap);
424 gap->ga_itemsize = 0;
425 }
426 else
427 {
428 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200430
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200431 // Line breaks encountered, concatenate all the lines.
432 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200433 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434
435 // free the lines only when using getsourceline()
436 if (evalarg->eval_cookie != NULL)
437 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200438 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200439 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200440 // Do not free the last line, "arg" points into it, free it
441 // later.
442 vim_free(evalarg->eval_tofree);
443 evalarg->eval_tofree =
444 ((char_u **)gap->ga_data)[gap->ga_len - 1];
445 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200446 ga_clear_strings(gap);
447 }
448 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200449 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200450 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200451
452 // free lines that were explicitly marked for freeing
453 ga_clear_strings(freegap);
454 }
455
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200456 gap->ga_itemsize = 0;
457 if (p == NULL)
458 return FAIL;
459 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 vim_free(evalarg->eval_tofree_lambda);
461 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200462 // Compute "end" relative to the end.
463 *end = *start + STRLEN(*start) - endoff;
464 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200465 }
466
467 return res;
468}
469
470/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100471 * Convert "tv" to a string.
472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
474 * Returns an allocated string (NULL when out of memory).
475 */
476 char_u *
477typval2string(typval_T *tv, int convert)
478{
479 garray_T ga;
480 char_u *retval;
481#ifdef FEAT_FLOAT
482 char_u numbuf[NUMBUFLEN];
483#endif
484
485 if (convert && tv->v_type == VAR_LIST)
486 {
487 ga_init2(&ga, (int)sizeof(char), 80);
488 if (tv->vval.v_list != NULL)
489 {
490 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
491 if (tv->vval.v_list->lv_len > 0)
492 ga_append(&ga, NL);
493 }
494 ga_append(&ga, NUL);
495 retval = (char_u *)ga.ga_data;
496 }
497#ifdef FEAT_FLOAT
498 else if (convert && tv->v_type == VAR_FLOAT)
499 {
500 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
501 retval = vim_strsave(numbuf);
502 }
503#endif
504 else
505 retval = vim_strsave(tv_get_string(tv));
506 return retval;
507}
508
509/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200510 * Top level evaluation function, returning a string. Does not handle line
511 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000512 * When "convert" is TRUE convert a List into a sequence of lines and convert
513 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 * Return pointer to allocated memory, or NULL for failure.
515 */
516 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100517eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100518 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100519 int convert,
520 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
Bram Moolenaar33570922005-01-25 22:26:29 +0000522 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100524 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100526 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
527 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 retval = NULL;
529 else
530 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100531 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000532 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100534 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 return retval;
537}
538
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100539 char_u *
540eval_to_string(
541 char_u *arg,
542 int convert)
543{
544 return eval_to_string_eap(arg, convert, NULL);
545}
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000548 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200550 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 */
552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100553eval_to_string_safe(
554 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100555 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200558 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200559 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200561 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200562 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000563 if (use_sandbox)
564 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200565 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200566 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 return retval;
573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575/*
576 * Top level evaluation function, returning a number.
577 * Evaluates "expr" silently.
578 * Returns -1 for an error.
579 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200580 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100581eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
Bram Moolenaar33570922005-01-25 22:26:29 +0000583 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000585 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 ++emsg_off;
588
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200589 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 retval = -1;
591 else
592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596 --emsg_off;
597
598 return retval;
599}
600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 * Top level evaluation function.
603 * Returns an allocated typval_T with the result.
604 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000605 */
606 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200607eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000608{
609 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200610 evalarg_T evalarg;
611
612 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200614 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200615 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100616 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617
Bram Moolenaar37c83712020-06-30 21:18:36 +0200618 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619 return tv;
620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100623 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200624 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
625 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000626 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629call_vim_function(
630 char_u *func,
631 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200632 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000635 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200636 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100638 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200639 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200640 funcexe.firstline = curwin->w_cursor.lnum;
641 funcexe.lastline = curwin->w_cursor.lnum;
642 funcexe.evaluate = TRUE;
643 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644 if (ret == FAIL)
645 clear_tv(rettv);
646
647 return ret;
648}
649
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100651 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100652 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200653 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
654 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100655 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200656 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657call_func_retnr(
658 char_u *func,
659 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200660 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100661{
662 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200663 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664
Bram Moolenaarded27a12018-08-01 19:06:03 +0200665 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666 return -1;
667
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100668 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669 clear_tv(&rettv);
670 return retval;
671}
672
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100674 * Call Vim script function like call_func_retnr() and drop the result.
675 * Returns FAIL when calling the function fails.
676 */
677 int
678call_func_noret(
679 char_u *func,
680 int argc,
681 typval_T *argv)
682{
683 typval_T rettv;
684
685 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
686 return FAIL;
687 clear_tv(&rettv);
688 return OK;
689}
690
691/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100692 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100693 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000694 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000695 */
696 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100697call_func_retstr(
698 char_u *func,
699 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200700 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701{
702 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000703 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000704
Bram Moolenaarded27a12018-08-01 19:06:03 +0200705 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000706 return NULL;
707
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100708 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 return retval;
711}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000712
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000713/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100714 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100715 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000716 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000717 */
718 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719call_func_retlist(
720 char_u *func,
721 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200722 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723{
724 typval_T rettv;
725
Bram Moolenaarded27a12018-08-01 19:06:03 +0200726 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000727 return NULL;
728
729 if (rettv.v_type != VAR_LIST)
730 {
731 clear_tv(&rettv);
732 return NULL;
733 }
734
735 return rettv.vval.v_list;
736}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#ifdef FEAT_FOLDING
739/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100740 * Evaluate "arg", which is 'foldexpr'.
741 * Note: caller must set "curwin" to match "arg".
742 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
743 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100746eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200749 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000751 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
752 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000755 if (use_sandbox)
756 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200757 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200759 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 retval = 0;
761 else
762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000764 if (tv.v_type == VAR_NUMBER)
765 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000766 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 retval = 0;
768 else
769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 // If the result is a string, check if there is a non-digit before
771 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (!VIM_ISDIGIT(*s) && *s != '-')
774 *cp = *s++;
775 retval = atol((char *)s);
776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000777 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000780 if (use_sandbox)
781 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200782 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200783 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200785 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787#endif
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 * Get an lval: variable, Dict item or List item that can be assigned a value
791 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
792 * "name.key", "name.key[expr]" etc.
793 * Indexing only works if "name" is an existing List or Dictionary.
794 * "name" points to the start of the name.
795 * If "rettv" is not NULL it points to the value to be assigned.
796 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
797 * wrong; must end in space or cmd separator.
798 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100799 * flags:
800 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100801 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100802 * GLV_NO_AUTOLOAD: do not use script autoloading
803 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000805 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200808 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809get_lval(
810 char_u *name,
811 typval_T *rettv,
812 lval_T *lp,
813 int unlet,
814 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100815 int flags, // GLV_ values
816 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 char_u *expr_start, *expr_end;
820 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 dictitem_T *v;
822 typval_T var1;
823 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000825 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100828 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100829 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100830 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200833 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100835 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100837 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200839 lp->ll_name_end = find_name_end(name, NULL, NULL,
840 FNE_INCL_BR | fne_flags);
841 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 }
843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000845 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (expr_start != NULL)
848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100850 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 && *p != '[' && *p != '.')
852 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200853 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 return NULL;
855 }
856
857 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
858 if (lp->ll_exp_name == NULL)
859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100860 // Report an invalid expression in braces, unless the
861 // expression evaluation has been cancelled due to an
862 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 if (!aborting() && !quiet)
864 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000865 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 return NULL;
868 }
869 }
870 lp->ll_name = lp->ll_exp_name;
871 }
872 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 lp->ll_name = name;
875
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200876 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200878 // "a: type" is declaring variable "a" with a type, not "a:".
879 if (p == name + 2 && p[-1] == ':')
880 {
881 --p;
882 lp->ll_name_end = p;
883 }
884 if (*p == ':')
885 {
886 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
887 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200889 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100890 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
891 if (lp->ll_type == NULL && !quiet)
892 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200893 lp->ll_name_end = tp;
894 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 }
896 }
897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100898 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
900 return p;
901
902 cc = *p;
903 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100904 // When we would write to the variable pass &ht and prevent autoload.
905 writing = !(flags & GLV_READ_ONLY);
906 v = find_var(lp->ll_name, writing ? &ht : NULL,
907 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200909 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000911 if (v == NULL)
912 return NULL;
913
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100914 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
915 {
916 if (!quiet)
917 semsg(_(e_variable_already_declared), lp->ll_name);
918 return NULL;
919 }
920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 /*
922 * Loop until no more [idx] or .key is following.
923 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000924 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100925 var1.v_type = VAR_UNKNOWN;
926 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200927 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200929 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
930 {
931 if (!quiet)
932 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
933 return NULL;
934 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200935 if (lp->ll_tv->v_type != VAR_LIST
936 && lp->ll_tv->v_type != VAR_DICT
937 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200943
944 // a NULL list/blob works like an empty list/blob, allocate one now.
945 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
946 rettv_list_alloc(lp->ll_tv);
947 else if (lp->ll_tv->v_type == VAR_BLOB
948 && lp->ll_tv->vval.v_blob == NULL)
949 rettv_blob_alloc(lp->ll_tv);
950
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100954 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000956 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000957
Bram Moolenaar10c65862020-10-08 21:16:42 +0200958 if (in_vim9script() && lp->ll_valtype == NULL
959 && lp->ll_tv == &v->di_tv
960 && ht != NULL && ht == get_script_local_ht())
961 {
962 svar_T *sv = find_typval_in_script(lp->ll_tv);
963
964 // Vim9 script local variable: get the type
965 if (sv != NULL)
966 lp->ll_valtype = sv->sv_type;
967 }
968
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 len = -1;
970 if (*p == '.')
971 {
972 key = p + 1;
973 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
974 ;
975 if (len == 0)
976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100978 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 }
981 p = key + len;
982 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000983 else
984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100985 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000986 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 if (*p == ':')
988 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000989 else
990 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200992 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100994 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000997 clear_tv(&var1);
998 return NULL;
999 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001000 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 }
1002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001003 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 if (*p == ':')
1005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001009 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001010 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001012 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001013 if (rettv != NULL
1014 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001015 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001017 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001021 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 }
1024 p = skipwhite(p + 1);
1025 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 else
1028 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001030 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001031 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001033 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001036 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001038 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001040 clear_tv(&var2);
1041 return NULL;
1042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001048
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001052 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001053 clear_tv(&var1);
1054 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 }
1057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001058 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 ++p;
1060 }
1061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 {
1064 if (len == -1)
1065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001066 // "[key]": get key from "var1"
1067 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001068 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 }
1073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001074 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001075
1076 // a NULL dict is equivalent with an empty dict
1077 if (lp->ll_tv->vval.v_dict == NULL)
1078 {
1079 lp->ll_tv->vval.v_dict = dict_alloc();
1080 if (lp->ll_tv->vval.v_dict == NULL)
1081 {
1082 clear_tv(&var1);
1083 return NULL;
1084 }
1085 ++lp->ll_tv->vval.v_dict->dv_refcount;
1086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001087 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001088
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001089 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // When assigning to a scope dictionary check that a function and
1092 // variable name is valid (only variable name unless it is l: or
1093 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001094 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001095 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001096 int prevval;
1097 int wrong;
1098
1099 if (len != -1)
1100 {
1101 prevval = key[len];
1102 key[len] = NUL;
1103 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001104 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001105 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001106 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1107 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001108 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001109 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001110 if (len != -1)
1111 key[len] = prevval;
1112 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001113 {
1114 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001115 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001116 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117 }
1118
Bram Moolenaar10c65862020-10-08 21:16:42 +02001119 if (lp->ll_valtype != NULL)
1120 // use the type of the member
1121 lp->ll_valtype = lp->ll_valtype->tt_member;
1122
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001125 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001126 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001127 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001130 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001131 return NULL;
1132 }
1133
Bram Moolenaar31b81602019-02-10 22:14:27 +01001134 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
1142 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001146 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 p = NULL;
1149 break;
1150 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001151 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001152 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001153 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1154 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001155 {
1156 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001157 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001158 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001159
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 else if (lp->ll_tv->v_type == VAR_BLOB)
1164 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001165 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1166
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 /*
1168 * Get the number and item for the only or first index of the List.
1169 */
1170 if (empty1)
1171 lp->ll_n1 = 0;
1172 else
1173 // is number or string
1174 lp->ll_n1 = (long)tv_get_number(&var1);
1175 clear_tv(&var1);
1176
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001177 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001178 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180 return NULL;
1181 }
1182 if (lp->ll_range && !lp->ll_empty2)
1183 {
1184 lp->ll_n2 = (long)tv_get_number(&var2);
1185 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001186 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1187 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001189 }
1190 lp->ll_blob = lp->ll_tv->vval.v_blob;
1191 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001192 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 else
1195 {
1196 /*
1197 * Get the number and item for the only or first index of the List.
1198 */
1199 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001201 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001203 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001204 clear_tv(&var1);
1205
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001206 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001208 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001209 if (lp->ll_li == NULL)
1210 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001211 // Vim9: Allow for adding an item at the end.
1212 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1213 && lp->ll_list->lv_lock == 0)
1214 {
1215 list_append_number(lp->ll_list, 0);
1216 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1217 }
1218 if (lp->ll_li == NULL)
1219 {
1220 clear_tv(&var2);
1221 if (!quiet)
1222 semsg(_(e_listidx), lp->ll_n1);
1223 return NULL;
1224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar10c65862020-10-08 21:16:42 +02001227 if (lp->ll_valtype != NULL)
1228 // use the type of the member
1229 lp->ll_valtype = lp->ll_valtype->tt_member;
1230
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 /*
1232 * May need to find the item or absolute index for the second
1233 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * When no index given: "lp->ll_empty2" is TRUE.
1235 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001239 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 {
1247 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (lp->ll_n1 < 0)
1256 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1257 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 {
1259 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001262 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001263 }
1264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 }
1268
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return p;
1272}
1273
1274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279{
1280 vim_free(lp->ll_exp_name);
1281 vim_free(lp->ll_newkey);
1282}
1283
1284/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001285 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001287 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1288 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291set_var_lval(
1292 lval_T *lp,
1293 char_u *endp,
1294 typval_T *rettv,
1295 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001296 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1297 char_u *op,
1298 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299{
1300 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 listitem_T *ri;
1302 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303
1304 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001306 cc = *endp;
1307 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001308 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1309 return;
1310
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 if (lp->ll_blob != NULL)
1312 {
1313 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001314
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 if (op != NULL && *op != '=')
1316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001317 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 return;
1319 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001320 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001321 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322
1323 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1324 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001325 if (lp->ll_empty2)
1326 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1327
Bram Moolenaar68452172021-04-12 21:21:02 +02001328 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1329 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 }
1332 else
1333 {
1334 val = (int)tv_get_number_chk(rettv, &error);
1335 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001336 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 }
1339 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001342
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001343 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1344 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001345 {
1346 emsg(_(e_cannot_mod));
1347 *endp = cc;
1348 return;
1349 }
1350
Bram Moolenaarff697e62019-02-12 22:28:33 +01001351 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001353 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001354 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001355 {
1356 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001357 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1358 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 && tv_op(&tv, rettv, op) == OK)
1360 set_var(lp->ll_name, &tv, FALSE);
1361 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001364 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001365 {
1366 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001367 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001368 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001369 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1370 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001371 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001372 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001374 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001375 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001376 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001377 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378 else if (lp->ll_range)
1379 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001380 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001381 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001383 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1384 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001385 {
1386 emsg(_("E996: Cannot lock a range"));
1387 return;
1388 }
1389
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001390 /*
1391 * Check whether any of the list items is locked
1392 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001393 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001394 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001395 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 return;
1397 ri = ri->li_next;
1398 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1399 break;
1400 ll_li = ll_li->li_next;
1401 ++ll_n1;
1402 }
1403
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 /*
1405 * Assign the List values to the list items.
1406 */
1407 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 if (op != NULL && *op != '=')
1410 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1411 else
1412 {
1413 clear_tv(&lp->ll_li->li_tv);
1414 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1415 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 ri = ri->li_next;
1417 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1418 break;
1419 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001421 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001422 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 ri = NULL;
1425 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 lp->ll_li = lp->ll_li->li_next;
1429 ++lp->ll_n1;
1430 }
1431 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001432 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 else if (lp->ll_empty2
1434 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001436 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 }
1438 else
1439 {
1440 /*
1441 * Assign to a List or Dictionary item.
1442 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001443 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1444 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001445 {
1446 emsg(_("E996: Cannot lock a list or dict"));
1447 return;
1448 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001449
1450 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001451 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001452 return;
1453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (lp->ll_newkey != NULL)
1455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 if (op != NULL && *op != '=')
1457 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001458 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 return;
1460 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001461 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1462 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001463 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001465 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001466 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 if (di == NULL)
1468 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001469 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1470 {
1471 vim_free(di);
1472 return;
1473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 else if (op != NULL && *op != '=')
1477 {
1478 tv_op(lp->ll_tv, rettv, op);
1479 return;
1480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001483
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 /*
1485 * Assign the value to the variable or list item.
1486 */
1487 if (copy)
1488 copy_tv(rettv, lp->ll_tv);
1489 else
1490 {
1491 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001492 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001493 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 }
1495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496}
1497
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001499 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1500 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501 * Returns OK or FAIL.
1502 */
1503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001506 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 char_u numbuf[NUMBUFLEN];
1508 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001509 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001511 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001512 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001513 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 {
1515 switch (tv1->v_type)
1516 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001517 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001518 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 case VAR_DICT:
1521 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001522 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001523 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001524 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001525 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001527 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 break;
1529
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001530 case VAR_BLOB:
1531 if (*op != '+' || tv2->v_type != VAR_BLOB)
1532 break;
1533 // BLOB += BLOB
1534 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1535 {
1536 blob_T *b1 = tv1->vval.v_blob;
1537 blob_T *b2 = tv2->vval.v_blob;
1538 int i, len = blob_len(b2);
1539 for (i = 0; i < len; i++)
1540 ga_append(&b1->bv_ga, blob_get(b2, i));
1541 }
1542 return OK;
1543
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 case VAR_LIST:
1545 if (*op != '+' || tv2->v_type != VAR_LIST)
1546 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001547 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001548 if (tv2->vval.v_list != NULL)
1549 {
1550 if (tv1->vval.v_list == NULL)
1551 {
1552 tv1->vval.v_list = tv2->vval.v_list;
1553 ++tv1->vval.v_list->lv_refcount;
1554 }
1555 else
1556 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 return OK;
1559
1560 case VAR_NUMBER:
1561 case VAR_STRING:
1562 if (tv2->v_type == VAR_LIST)
1563 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001567 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568#ifdef FEAT_FLOAT
1569 if (tv2->v_type == VAR_FLOAT)
1570 {
1571 float_T f = n;
1572
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 if (*op == '%')
1574 break;
1575 switch (*op)
1576 {
1577 case '+': f += tv2->vval.v_float; break;
1578 case '-': f -= tv2->vval.v_float; break;
1579 case '*': f *= tv2->vval.v_float; break;
1580 case '/': f /= tv2->vval.v_float; break;
1581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 clear_tv(tv1);
1583 tv1->v_type = VAR_FLOAT;
1584 tv1->vval.v_float = f;
1585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587#endif
1588 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 switch (*op)
1590 {
1591 case '+': n += tv_get_number(tv2); break;
1592 case '-': n -= tv_get_number(tv2); break;
1593 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001594 case '/': n = num_divide(n, tv_get_number(tv2),
1595 &failed); break;
1596 case '%': n = num_modulus(n, tv_get_number(tv2),
1597 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599 clear_tv(tv1);
1600 tv1->v_type = VAR_NUMBER;
1601 tv1->vval.v_number = n;
1602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 }
1604 else
1605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 if (tv2->v_type == VAR_FLOAT)
1607 break;
1608
Bram Moolenaarff697e62019-02-12 22:28:33 +01001609 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001610 s = tv_get_string(tv1);
1611 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 clear_tv(tv1);
1613 tv1->v_type = VAR_STRING;
1614 tv1->vval.v_string = s;
1615 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001616 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001619#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620 {
1621 float_T f;
1622
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 if (*op == '%' || *op == '.'
1624 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 && tv2->v_type != VAR_NUMBER
1626 && tv2->v_type != VAR_STRING))
1627 break;
1628 if (tv2->v_type == VAR_FLOAT)
1629 f = tv2->vval.v_float;
1630 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001631 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001632 switch (*op)
1633 {
1634 case '+': tv1->vval.v_float += f; break;
1635 case '-': tv1->vval.v_float -= f; break;
1636 case '*': tv1->vval.v_float *= f; break;
1637 case '/': tv1->vval.v_float /= f; break;
1638 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001641 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 }
1643 }
1644
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001645 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 return FAIL;
1647}
1648
1649/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 * Evaluate the expression used in a ":for var in expr" command.
1651 * "arg" points to "var".
1652 * Set "*errp" to TRUE for an error, FALSE otherwise;
1653 * Return a pointer that holds the info. Null when there is an error.
1654 */
1655 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001656eval_for_line(
1657 char_u *arg,
1658 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001659 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001660 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661{
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001664 typval_T tv;
1665 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001666 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001668 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001670 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 if (fi == NULL)
1672 return NULL;
1673
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001674 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 if (expr == NULL)
1676 return fi;
1677
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001678 expr = skipwhite_and_linebreak(expr, evalarg);
1679 if (expr[0] != 'i' || expr[1] != 'n'
1680 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 return fi;
1684 }
1685
1686 if (skip)
1687 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001688 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1689 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 {
1691 *errp = FALSE;
1692 if (!skip)
1693 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001695 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001696 l = tv.vval.v_list;
1697 if (l == NULL)
1698 {
1699 // a null list is like an empty list: do nothing
1700 clear_tv(&tv);
1701 }
1702 else
1703 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001705 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001707 // No need to increment the refcount, it's already set for
1708 // the list being used in "tv".
1709 fi->fi_list = l;
1710 list_add_watch(l, &fi->fi_lw);
1711 fi->fi_lw.lw_item = l->lv_first;
1712 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001713 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001715 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001716 fi->fi_bi = 0;
1717 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001719 typval_T btv;
1720
1721 // Make a copy, so that the iteration still works when the
1722 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001726 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001727 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001728 else if (tv.v_type == VAR_STRING)
1729 {
1730 fi->fi_byte_idx = 0;
1731 fi->fi_string = tv.vval.v_string;
1732 tv.vval.v_string = NULL;
1733 if (fi->fi_string == NULL)
1734 fi->fi_string = vim_strsave((char_u *)"");
1735 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 else
1737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001738 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001739 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 }
1741 }
1742 }
1743 if (skip)
1744 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001745 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746
1747 return fi;
1748}
1749
1750/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001751 * Used when looping over a :for line, skip the "in expr" part.
1752 */
1753 void
1754skip_for_lines(void *fi_void, evalarg_T *evalarg)
1755{
1756 forinfo_T *fi = (forinfo_T *)fi_void;
1757 int i;
1758
1759 for (i = 0; i < fi->fi_break_count; ++i)
1760 eval_next_line(evalarg);
1761}
1762
1763/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 * Use the first item in a ":for" list. Advance to the next.
1765 * Assign the values to the variable (list). "arg" points to the first one.
1766 * Return TRUE when a valid item was found, FALSE when at end of list or
1767 * something wrong.
1768 */
1769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001770next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001772 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001773 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001774 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1775 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1776 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001779 if (fi->fi_blob != NULL)
1780 {
1781 typval_T tv;
1782
1783 if (fi->fi_bi >= blob_len(fi->fi_blob))
1784 return FALSE;
1785 tv.v_type = VAR_NUMBER;
1786 tv.v_lock = VAR_FIXED;
1787 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1788 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001789 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001790 fi->fi_varcount, flag, NULL) == OK;
1791 }
1792
1793 if (fi->fi_string != NULL)
1794 {
1795 typval_T tv;
1796 int len;
1797
1798 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1799 if (len == 0)
1800 return FALSE;
1801 tv.v_type = VAR_STRING;
1802 tv.v_lock = VAR_FIXED;
1803 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1804 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001805 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001806 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001807 vim_free(tv.vval.v_string);
1808 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001809 }
1810
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 item = fi->fi_lw.lw_item;
1812 if (item == NULL)
1813 result = FALSE;
1814 else
1815 {
1816 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001817 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001818 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 }
1820 return result;
1821}
1822
1823/*
1824 * Free the structure used to store info used by ":for".
1825 */
1826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828{
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001831 if (fi == NULL)
1832 return;
1833 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001834 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001836 list_unref(fi->fi_list);
1837 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001838 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001839 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001840 else
1841 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 vim_free(fi);
1843}
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846set_context_for_expression(
1847 expand_T *xp,
1848 char_u *arg,
1849 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001851 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001855 if (cmdidx == CMD_let || cmdidx == CMD_var
1856 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 {
1858 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001859 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001861 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001862 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 {
1864 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001865 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001866 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 break;
1868 }
1869 return;
1870 }
1871 }
1872 else
1873 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1874 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 while ((xp->xp_pattern = vim_strpbrk(arg,
1876 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1877 {
1878 c = *xp->xp_pattern;
1879 if (c == '&')
1880 {
1881 c = xp->xp_pattern[1];
1882 if (c == '&')
1883 {
1884 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001885 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001890 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1891 xp->xp_pattern += 2;
1892
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895 else if (c == '$')
1896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 xp->xp_context = EXPAND_ENV_VARS;
1899 }
1900 else if (c == '=')
1901 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001902 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 xp->xp_context = EXPAND_EXPRESSION;
1904 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001905 else if (c == '#'
1906 && xp->xp_context == EXPAND_EXPRESSION)
1907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001909 break;
1910 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001911 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && xp->xp_context == EXPAND_FUNCTIONS
1913 && vim_strchr(xp->xp_pattern, '(') == NULL)
1914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001915 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 break;
1917 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001918 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001920 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
1922 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1923 if (c == '\\' && xp->xp_pattern[1] != NUL)
1924 ++xp->xp_pattern;
1925 xp->xp_context = EXPAND_NOTHING;
1926 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1931 /* skip */ ;
1932 xp->xp_context = EXPAND_NOTHING;
1933 }
1934 else if (c == '|')
1935 {
1936 if (xp->xp_pattern[1] == '|')
1937 {
1938 ++xp->xp_pattern;
1939 xp->xp_context = EXPAND_EXPRESSION;
1940 }
1941 else
1942 xp->xp_context = EXPAND_COMMANDS;
1943 }
1944 else
1945 xp->xp_context = EXPAND_EXPRESSION;
1946 }
1947 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001948 // Doesn't look like something valid, expand as an expression
1949 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 arg = xp->xp_pattern;
1952 if (*arg != NUL)
1953 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1954 /* skip */ ;
1955 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001956
1957 // ":exe one two" completes "two"
1958 if ((cmdidx == CMD_execute
1959 || cmdidx == CMD_echo
1960 || cmdidx == CMD_echon
1961 || cmdidx == CMD_echomsg)
1962 && xp->xp_context == EXPAND_EXPRESSION)
1963 {
1964 for (;;)
1965 {
1966 char_u *n = skiptowhite(arg);
1967
1968 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1969 break;
1970 arg = skipwhite(n);
1971 }
1972 }
1973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 xp->xp_pattern = arg;
1975}
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001978 * Return TRUE if "pat" matches "text".
1979 * Does not use 'cpo' and always uses 'magic'.
1980 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001981 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001982pattern_match(char_u *pat, char_u *text, int ic)
1983{
1984 int matches = FALSE;
1985 char_u *save_cpo;
1986 regmatch_T regmatch;
1987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001990 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001991 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1992 if (regmatch.regprog != NULL)
1993 {
1994 regmatch.rm_ic = ic;
1995 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1996 vim_regfree(regmatch.regprog);
1997 }
1998 p_cpo = save_cpo;
1999 return matches;
2000}
2001
2002/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002003 * Handle a name followed by "(". Both for just "name(arg)" and for
2004 * "expr->name(arg)".
2005 * Returns OK or FAIL.
2006 */
2007 static int
2008eval_func(
2009 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002010 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002011 char_u *name,
2012 int name_len,
2013 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002014 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 typval_T *basetv) // "expr" for "expr->name(arg)"
2016{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002017 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002018 char_u *s = name;
2019 int len = name_len;
2020 partial_T *partial;
2021 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002022 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023
2024 if (!evaluate)
2025 check_vars(s, len);
2026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002027 // If "s" is the name of a variable of type VAR_FUNC
2028 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 s = deref_func_name(s, &len, &partial,
2030 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // Need to make a copy, in case evaluating the arguments makes
2033 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002035 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036 ret = FAIL;
2037 else
2038 {
2039 funcexe_T funcexe;
2040
2041 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002042 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 funcexe.firstline = curwin->w_cursor.lnum;
2044 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 funcexe.evaluate = evaluate;
2046 funcexe.partial = partial;
2047 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002048 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002049 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002050 }
2051 vim_free(s);
2052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002053 // If evaluate is FALSE rettv->v_type was not set in
2054 // get_func_tv, but it's needed in handle_subscript() to parse
2055 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2057 {
2058 rettv->vval.v_string = NULL;
2059 rettv->v_type = VAR_FUNC;
2060 }
2061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002062 // Stop the expression evaluation when immediately
2063 // aborting on error, or when an interrupt occurred or
2064 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 if (evaluate && aborting())
2066 {
2067 if (ret == OK)
2068 clear_tv(rettv);
2069 ret = FAIL;
2070 }
2071 return ret;
2072}
2073
2074/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002075 * Get the next line source line without advancing. But do skip over comment
2076 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002077 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002078 */
2079 static char_u *
2080getline_peek_skip_comments(evalarg_T *evalarg)
2081{
2082 for (;;)
2083 {
2084 char_u *next = getline_peek(evalarg->eval_getline,
2085 evalarg->eval_cookie);
2086 char_u *p;
2087
2088 if (next == NULL)
2089 break;
2090 p = skipwhite(next);
2091 if (*p != NUL && !vim9_comment_start(p))
2092 return next;
2093 (void)eval_next_line(evalarg);
2094 }
2095 return NULL;
2096}
2097
2098/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002099 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2100 * comment) and there is a next line, return the next line (skipping blanks)
2101 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002102 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2103 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 * "arg" must point somewhere inside a line, not at the start.
2105 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002106 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2108{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 char_u *p = skipwhite(arg);
2110
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002112 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002114 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002115 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002118
2119 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002120 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002121 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123
Bram Moolenaar9d489562020-07-30 20:08:50 +02002124 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002125 {
2126 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128 }
2129 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002131}
2132
2133/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002135 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002136 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002137 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002138eval_next_line(evalarg_T *evalarg)
2139{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002140 garray_T *gap = &evalarg->eval_ga;
2141 char_u *line;
2142
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002143 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002144 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2145 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002146 else
2147 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002148 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002149 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2150 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002151 char_u *p = skipwhite(line);
2152
2153 // Going to concatenate the lines after parsing. For an empty or
2154 // comment line use an empty string.
2155 if (*p == NUL || vim9_comment_start(p))
2156 {
2157 vim_free(line);
2158 line = vim_strsave((char_u *)"");
2159 }
2160
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002161 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2162 ++gap->ga_len;
2163 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002164 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002165 {
2166 vim_free(evalarg->eval_tofree);
2167 evalarg->eval_tofree = line;
2168 }
2169 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002170}
2171
Bram Moolenaare6b53242020-07-01 17:28:33 +02002172/*
2173 * Call eval_next_non_blank() and get the next line if needed.
2174 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002175 char_u *
2176skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2177{
2178 int getnext;
2179 char_u *p = skipwhite(arg);
2180
Bram Moolenaar962d7212020-07-04 14:15:00 +02002181 if (evalarg == NULL)
2182 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002183 eval_next_non_blank(p, evalarg, &getnext);
2184 if (getnext)
2185 return eval_next_line(evalarg);
2186 return p;
2187}
2188
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002189/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002190 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002191 */
2192 void
2193clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2194{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002195 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002196 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002197 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002198 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002199 if (eap != NULL)
2200 {
2201 // We may need to keep the original command line, e.g. for
2202 // ":let" it has the variable names. But we may also need the
2203 // new one, "nextcmd" points into it. Keep both.
2204 vim_free(eap->cmdline_tofree);
2205 eap->cmdline_tofree = *eap->cmdlinep;
2206 *eap->cmdlinep = evalarg->eval_tofree;
2207 }
2208 else
2209 vim_free(evalarg->eval_tofree);
2210 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002211 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002212
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002213 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2214 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002215 }
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2222 */
2223
2224/*
2225 * Handle zero level expression.
2226 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002228 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002229 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * Return OK or FAIL.
2231 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233eval0(
2234 char_u *arg,
2235 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002236 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002237 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238{
2239 int ret;
2240 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002241 int did_emsg_before = did_emsg;
2242 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002243 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002244 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002248 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002250 if (ret != FAIL)
2251 end_error = !ends_excmd2(arg, p);
2252 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 /*
2257 * Report the invalid expression unless the expression evaluation has
2258 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002259 * exception, or we already gave a more specific error.
2260 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002262 if (!aborting()
2263 && did_emsg == did_emsg_before
2264 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002265 && (flags & EVAL_CONSTANT) == 0
2266 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002267 {
2268 if (end_error)
2269 semsg(_(e_trailing_arg), p);
2270 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002271 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002272 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002273
2274 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002275 // a next command to avoid more errors, unless "|" is following, which
2276 // could only be a command separator.
2277 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2278 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002281
2282 if (eap != NULL)
2283 eap->nextcmd = check_nextcmd(p);
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 return ret;
2286}
2287
2288/*
2289 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002290 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002291 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 *
2293 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002294 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002296 * Note: "rettv.v_lock" is not set.
2297 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Return OK or FAIL.
2299 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002300 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002303 char_u *p;
2304 int getnext;
2305
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002306 CLEAR_POINTER(rettv);
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /*
2309 * Get the first variable.
2310 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313
Bram Moolenaar793648f2020-06-26 21:28:25 +02002314 p = eval_next_non_blank(*arg, evalarg, &getnext);
2315 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 int result;
2319 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002320 evalarg_T *evalarg_used = evalarg;
2321 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002323 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002324 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325
2326 if (evalarg == NULL)
2327 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328 CLEAR_FIELD(local_evalarg);
2329 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002331 orig_flags = evalarg_used->eval_flags;
2332 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002334 if (getnext)
2335 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002336 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002337 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002338 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002339 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002340 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002341 clear_tv(rettv);
2342 return FAIL;
2343 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002344 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002345 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002348 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 int error = FALSE;
2351
Bram Moolenaar13106602020-10-04 16:06:05 +02002352 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002353 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002354 else if (vim9script)
2355 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002356 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 if (error || !op_falsy || !result)
2359 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (error)
2361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363
2364 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002365 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002367 if (op_falsy)
2368 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002369 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002370 {
mityu4ac198c2021-05-28 17:52:40 +02002371 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002372 clear_tv(rettv);
2373 return FAIL;
2374 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002375 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002376 evalarg_used->eval_flags = (op_falsy ? !result : result)
2377 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2378 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002379 {
2380 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002382 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002383 if (!op_falsy || !result)
2384 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 /*
2389 * Check for the ":".
2390 */
2391 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2392 if (*p != ':')
2393 {
2394 emsg(_(e_missing_colon));
2395 if (evaluate && result)
2396 clear_tv(rettv);
2397 evalarg_used->eval_flags = orig_flags;
2398 return FAIL;
2399 }
2400 if (getnext)
2401 *arg = eval_next_line(evalarg_used);
2402 else
2403 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002404 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002405 {
2406 error_white_both(p, 1);
2407 clear_tv(rettv);
2408 evalarg_used->eval_flags = orig_flags;
2409 return FAIL;
2410 }
2411 *arg = p;
2412 }
2413
2414 /*
2415 * Get the third variable. Recursive!
2416 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002417 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 {
mityu4ac198c2021-05-28 17:52:40 +02002419 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002420 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002421 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002422 return FAIL;
2423 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002424 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2425 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002427 if (eval1(arg, &var2, evalarg_used) == FAIL)
2428 {
2429 if (evaluate && result)
2430 clear_tv(rettv);
2431 evalarg_used->eval_flags = orig_flags;
2432 return FAIL;
2433 }
2434 if (evaluate && !result)
2435 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437
2438 if (evalarg == NULL)
2439 clear_evalarg(&local_evalarg, NULL);
2440 else
2441 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443
2444 return OK;
2445}
2446
2447/*
2448 * Handle first level expression:
2449 * expr2 || expr2 || expr2 logical OR
2450 *
2451 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002452 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 *
2454 * Return OK or FAIL.
2455 */
2456 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002459 char_u *p;
2460 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /*
2463 * Get the first variable.
2464 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 return FAIL;
2467
2468 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002471 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 evalarg_T *evalarg_used = evalarg;
2475 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 int evaluate;
2477 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478 long result = FALSE;
2479 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002480 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002481 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002482
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483 if (evalarg == NULL)
2484 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 CLEAR_FIELD(local_evalarg);
2486 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 orig_flags = evalarg_used->eval_flags;
2489 evaluate = orig_flags & EVAL_EVALUATE;
2490 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002492 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002493 result = tv_get_bool_chk(rettv, &error);
2494 else if (tv_get_number_chk(rettv, &error) != 0)
2495 result = TRUE;
2496 clear_tv(rettv);
2497 if (error)
2498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500
2501 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002502 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 while (p[0] == '|' && p[1] == '|')
2505 {
2506 if (getnext)
2507 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002508 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002509 {
2510 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2511 {
2512 error_white_both(p, 2);
2513 clear_tv(rettv);
2514 return FAIL;
2515 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002516 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002517 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518
2519 /*
2520 * Get the second variable.
2521 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002522 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2523 {
mityu4ac198c2021-05-28 17:52:40 +02002524 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002525 clear_tv(rettv);
2526 return FAIL;
2527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2529 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002530 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
2534 /*
2535 * Compute the result.
2536 */
2537 if (evaluate && !result)
2538 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002539 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002540 result = tv_get_bool_chk(&var2, &error);
2541 else if (tv_get_number_chk(&var2, &error) != 0)
2542 result = TRUE;
2543 clear_tv(&var2);
2544 if (error)
2545 return FAIL;
2546 }
2547 if (evaluate)
2548 {
2549 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002550 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002551 rettv->v_type = VAR_BOOL;
2552 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002553 }
2554 else
2555 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 rettv->v_type = VAR_NUMBER;
2557 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002558 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560
2561 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 if (evalarg == NULL)
2565 clear_evalarg(&local_evalarg, NULL);
2566 else
2567 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
2570 return OK;
2571}
2572
2573/*
2574 * Handle second level expression:
2575 * expr3 && expr3 && expr3 logical AND
2576 *
2577 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002578 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 *
2580 * Return OK or FAIL.
2581 */
2582 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002585 char_u *p;
2586 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588 /*
2589 * Get the first variable.
2590 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 return FAIL;
2593
2594 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002597 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 evalarg_T *evalarg_used = evalarg;
2601 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 int orig_flags;
2603 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604 long result = TRUE;
2605 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002606 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002607 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002608
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609 if (evalarg == NULL)
2610 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 CLEAR_FIELD(local_evalarg);
2612 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 orig_flags = evalarg_used->eval_flags;
2615 evaluate = orig_flags & EVAL_EVALUATE;
2616 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002618 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 result = tv_get_bool_chk(rettv, &error);
2620 else if (tv_get_number_chk(rettv, &error) == 0)
2621 result = FALSE;
2622 clear_tv(rettv);
2623 if (error)
2624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
2626
2627 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 while (p[0] == '&' && p[1] == '&')
2631 {
2632 if (getnext)
2633 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002634 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002635 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002637 {
2638 error_white_both(p, 2);
2639 clear_tv(rettv);
2640 return FAIL;
2641 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002642 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002643 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002644
2645 /*
2646 * Get the second variable.
2647 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002648 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2649 {
mityu4ac198c2021-05-28 17:52:40 +02002650 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002651 clear_tv(rettv);
2652 return FAIL;
2653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2655 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002657 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660
2661 /*
2662 * Compute the result.
2663 */
2664 if (evaluate && result)
2665 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002666 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002667 result = tv_get_bool_chk(&var2, &error);
2668 else if (tv_get_number_chk(&var2, &error) == 0)
2669 result = FALSE;
2670 clear_tv(&var2);
2671 if (error)
2672 return FAIL;
2673 }
2674 if (evaluate)
2675 {
2676 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002677 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002678 rettv->v_type = VAR_BOOL;
2679 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002680 }
2681 else
2682 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002683 rettv->v_type = VAR_NUMBER;
2684 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002685 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687
2688 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002690
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 if (evalarg == NULL)
2692 clear_evalarg(&local_evalarg, NULL);
2693 else
2694 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696
2697 return OK;
2698}
2699
2700/*
2701 * Handle third level expression:
2702 * var1 == var2
2703 * var1 =~ var2
2704 * var1 != var2
2705 * var1 !~ var2
2706 * var1 > var2
2707 * var1 >= var2
2708 * var1 < var2
2709 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002710 * var1 is var2
2711 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 *
2713 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002714 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 *
2716 * Return OK or FAIL.
2717 */
2718 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002722 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002723 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002725 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
2727 /*
2728 * Get the first variable.
2729 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return FAIL;
2732
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002733 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002734 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002737 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002739 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002741 typval_T var2;
2742 int ic;
2743 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002744 int evaluate = evalarg == NULL
2745 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002746
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002747 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002748 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002749 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002750 p = *arg;
2751 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002752 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2753 {
mityu4ac198c2021-05-28 17:52:40 +02002754 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002755 clear_tv(rettv);
2756 return FAIL;
2757 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002758
Bram Moolenaar696ba232020-07-29 21:20:41 +02002759 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2760 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002761 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002762 clear_tv(rettv);
2763 return FAIL;
2764 }
2765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002766 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (p[len] == '?')
2768 {
2769 ic = TRUE;
2770 ++len;
2771 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002772 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else if (p[len] == '#')
2774 {
2775 ic = FALSE;
2776 ++len;
2777 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002778 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002780 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 /*
2783 * Get the second variable.
2784 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002785 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2786 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002787 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002788 clear_tv(rettv);
2789 return FAIL;
2790 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002791 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 return FAIL;
2796 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002797 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002798 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002799 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002800
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002801 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002802 {
2803 ret = FAIL;
2804 clear_tv(rettv);
2805 }
2806 else
2807 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002808 clear_tv(&var2);
2809 return ret;
2810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
2813 return OK;
2814}
2815
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002816/*
2817 * Make a copy of blob "tv1" and append blob "tv2".
2818 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 void
2820eval_addblob(typval_T *tv1, typval_T *tv2)
2821{
2822 blob_T *b1 = tv1->vval.v_blob;
2823 blob_T *b2 = tv2->vval.v_blob;
2824 blob_T *b = blob_alloc();
2825 int i;
2826
2827 if (b != NULL)
2828 {
2829 for (i = 0; i < blob_len(b1); i++)
2830 ga_append(&b->bv_ga, blob_get(b1, i));
2831 for (i = 0; i < blob_len(b2); i++)
2832 ga_append(&b->bv_ga, blob_get(b2, i));
2833
2834 clear_tv(tv1);
2835 rettv_blob_set(tv1, b);
2836 }
2837}
2838
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002839/*
2840 * Make a copy of list "tv1" and append list "tv2".
2841 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 int
2843eval_addlist(typval_T *tv1, typval_T *tv2)
2844{
2845 typval_T var3;
2846
2847 // concatenate Lists
2848 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2849 {
2850 clear_tv(tv1);
2851 clear_tv(tv2);
2852 return FAIL;
2853 }
2854 clear_tv(tv1);
2855 *tv1 = var3;
2856 return OK;
2857}
2858
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
2860 * Handle fourth level expression:
2861 * + number addition
2862 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002863 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002864 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 *
2866 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002867 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 *
2869 * Return OK or FAIL.
2870 */
2871 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 /*
2875 * Get the first variable.
2876 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return FAIL;
2879
2880 /*
2881 * Repeat computing, until no '+', '-' or '.' is following.
2882 */
2883 for (;;)
2884 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002885 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002886 int getnext;
2887 char_u *p;
2888 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 int concat;
2891 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002892 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002894 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002895 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002896 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 p = eval_next_non_blank(*arg, evalarg, &getnext);
2898 op = *p;
2899 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002900 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2901 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002903 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2904 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002905
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002906 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002907 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002909 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002910 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002911 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002912 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002913 {
mityu4ac198c2021-05-28 17:52:40 +02002914 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002915 clear_tv(rettv);
2916 return FAIL;
2917 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002918 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002919 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002920 if ((op != '+' || (rettv->v_type != VAR_LIST
2921 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922#ifdef FEAT_FLOAT
2923 && (op == '.' || rettv->v_type != VAR_FLOAT)
2924#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002925 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002926 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002927 int error = FALSE;
2928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002929 // For "list + ...", an illegal use of the first operand as
2930 // a number cannot be determined before evaluating the 2nd
2931 // operand: if this is also a list, all is ok.
2932 // For "something . ...", "something - ..." or "non-list + ...",
2933 // we know that the first operand needs to be a string or number
2934 // without evaluating the 2nd operand. So check before to avoid
2935 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002936 if (op != '.')
2937 tv_get_number_chk(rettv, &error);
2938 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002939 {
2940 clear_tv(rettv);
2941 return FAIL;
2942 }
2943 }
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /*
2946 * Get the second variable.
2947 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002948 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002949 {
mityu89dcb4d2021-05-28 13:50:17 +02002950 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002951 clear_tv(rettv);
2952 return FAIL;
2953 }
2954 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002955 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 return FAIL;
2959 }
2960
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 /*
2964 * Compute the result.
2965 */
2966 if (op == '.')
2967 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2969 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002970 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971
Bram Moolenaar2e086612020-08-25 22:37:48 +02002972 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002973 || var2.v_type == VAR_CHANNEL
2974 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002975 semsg(_(e_using_invalid_value_as_string_str),
2976 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002977#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002978 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002979 {
2980 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2981 var2.vval.v_float);
2982 s2 = buf2;
2983 }
2984#endif
2985 else
2986 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002987 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002988 {
2989 clear_tv(rettv);
2990 clear_tv(&var2);
2991 return FAIL;
2992 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 clear_tv(rettv);
2995 rettv->v_type = VAR_STRING;
2996 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002998 else if (op == '+' && rettv->v_type == VAR_BLOB
2999 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003001 else if (op == '+' && rettv->v_type == VAR_LIST
3002 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003005 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 else
3008 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003009 int error = FALSE;
3010 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003012 float_T f1 = 0, f2 = 0;
3013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016 f1 = rettv->vval.v_float;
3017 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 else
3020#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023 if (error)
3024 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003025 // This can only happen for "list + non-list" or
3026 // "blob + non-blob". For "non-list + ..." or
3027 // "something - ...", we returned before evaluating the
3028 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003030 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031 return FAIL;
3032 }
3033#ifdef FEAT_FLOAT
3034 if (var2.v_type == VAR_FLOAT)
3035 f1 = n1;
3036#endif
3037 }
3038#ifdef FEAT_FLOAT
3039 if (var2.v_type == VAR_FLOAT)
3040 {
3041 f2 = var2.vval.v_float;
3042 n2 = 0;
3043 }
3044 else
3045#endif
3046 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003047 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048 if (error)
3049 {
3050 clear_tv(rettv);
3051 clear_tv(&var2);
3052 return FAIL;
3053 }
3054#ifdef FEAT_FLOAT
3055 if (rettv->v_type == VAR_FLOAT)
3056 f2 = n2;
3057#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060
3061#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003062 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3064 {
3065 if (op == '+')
3066 f1 = f1 + f2;
3067 else
3068 f1 = f1 - f2;
3069 rettv->v_type = VAR_FLOAT;
3070 rettv->vval.v_float = f1;
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073#endif
3074 {
3075 if (op == '+')
3076 n1 = n1 + n2;
3077 else
3078 n1 = n1 - n2;
3079 rettv->v_type = VAR_NUMBER;
3080 rettv->vval.v_number = n1;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 }
3086 return OK;
3087}
3088
3089/*
3090 * Handle fifth level expression:
3091 * * number multiplication
3092 * / number division
3093 * % number modulo
3094 *
3095 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003096 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 *
3098 * Return OK or FAIL.
3099 */
3100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003101eval6(
3102 char_u **arg,
3103 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003104 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003105 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 /*
3112 * Get the first variable.
3113 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003114 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 return FAIL;
3116
3117 /*
3118 * Repeat computing, until no '*', '/' or '%' is following.
3119 */
3120 for (;;)
3121 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003122 int evaluate;
3123 int getnext;
3124 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003125 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003126 int op;
3127 varnumber_T n1, n2;
3128#ifdef FEAT_FLOAT
3129 float_T f1, f2;
3130#endif
3131 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003132
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003133 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003134 p = eval_next_non_blank(*arg, evalarg, &getnext);
3135 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003136 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003138
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003139 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003140 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003141 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003142 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003143 {
3144 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3145 {
mityu4ac198c2021-05-28 17:52:40 +02003146 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003147 clear_tv(rettv);
3148 return FAIL;
3149 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003150 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003153#ifdef FEAT_FLOAT
3154 f1 = 0;
3155 f2 = 0;
3156#endif
3157 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003158 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160#ifdef FEAT_FLOAT
3161 if (rettv->v_type == VAR_FLOAT)
3162 {
3163 f1 = rettv->vval.v_float;
3164 use_float = TRUE;
3165 n1 = 0;
3166 }
3167 else
3168#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003171 if (error)
3172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 else
3175 n1 = 0;
3176
3177 /*
3178 * Get the second variable.
3179 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003180 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3181 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003182 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003183 clear_tv(rettv);
3184 return FAIL;
3185 }
3186 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003187 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return FAIL;
3189
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003190 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192#ifdef FEAT_FLOAT
3193 if (var2.v_type == VAR_FLOAT)
3194 {
3195 if (!use_float)
3196 {
3197 f1 = n1;
3198 use_float = TRUE;
3199 }
3200 f2 = var2.vval.v_float;
3201 n2 = 0;
3202 }
3203 else
3204#endif
3205 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003206 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207 clear_tv(&var2);
3208 if (error)
3209 return FAIL;
3210#ifdef FEAT_FLOAT
3211 if (use_float)
3212 f2 = n2;
3213#endif
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 /*
3217 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#ifdef FEAT_FLOAT
3221 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 if (op == '*')
3224 f1 = f1 * f2;
3225 else if (op == '/')
3226 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003227# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003228 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003229 if (f2 == 0.0)
3230 {
3231 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003232 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003233 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003234 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003235 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003236 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003237 }
3238 else
3239 f1 = f1 / f2;
3240# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003241 // We rely on the floating point library to handle divide
3242 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003244# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003247 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003249 return FAIL;
3250 }
3251 rettv->v_type = VAR_FLOAT;
3252 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003257 int failed = FALSE;
3258
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003259 if (op == '*')
3260 n1 = n1 * n2;
3261 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003262 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003264 n1 = num_modulus(n1, n2, &failed);
3265 if (failed)
3266 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 rettv->v_type = VAR_NUMBER;
3269 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272 }
3273
3274 return OK;
3275}
3276
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003277/*
3278 * Handle a type cast before a base level expression.
3279 * "arg" must point to the first non-white of the expression.
3280 * "arg" is advanced to just after the recognized expression.
3281 * Return OK or FAIL.
3282 */
3283 static int
3284eval7t(
3285 char_u **arg,
3286 typval_T *rettv,
3287 evalarg_T *evalarg,
3288 int want_string) // after "." operator
3289{
3290 type_T *want_type = NULL;
3291 garray_T type_list; // list of pointers to allocated types
3292 int res;
3293 int evaluate = evalarg == NULL ? 0
3294 : (evalarg->eval_flags & EVAL_EVALUATE);
3295
3296 // Recognize <type> in Vim9 script only.
3297 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3298 {
3299 ++*arg;
3300 ga_init2(&type_list, sizeof(type_T *), 10);
3301 want_type = parse_type(arg, &type_list, TRUE);
3302 if (want_type == NULL && (evaluate || **arg != '>'))
3303 {
3304 clear_type_list(&type_list);
3305 return FAIL;
3306 }
3307
3308 if (**arg != '>')
3309 {
3310 if (*skipwhite(*arg) == '>')
3311 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3312 else
3313 emsg(_(e_missing_gt));
3314 clear_type_list(&type_list);
3315 return FAIL;
3316 }
3317 ++*arg;
3318 *arg = skipwhite_and_linebreak(*arg, evalarg);
3319 }
3320
3321 res = eval7(arg, rettv, evalarg, want_string);
3322
3323 if (want_type != NULL && evaluate)
3324 {
3325 if (res == OK)
3326 {
3327 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3328
3329 if (!equal_type(want_type, actual))
3330 {
3331 if (want_type == &t_bool && actual != &t_bool
3332 && (actual->tt_flags & TTFLAG_BOOL_OK))
3333 {
3334 int n = tv2bool(rettv);
3335
3336 // can use "0" and "1" for boolean in some places
3337 clear_tv(rettv);
3338 rettv->v_type = VAR_BOOL;
3339 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3340 }
3341 else
3342 {
3343 where_T where;
3344
3345 where.wt_index = 0;
3346 where.wt_variable = TRUE;
3347 res = check_type(want_type, actual, TRUE, where);
3348 }
3349 }
3350 }
3351 clear_type_list(&type_list);
3352 }
3353
3354 return res;
3355}
3356
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003357 int
3358eval_leader(char_u **arg, int vim9)
3359{
3360 char_u *s = *arg;
3361 char_u *p = *arg;
3362
3363 while (*p == '!' || *p == '-' || *p == '+')
3364 {
3365 char_u *n = skipwhite(p + 1);
3366
3367 // ++, --, -+ and +- are not accepted in Vim9 script
3368 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3369 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003370 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003371 return FAIL;
3372 }
3373 p = n;
3374 }
3375 *arg = p;
3376 return OK;
3377}
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * Handle sixth level expression:
3381 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003382 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003383 * "string" string constant
3384 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * &option-name option value
3386 * @r register contents
3387 * identifier variable value
3388 * function() function call
3389 * $VAR environment variable
3390 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003393 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003394 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 *
3396 * Also handle:
3397 * ! in front logical NOT
3398 * - in front unary minus
3399 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 * trailing [] subscript in String or List
3401 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003402 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 *
3404 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003405 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * Return OK or FAIL.
3408 */
3409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410eval7(
3411 char_u **arg,
3412 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003413 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003416 int evaluate = evalarg != NULL
3417 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 int len;
3419 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 char_u *start_leader, *end_leader;
3421 int ret = OK;
3422 char_u *alias;
3423
3424 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003426 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003431 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003434 if (eval_leader(arg, in_vim9script()) == FAIL)
3435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 end_leader = *arg;
3437
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003438 if (**arg == '.' && (!isdigit(*(*arg + 1))
3439#ifdef FEAT_FLOAT
3440 || current_sctx.sc_version < 2
3441#endif
3442 ))
3443 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003444 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003445 ++*arg;
3446 return FAIL;
3447 }
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 switch (**arg)
3450 {
3451 /*
3452 * Number constant.
3453 */
3454 case '0':
3455 case '1':
3456 case '2':
3457 case '3':
3458 case '4':
3459 case '5':
3460 case '6':
3461 case '7':
3462 case '8':
3463 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003464 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003465
3466 // Apply prefixed "-" and "+" now. Matters especially when
3467 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003468 if (ret == OK && evaluate && end_leader > start_leader
3469 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003470 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
3472
3473 /*
3474 * String constant: "string".
3475 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003476 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 break;
3478
3479 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003482 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 break;
3484
3485 /*
3486 * List: [expr, expr]
3487 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003488 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 break;
3490
3491 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003492 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003493 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003494 case '#': if (in_vim9script())
3495 {
3496 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3497 }
3498 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003499 {
3500 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003501 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003502 }
3503 else
3504 ret = NOTDONE;
3505 break;
3506
3507 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003508 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003509 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003510 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003511 case '{': if (in_vim9script())
3512 ret = NOTDONE;
3513 else
3514 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003515 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003516 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 break;
3518
3519 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003520 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003522 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 break;
3524
3525 /*
3526 * Environment variable: $VAR.
3527 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003528 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 break;
3530
3531 /*
3532 * Register contents: @r.
3533 */
3534 case '@': ++*arg;
3535 if (evaluate)
3536 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003537 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3538 semsg(_(e_syntax_error_at_str), *arg);
3539 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3540 emsg_invreg(**arg);
3541 else
3542 {
3543 rettv->v_type = VAR_STRING;
3544 rettv->vval.v_string = get_reg_contents(**arg,
3545 GREG_EXPR_SRC);
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 if (**arg != NUL)
3549 ++*arg;
3550 break;
3551
3552 /*
3553 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003554 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003556 case '(': ret = NOTDONE;
3557 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003558 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003559 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003560 if (ret == OK && evaluate)
3561 {
3562 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3563
Bram Moolenaara9931532021-06-12 15:58:16 +02003564 // Compile it here to get the return type. The return
3565 // type is optional, when it's missing use t_unknown.
3566 // This is recognized in compile_return().
3567 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3568 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003569 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003570 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003571 {
3572 clear_tv(rettv);
3573 ret = FAIL;
3574 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003575 }
3576 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003577 if (ret == NOTDONE)
3578 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003579 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003580 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003581
Bram Moolenaar9215f012020-06-27 21:18:00 +02003582 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003583 if (**arg == ')')
3584 ++*arg;
3585 else if (ret == OK)
3586 {
3587 emsg(_(e_missing_close));
3588 clear_tv(rettv);
3589 ret = FAIL;
3590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592 break;
3593
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 break;
3596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597
3598 if (ret == NOTDONE)
3599 {
3600 /*
3601 * Must be a variable or function name.
3602 * Can also be a curly-braces kind of name: {expr}.
3603 */
3604 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003605 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 if (alias != NULL)
3607 s = alias;
3608
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003609 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 ret = FAIL;
3611 else
3612 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003613 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3614
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003615 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003616 {
3617 emsg(_(e_cannot_use_underscore_here));
3618 ret = FAIL;
3619 }
3620 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003621 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003622 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003623 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003624 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003625 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003626 else if (flags & EVAL_CONSTANT)
3627 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003629 {
3630 // get the value of "true", "false" or a variable
3631 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3632 {
3633 rettv->v_type = VAR_BOOL;
3634 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003635 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003636 }
3637 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003638 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003639 {
3640 rettv->v_type = VAR_BOOL;
3641 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003642 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003643 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003644 else if (len == 4 && in_vim9script()
3645 && STRNCMP(s, "null", 4) == 0)
3646 {
3647 rettv->v_type = VAR_SPECIAL;
3648 rettv->vval.v_number = VVAL_NULL;
3649 ret = OK;
3650 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003651 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003652 ret = eval_variable(s, len, rettv, NULL,
3653 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003654 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003655 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003656 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003657 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003658 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003659 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003662 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 }
3664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003665 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3666 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003667 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003668 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /*
3671 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3672 */
3673 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003674 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003675 return ret;
3676}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003678/*
3679 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003680 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003681 * Adjusts "end_leaderp" until it is at "start_leader".
3682 */
3683 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003684eval7_leader(
3685 typval_T *rettv,
3686 int numeric_only,
3687 char_u *start_leader,
3688 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003689{
3690 char_u *end_leader = *end_leaderp;
3691 int ret = OK;
3692 int error = FALSE;
3693 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003694 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003695#ifdef FEAT_FLOAT
3696 float_T f = 0.0;
3697
3698 if (rettv->v_type == VAR_FLOAT)
3699 f = rettv->vval.v_float;
3700 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003701#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003702 {
3703 while (VIM_ISWHITE(end_leader[-1]))
3704 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003705 if (in_vim9script() && end_leader[-1] == '!')
3706 val = tv2bool(rettv);
3707 else
3708 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003709 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003710 if (error)
3711 {
3712 clear_tv(rettv);
3713 ret = FAIL;
3714 }
3715 else
3716 {
3717 while (end_leader > start_leader)
3718 {
3719 --end_leader;
3720 if (*end_leader == '!')
3721 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003722 if (numeric_only)
3723 {
3724 ++end_leader;
3725 break;
3726 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727#ifdef FEAT_FLOAT
3728 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003729 {
3730 if (in_vim9script())
3731 {
3732 rettv->v_type = VAR_BOOL;
3733 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3734 }
3735 else
3736 f = !f;
3737 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003738 else
3739#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003740 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003741 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003742 type = VAR_BOOL;
3743 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003744 }
3745 else if (*end_leader == '-')
3746 {
3747#ifdef FEAT_FLOAT
3748 if (rettv->v_type == VAR_FLOAT)
3749 f = -f;
3750 else
3751#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003752 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003753 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003754 type = VAR_NUMBER;
3755 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003756 }
3757 }
3758#ifdef FEAT_FLOAT
3759 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003762 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003765#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003767 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003768 if (in_vim9script())
3769 rettv->v_type = type;
3770 else
3771 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003775 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return ret;
3777}
3778
3779/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003780 * Call the function referred to in "rettv".
3781 */
3782 static int
3783call_func_rettv(
3784 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003785 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003786 typval_T *rettv,
3787 int evaluate,
3788 dict_T *selfdict,
3789 typval_T *basetv)
3790{
3791 partial_T *pt = NULL;
3792 funcexe_T funcexe;
3793 typval_T functv;
3794 char_u *s;
3795 int ret;
3796
3797 // need to copy the funcref so that we can clear rettv
3798 if (evaluate)
3799 {
3800 functv = *rettv;
3801 rettv->v_type = VAR_UNKNOWN;
3802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003803 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003804 if (functv.v_type == VAR_PARTIAL)
3805 {
3806 pt = functv.vval.v_partial;
3807 s = partial_name(pt);
3808 }
3809 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003810 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003811 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003812 if (s == NULL || *s == NUL)
3813 {
3814 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003815 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003816 goto theend;
3817 }
3818 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003819 }
3820 else
3821 s = (char_u *)"";
3822
Bram Moolenaara80faa82020-04-12 19:37:17 +02003823 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003824 funcexe.firstline = curwin->w_cursor.lnum;
3825 funcexe.lastline = curwin->w_cursor.lnum;
3826 funcexe.evaluate = evaluate;
3827 funcexe.partial = pt;
3828 funcexe.selfdict = selfdict;
3829 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003830 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003831
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003832theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003833 // Clear the funcref afterwards, so that deleting it while
3834 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835 if (evaluate)
3836 clear_tv(&functv);
3837
3838 return ret;
3839}
3840
3841/*
3842 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003843 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003844 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3845 */
3846 static int
3847eval_lambda(
3848 char_u **arg,
3849 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003850 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003852{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003853 int evaluate = evalarg != NULL
3854 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003855 typval_T base = *rettv;
3856 int ret;
3857
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003858 rettv->v_type = VAR_UNKNOWN;
3859
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003860 if (**arg == '{')
3861 {
3862 // ->{lambda}()
3863 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3864 }
3865 else
3866 {
3867 // ->(lambda)()
3868 ++*arg;
3869 ret = eval1(arg, rettv, evalarg);
3870 *arg = skipwhite_and_linebreak(*arg, evalarg);
3871 if (**arg != ')')
3872 {
3873 emsg(_(e_missing_close));
3874 ret = FAIL;
3875 }
3876 ++*arg;
3877 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003878 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003879 return FAIL;
3880 else if (**arg != '(')
3881 {
3882 if (verbose)
3883 {
3884 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003885 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003888 }
3889 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003890 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003891 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003892 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003893 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003894
3895 // Clear the funcref afterwards, so that deleting it while
3896 // evaluating the arguments is possible (see test55).
3897 if (evaluate)
3898 clear_tv(&base);
3899
3900 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003901}
3902
3903/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003904 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003905 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003906 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3907 */
3908 static int
3909eval_method(
3910 char_u **arg,
3911 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003912 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003913 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914{
3915 char_u *name;
3916 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003917 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003918 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003919 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003920 int evaluate = evalarg != NULL
3921 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003922
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003923 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003924
Bram Moolenaarac92e252019-08-03 21:58:38 +02003925 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003926 len = get_name_len(arg, &alias, evaluate, TRUE);
3927 if (alias != NULL)
3928 name = alias;
3929
3930 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 {
3932 if (verbose)
3933 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003934 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003936 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003938 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003939 if (**arg != '(')
3940 {
3941 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003943 ret = FAIL;
3944 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003945 else if (VIM_ISWHITE((*arg)[-1]))
3946 {
3947 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003948 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003949 ret = FAIL;
3950 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003951 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003952 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003953 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003954 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003955
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003956 // Clear the funcref afterwards, so that deleting it while
3957 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003958 if (evaluate)
3959 clear_tv(&base);
3960
Bram Moolenaarac92e252019-08-03 21:58:38 +02003961 return ret;
3962}
3963
3964/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003965 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3966 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3968 */
3969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003970eval_index(
3971 char_u **arg,
3972 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003973 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003976 int evaluate = evalarg != NULL
3977 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003978 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003981 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003982 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003983 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3986 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003987
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003988 init_tv(&var1);
3989 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 /*
3993 * dict.name
3994 */
3995 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003996 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003997 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003998 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003999 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004000 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001 }
4002 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 /*
4005 * something[idx]
4006 *
4007 * Get the (first) variable from inside the [].
4008 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004009 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004010 if (**arg == ':')
4011 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004012 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004014 else if (vim9 && **arg == ':')
4015 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004016 semsg(_(e_white_space_required_before_and_after_str_at_str),
4017 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004018 clear_tv(&var1);
4019 return FAIL;
4020 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004021 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004023#ifdef FEAT_FLOAT
4024 // allow for indexing with float
4025 if (vim9 && rettv->v_type == VAR_DICT
4026 && var1.v_type == VAR_FLOAT)
4027 {
4028 var1.vval.v_string = typval_tostring(&var1, TRUE);
4029 var1.v_type = VAR_STRING;
4030 }
4031#endif
4032 if (tv_get_string_chk(&var1) == NULL)
4033 {
4034 // not a number or string
4035 clear_tv(&var1);
4036 return FAIL;
4037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004039
4040 /*
4041 * Get the second variable from inside the [:].
4042 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004043 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 if (**arg == ':')
4045 {
4046 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004047 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004048 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004049 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004050 semsg(_(e_white_space_required_before_and_after_str_at_str),
4051 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004052 if (!empty1)
4053 clear_tv(&var1);
4054 return FAIL;
4055 }
4056 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 if (**arg == ']')
4058 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004059 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004061 if (!empty1)
4062 clear_tv(&var1);
4063 return FAIL;
4064 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004065 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004067 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 if (!empty1)
4069 clear_tv(&var1);
4070 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 return FAIL;
4072 }
4073 }
4074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004075 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004076 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004077 if (**arg != ']')
4078 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004079 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004081 clear_tv(&var1);
4082 if (range)
4083 clear_tv(&var2);
4084 return FAIL;
4085 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004086 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004087 }
4088
4089 if (evaluate)
4090 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004091 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004092 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004094
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004095 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004098 clear_tv(&var2);
4099 return res;
4100 }
4101 return OK;
4102}
4103
4104/*
4105 * Check if "rettv" can have an [index] or [sli:ce]
4106 */
4107 int
4108check_can_index(typval_T *rettv, int evaluate, int verbose)
4109{
4110 switch (rettv->v_type)
4111 {
4112 case VAR_FUNC:
4113 case VAR_PARTIAL:
4114 if (verbose)
4115 emsg(_("E695: Cannot index a Funcref"));
4116 return FAIL;
4117 case VAR_FLOAT:
4118#ifdef FEAT_FLOAT
4119 if (verbose)
4120 emsg(_(e_float_as_string));
4121 return FAIL;
4122#endif
4123 case VAR_BOOL:
4124 case VAR_SPECIAL:
4125 case VAR_JOB:
4126 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004127 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004128 if (verbose)
4129 emsg(_(e_cannot_index_special_variable));
4130 return FAIL;
4131 case VAR_UNKNOWN:
4132 case VAR_ANY:
4133 case VAR_VOID:
4134 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004135 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004136 emsg(_(e_cannot_index_special_variable));
4137 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004138 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004139 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004140
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004141 case VAR_STRING:
4142 case VAR_LIST:
4143 case VAR_DICT:
4144 case VAR_BLOB:
4145 break;
4146 case VAR_NUMBER:
4147 if (in_vim9script())
4148 emsg(_(e_cannot_index_number));
4149 break;
4150 }
4151 return OK;
4152}
4153
4154/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004155 * slice() function
4156 */
4157 void
4158f_slice(typval_T *argvars, typval_T *rettv)
4159{
4160 if (check_can_index(argvars, TRUE, FALSE) == OK)
4161 {
4162 copy_tv(argvars, rettv);
4163 eval_index_inner(rettv, TRUE, argvars + 1,
4164 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4165 TRUE, NULL, 0, FALSE);
4166 }
4167}
4168
4169/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 * Apply index or range to "rettv".
4171 * "var1" is the first index, NULL for [:expr].
4172 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004173 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4174 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4176 */
4177 int
4178eval_index_inner(
4179 typval_T *rettv,
4180 int is_range,
4181 typval_T *var1,
4182 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004183 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004184 char_u *key,
4185 int keylen,
4186 int verbose)
4187{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 varnumber_T n1, n2 = 0;
4189 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004190
4191 n1 = 0;
4192 if (var1 != NULL && rettv->v_type != VAR_DICT)
4193 n1 = tv_get_number(var1);
4194
4195 if (is_range)
4196 {
4197 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004198 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004199 if (verbose)
4200 emsg(_(e_cannot_slice_dictionary));
4201 return FAIL;
4202 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004203 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004204 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004205 else
4206 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004207 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004208
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 switch (rettv->v_type)
4210 {
4211 case VAR_UNKNOWN:
4212 case VAR_ANY:
4213 case VAR_VOID:
4214 case VAR_FUNC:
4215 case VAR_PARTIAL:
4216 case VAR_FLOAT:
4217 case VAR_BOOL:
4218 case VAR_SPECIAL:
4219 case VAR_JOB:
4220 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004221 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004222 break; // not evaluating, skipping over subscript
4223
4224 case VAR_NUMBER:
4225 case VAR_STRING:
4226 {
4227 char_u *s = tv_get_string(rettv);
4228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004230 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004231 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004233 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004234 else
4235 s = char_from_string(s, n1);
4236 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004239 // The resulting variable is a substring. If the indexes
4240 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 if (n1 < 0)
4242 {
4243 n1 = len + n1;
4244 if (n1 < 0)
4245 n1 = 0;
4246 }
4247 if (n2 < 0)
4248 n2 = len + n2;
4249 else if (n2 >= len)
4250 n2 = len;
4251 if (n1 >= len || n2 < 0 || n1 > n2)
4252 s = NULL;
4253 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004254 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 }
4256 else
4257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004258 // The resulting variable is a string of a single
4259 // character. If the index is too big or negative the
4260 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 if (n1 >= len || n1 < 0)
4262 s = NULL;
4263 else
4264 s = vim_strnsave(s + n1, 1);
4265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
4267 rettv->v_type = VAR_STRING;
4268 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004269 }
4270 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004272 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004273 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4274 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 break;
4276
4277 case VAR_LIST:
4278 if (var1 == NULL)
4279 n1 = 0;
4280 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004281 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004282 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004283 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004284 return FAIL;
4285 break;
4286
4287 case VAR_DICT:
4288 {
4289 dictitem_T *item;
4290 typval_T tmp;
4291
4292 if (key == NULL)
4293 {
4294 key = tv_get_string_chk(var1);
4295 if (key == NULL)
4296 return FAIL;
4297 }
4298
4299 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4300
4301 if (item == NULL && verbose)
4302 semsg(_(e_dictkey), key);
4303 if (item == NULL)
4304 return FAIL;
4305
4306 copy_tv(&item->di_tv, &tmp);
4307 clear_tv(rettv);
4308 *rettv = tmp;
4309 }
4310 break;
4311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004312 return OK;
4313}
4314
4315/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004316 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004317 */
4318 char_u *
4319partial_name(partial_T *pt)
4320{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004321 if (pt != NULL)
4322 {
4323 if (pt->pt_name != NULL)
4324 return pt->pt_name;
4325 if (pt->pt_func != NULL)
4326 return pt->pt_func->uf_name;
4327 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004328 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004329}
4330
Bram Moolenaarddecc252016-04-06 22:59:37 +02004331 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004332partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004333{
4334 int i;
4335
4336 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004337 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004338 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004339 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004340 if (pt->pt_name != NULL)
4341 {
4342 func_unref(pt->pt_name);
4343 vim_free(pt->pt_name);
4344 }
4345 else
4346 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004347
Bram Moolenaar54656012021-06-09 20:50:46 +02004348 // "out_up" is no longer used, decrement refcount on partial that owns it.
4349 partial_unref(pt->pt_outer.out_up_partial);
4350
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004351 // Decrease the reference count for the context of a closure. If down
4352 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004353 if (pt->pt_funcstack != NULL)
4354 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004355 --pt->pt_funcstack->fs_refcount;
4356 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004357 }
4358
Bram Moolenaarddecc252016-04-06 22:59:37 +02004359 vim_free(pt);
4360}
4361
4362/*
4363 * Unreference a closure: decrement the reference count and free it when it
4364 * becomes zero.
4365 */
4366 void
4367partial_unref(partial_T *pt)
4368{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004369 if (pt != NULL)
4370 {
4371 if (--pt->pt_refcount <= 0)
4372 partial_free(pt);
4373
4374 // If the reference count goes down to one, the funcstack may be the
4375 // only reference and can be freed if no other partials reference it.
4376 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4377 funcstack_check_refcount(pt->pt_funcstack);
4378 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004379}
4380
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004382 * Return the next (unique) copy ID.
4383 * Used for serializing nested structures.
4384 */
4385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004386get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004387{
4388 current_copyID += COPYID_INC;
4389 return current_copyID;
4390}
4391
4392/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004393 * Garbage collection for lists and dictionaries.
4394 *
4395 * We use reference counts to be able to free most items right away when they
4396 * are no longer used. But for composite items it's possible that it becomes
4397 * unused while the reference count is > 0: When there is a recursive
4398 * reference. Example:
4399 * :let l = [1, 2, 3]
4400 * :let d = {9: l}
4401 * :let l[1] = d
4402 *
4403 * Since this is quite unusual we handle this with garbage collection: every
4404 * once in a while find out which lists and dicts are not referenced from any
4405 * variable.
4406 *
4407 * Here is a good reference text about garbage collection (refers to Python
4408 * but it applies to all reference-counting mechanisms):
4409 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004410 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004411
4412/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004413 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004414 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004415 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004416 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004417 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004418garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004419{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004420 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004421 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004422 buf_T *buf;
4423 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004424 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004425 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004426
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004427 if (!testing)
4428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004429 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004430 want_garbage_collect = FALSE;
4431 may_garbage_collect = FALSE;
4432 garbage_collect_at_exit = FALSE;
4433 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004434
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004435 // The execution stack can grow big, limit the size.
4436 if (exestack.ga_maxlen - exestack.ga_len > 500)
4437 {
4438 size_t new_len;
4439 char_u *pp;
4440 int n;
4441
4442 // Keep 150% of the current size, with a minimum of the growth size.
4443 n = exestack.ga_len / 2;
4444 if (n < exestack.ga_growsize)
4445 n = exestack.ga_growsize;
4446
4447 // Don't make it bigger though.
4448 if (exestack.ga_len + n < exestack.ga_maxlen)
4449 {
4450 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4451 pp = vim_realloc(exestack.ga_data, new_len);
4452 if (pp == NULL)
4453 return FAIL;
4454 exestack.ga_maxlen = exestack.ga_len + n;
4455 exestack.ga_data = pp;
4456 }
4457 }
4458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // We advance by two because we add one for items referenced through
4460 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004461 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004462
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004463 /*
4464 * 1. Go through all accessible variables and mark all lists and dicts
4465 * with copyID.
4466 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // Don't free variables in the previous_funccal list unless they are only
4469 // referenced through previous_funccal. This must be first, because if
4470 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004474 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004477 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004478 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4479 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004482 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004483 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4484 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004485 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004486 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4487 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004488#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004489 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004490 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4491 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004492 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004493 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004494 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4495 NULL, NULL);
4496#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004498 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004499 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4501 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004503 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004505 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004508 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004509 abort = abort || set_ref_in_functions(copyID);
4510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004515 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004516
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004517 // callbacks in buffers
4518 abort = abort || set_ref_in_buffers(copyID);
4519
Bram Moolenaar1dced572012-04-05 16:54:08 +02004520#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004522#endif
4523
Bram Moolenaardb913952012-06-29 12:54:53 +02004524#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004525 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004526#endif
4527
4528#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004530#endif
4531
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004533 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004534 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004535#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004536#ifdef FEAT_NETBEANS_INTG
4537 abort = abort || set_ref_in_nb_channel(copyID);
4538#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004539
Bram Moolenaare3188e22016-05-31 21:13:04 +02004540#ifdef FEAT_TIMERS
4541 abort = abort || set_ref_in_timer(copyID);
4542#endif
4543
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004544#ifdef FEAT_QUICKFIX
4545 abort = abort || set_ref_in_quickfix(copyID);
4546#endif
4547
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004548#ifdef FEAT_TERMINAL
4549 abort = abort || set_ref_in_term(copyID);
4550#endif
4551
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004552#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004553 abort = abort || set_ref_in_popups(copyID);
4554#endif
4555
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004556 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004557 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004558 /*
4559 * 2. Free lists and dictionaries that are not referenced.
4560 */
4561 did_free = free_unref_items(copyID);
4562
4563 /*
4564 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004565 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004566 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004567 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004568 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004569 else if (p_verbose > 0)
4570 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004571 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004573
4574 return did_free;
4575}
4576
4577/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004578 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004579 */
4580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004581free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004582{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004583 int did_free = FALSE;
4584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // Let all "free" functions know that we are here. This means no
4586 // dictionaries, lists, channels or jobs are to be freed, because we will
4587 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588 in_free_unref_items = TRUE;
4589
4590 /*
4591 * PASS 1: free the contents of the items. We don't free the items
4592 * themselves yet, so that it is possible to decrement refcount counters
4593 */
4594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004595 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004596 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004598 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004599 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004600
4601#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 // Go through the list of jobs and free items without the copyID. This
4603 // must happen before doing channels, because jobs refer to channels, but
4604 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004608 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4609#endif
4610
4611 /*
4612 * PASS 2: free the items themselves.
4613 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004614 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004615 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004616
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004617#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 // Go through the list of jobs and free items without the copyID. This
4619 // must happen before doing channels, because jobs refer to channels, but
4620 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 free_unused_jobs(copyID, COPYID_MASK);
4622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004624 free_unused_channels(copyID, COPYID_MASK);
4625#endif
4626
4627 in_free_unref_items = FALSE;
4628
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004629 return did_free;
4630}
4631
4632/*
4633 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004634 * "list_stack" is used to add lists to be marked. Can be NULL.
4635 *
4636 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004637 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004638 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004639set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640{
4641 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004642 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004643 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644 hashtab_T *cur_ht;
4645 ht_stack_T *ht_stack = NULL;
4646 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004647
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 cur_ht = ht;
4649 for (;;)
4650 {
4651 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004652 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004653 // Mark each item in the hashtab. If the item contains a hashtab
4654 // it is added to ht_stack, if it contains a list it is added to
4655 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004656 todo = (int)cur_ht->ht_used;
4657 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4658 if (!HASHITEM_EMPTY(hi))
4659 {
4660 --todo;
4661 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4662 &ht_stack, list_stack);
4663 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004664 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004665
4666 if (ht_stack == NULL)
4667 break;
4668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670 cur_ht = ht_stack->ht;
4671 tempitem = ht_stack;
4672 ht_stack = ht_stack->prev;
4673 free(tempitem);
4674 }
4675
4676 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004677}
4678
4679/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004680 * Mark a dict and its items with "copyID".
4681 * Returns TRUE if setting references failed somehow.
4682 */
4683 int
4684set_ref_in_dict(dict_T *d, int copyID)
4685{
4686 if (d != NULL && d->dv_copyID != copyID)
4687 {
4688 d->dv_copyID = copyID;
4689 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4690 }
4691 return FALSE;
4692}
4693
4694/*
4695 * Mark a list and its items with "copyID".
4696 * Returns TRUE if setting references failed somehow.
4697 */
4698 int
4699set_ref_in_list(list_T *ll, int copyID)
4700{
4701 if (ll != NULL && ll->lv_copyID != copyID)
4702 {
4703 ll->lv_copyID = copyID;
4704 return set_ref_in_list_items(ll, copyID, NULL);
4705 }
4706 return FALSE;
4707}
4708
4709/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004710 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004711 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4712 *
4713 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004714 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004715 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004716set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004717{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004718 listitem_T *li;
4719 int abort = FALSE;
4720 list_T *cur_l;
4721 list_stack_T *list_stack = NULL;
4722 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004723
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004724 cur_l = l;
4725 for (;;)
4726 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004727 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // Mark each item in the list. If the item contains a hashtab
4729 // it is added to ht_stack, if it contains a list it is added to
4730 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004731 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4732 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4733 ht_stack, &list_stack);
4734 if (list_stack == NULL)
4735 break;
4736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004737 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 cur_l = list_stack->list;
4739 tempitem = list_stack;
4740 list_stack = list_stack->prev;
4741 free(tempitem);
4742 }
4743
4744 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004745}
4746
4747/*
4748 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004749 * "list_stack" is used to add lists to be marked. Can be NULL.
4750 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4751 *
4752 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004753 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755set_ref_in_item(
4756 typval_T *tv,
4757 int copyID,
4758 ht_stack_T **ht_stack,
4759 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004760{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004761 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004762
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004763 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004764 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004765 dict_T *dd = tv->vval.v_dict;
4766
Bram Moolenaara03f2332016-02-06 18:09:59 +01004767 if (dd != NULL && dd->dv_copyID != copyID)
4768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004770 dd->dv_copyID = copyID;
4771 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004772 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004773 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4774 }
4775 else
4776 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004777 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4778
Bram Moolenaara03f2332016-02-06 18:09:59 +01004779 if (newitem == NULL)
4780 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004781 else
4782 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004783 newitem->ht = &dd->dv_hashtab;
4784 newitem->prev = *ht_stack;
4785 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004786 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004787 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004788 }
4789 }
4790 else if (tv->v_type == VAR_LIST)
4791 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 list_T *ll = tv->vval.v_list;
4793
Bram Moolenaara03f2332016-02-06 18:09:59 +01004794 if (ll != NULL && ll->lv_copyID != copyID)
4795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004797 ll->lv_copyID = copyID;
4798 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004799 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004800 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004801 }
4802 else
4803 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004804 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4805
Bram Moolenaara03f2332016-02-06 18:09:59 +01004806 if (newitem == NULL)
4807 abort = TRUE;
4808 else
4809 {
4810 newitem->list = ll;
4811 newitem->prev = *list_stack;
4812 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004813 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004814 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004815 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004816 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004817 else if (tv->v_type == VAR_FUNC)
4818 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004819 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004820 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004821 else if (tv->v_type == VAR_PARTIAL)
4822 {
4823 partial_T *pt = tv->vval.v_partial;
4824 int i;
4825
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004826 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004827 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004828 // Didn't see this partial yet.
4829 pt->pt_copyID = copyID;
4830
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004831 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004832
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004833 if (pt->pt_dict != NULL)
4834 {
4835 typval_T dtv;
4836
4837 dtv.v_type = VAR_DICT;
4838 dtv.vval.v_dict = pt->pt_dict;
4839 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4840 }
4841
4842 for (i = 0; i < pt->pt_argc; ++i)
4843 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4844 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004845 if (pt->pt_funcstack != NULL)
4846 {
4847 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4848
4849 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4850 abort = abort || set_ref_in_item(stack + i, copyID,
4851 ht_stack, list_stack);
4852 }
4853
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004854 }
4855 }
4856#ifdef FEAT_JOB_CHANNEL
4857 else if (tv->v_type == VAR_JOB)
4858 {
4859 job_T *job = tv->vval.v_job;
4860 typval_T dtv;
4861
4862 if (job != NULL && job->jv_copyID != copyID)
4863 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004864 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004865 if (job->jv_channel != NULL)
4866 {
4867 dtv.v_type = VAR_CHANNEL;
4868 dtv.vval.v_channel = job->jv_channel;
4869 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4870 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004872 {
4873 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004875 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4876 }
4877 }
4878 }
4879 else if (tv->v_type == VAR_CHANNEL)
4880 {
4881 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004882 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004883 typval_T dtv;
4884 jsonq_T *jq;
4885 cbq_T *cq;
4886
4887 if (ch != NULL && ch->ch_copyID != copyID)
4888 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004889 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004890 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 {
4892 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4893 jq = jq->jq_next)
4894 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4895 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4896 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004897 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 {
4899 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4902 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004903 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004904 {
4905 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004906 dtv.vval.v_partial =
4907 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4909 }
4910 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004911 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004912 {
4913 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004914 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4916 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004917 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004918 {
4919 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004920 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004921 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4922 }
4923 }
4924 }
4925#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004926 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004927}
4928
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 * Return a string with the string representation of a variable.
4931 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004932 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004933 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004934 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004935 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004936 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004937 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4938 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004939 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004941 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004942echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943 typval_T *tv,
4944 char_u **tofree,
4945 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004946 int copyID,
4947 int echo_style,
4948 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004949 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004950{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004951 static int recurse = 0;
4952 char_u *r = NULL;
4953
Bram Moolenaar33570922005-01-25 22:26:29 +00004954 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004955 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004956 if (!did_echo_string_emsg)
4957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 // Only give this message once for a recursive call to avoid
4959 // flooding the user with errors. And stop iterating over lists
4960 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004961 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004962 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004963 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004964 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004965 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004966 }
4967 ++recurse;
4968
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 switch (tv->v_type)
4970 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004971 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004972 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004973 {
4974 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004975 r = tv->vval.v_string;
4976 if (r == NULL)
4977 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004978 }
4979 else
4980 {
4981 *tofree = string_quote(tv->vval.v_string, FALSE);
4982 r = *tofree;
4983 }
4984 break;
4985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004987 if (echo_style)
4988 {
4989 *tofree = NULL;
4990 r = tv->vval.v_string;
4991 }
4992 else
4993 {
4994 *tofree = string_quote(tv->vval.v_string, TRUE);
4995 r = *tofree;
4996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004997 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004998
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004999 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005000 {
5001 partial_T *pt = tv->vval.v_partial;
5002 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005003 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005004 garray_T ga;
5005 int i;
5006 char_u *tf;
5007
5008 ga_init2(&ga, 1, 100);
5009 ga_concat(&ga, (char_u *)"function(");
5010 if (fname != NULL)
5011 {
5012 ga_concat(&ga, fname);
5013 vim_free(fname);
5014 }
5015 if (pt != NULL && pt->pt_argc > 0)
5016 {
5017 ga_concat(&ga, (char_u *)", [");
5018 for (i = 0; i < pt->pt_argc; ++i)
5019 {
5020 if (i > 0)
5021 ga_concat(&ga, (char_u *)", ");
5022 ga_concat(&ga,
5023 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5024 vim_free(tf);
5025 }
5026 ga_concat(&ga, (char_u *)"]");
5027 }
5028 if (pt != NULL && pt->pt_dict != NULL)
5029 {
5030 typval_T dtv;
5031
5032 ga_concat(&ga, (char_u *)", ");
5033 dtv.v_type = VAR_DICT;
5034 dtv.vval.v_dict = pt->pt_dict;
5035 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5036 vim_free(tf);
5037 }
5038 ga_concat(&ga, (char_u *)")");
5039
5040 *tofree = ga.ga_data;
5041 r = *tofree;
5042 break;
5043 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005044
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005045 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005046 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005047 break;
5048
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005049 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005050 if (tv->vval.v_list == NULL)
5051 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005052 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005053 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005054 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005055 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005056 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5057 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 {
5059 *tofree = NULL;
5060 r = (char_u *)"[...]";
5061 }
5062 else
5063 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005064 int old_copyID = tv->vval.v_list->lv_copyID;
5065
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005066 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005067 *tofree = list2string(tv, copyID, restore_copyID);
5068 if (restore_copyID)
5069 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005070 r = *tofree;
5071 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005072 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005073
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005075 if (tv->vval.v_dict == NULL)
5076 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005077 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005079 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005080 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005081 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5082 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005083 {
5084 *tofree = NULL;
5085 r = (char_u *)"{...}";
5086 }
5087 else
5088 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005089 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005090
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005091 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005092 *tofree = dict2string(tv, copyID, restore_copyID);
5093 if (restore_copyID)
5094 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005095 r = *tofree;
5096 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005097 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005100 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005101 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005103 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005104 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005105 break;
5106
Bram Moolenaar835dc632016-02-07 14:27:38 +01005107 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005108 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005111 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5112 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005113 if (composite_val)
5114 {
5115 *tofree = string_quote(r, FALSE);
5116 r = *tofree;
5117 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005118#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005120
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005121 case VAR_INSTR:
5122 *tofree = NULL;
5123 r = (char_u *)"instructions";
5124 break;
5125
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005127#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005128 *tofree = NULL;
5129 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5130 r = numbuf;
5131 break;
5132#endif
5133
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005134 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005135 case VAR_SPECIAL:
5136 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005137 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005138 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140
Bram Moolenaar8502c702014-06-17 12:51:16 +02005141 if (--recurse == 0)
5142 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005143 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144}
5145
5146/*
5147 * Return a string with the string representation of a variable.
5148 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5149 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005150 * Does not put quotes around strings, as ":echo" displays values.
5151 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5152 * May return NULL.
5153 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005155echo_string(
5156 typval_T *tv,
5157 char_u **tofree,
5158 char_u *numbuf,
5159 int copyID)
5160{
5161 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5162}
5163
5164/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005165 * Return string "str" in ' quotes, doubling ' characters.
5166 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005168 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005169 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005170string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005171{
Bram Moolenaar33570922005-01-25 22:26:29 +00005172 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 char_u *p, *r, *s;
5174
Bram Moolenaar33570922005-01-25 22:26:29 +00005175 len = (function ? 13 : 3);
5176 if (str != NULL)
5177 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005178 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005179 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005180 if (*p == '\'')
5181 ++len;
5182 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 s = r = alloc(len);
5184 if (r != NULL)
5185 {
5186 if (function)
5187 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189 r += 10;
5190 }
5191 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005193 if (str != NULL)
5194 for (p = str; *p != NUL; )
5195 {
5196 if (*p == '\'')
5197 *r++ = '\'';
5198 MB_COPY_CHAR(p, r);
5199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005201 if (function)
5202 *r++ = ')';
5203 *r++ = NUL;
5204 }
5205 return s;
5206}
5207
5208/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005209 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5210 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005211 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005212 */
5213 int
5214buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5215{
5216 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005217 char_u *t;
5218 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005219
5220 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5221 return -1;
5222
5223 if (lnum > buf->b_ml.ml_line_count)
5224 lnum = buf->b_ml.ml_line_count;
5225
5226 str = ml_get_buf(buf, lnum, FALSE);
5227 if (str == NULL)
5228 return -1;
5229
5230 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005231 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005232
Bram Moolenaar91458462021-01-13 20:08:38 +01005233 // count the number of characters
5234 t = str;
5235 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5236 t += mb_ptr2len(t);
5237
5238 // In insert mode, when the cursor is at the end of a non-empty line,
5239 // byteidx points to the NUL character immediately past the end of the
5240 // string. In this case, add one to the character count.
5241 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5242 count++;
5243
5244 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005245}
5246
5247/*
5248 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005249 * byte index. Works only for loaded buffers. Returns -1 on failure.
5250 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251 */
5252 int
5253buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5254{
5255 char_u *str;
5256 char_u *t;
5257
5258 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5259 return -1;
5260
5261 if (lnum > buf->b_ml.ml_line_count)
5262 lnum = buf->b_ml.ml_line_count;
5263
5264 str = ml_get_buf(buf, lnum, FALSE);
5265 if (str == NULL)
5266 return -1;
5267
5268 // Convert the character offset to a byte offset
5269 t = str;
5270 while (*t != NUL && --charidx > 0)
5271 t += mb_ptr2len(t);
5272
Bram Moolenaar91458462021-01-13 20:08:38 +01005273 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005274}
5275
5276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005278 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005281var2fpos(
5282 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005284 int *fnum, // set to fnum for '0, 'A, etc.
5285 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005287 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005289 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005292 if (varp->v_type == VAR_LIST)
5293 {
5294 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005295 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005296 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005297 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005298
5299 l = varp->vval.v_list;
5300 if (l == NULL)
5301 return NULL;
5302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005304 pos.lnum = list_find_nr(l, 0L, &error);
5305 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005306 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005307 if (charcol)
5308 len = (long)mb_charlen(ml_get(pos.lnum));
5309 else
5310 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005311
Bram Moolenaarec65d772020-08-20 22:29:12 +02005312 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005314 li = list_find(l, 1L);
5315 if (li != NULL && li->li_tv.v_type == VAR_STRING
5316 && li->li_tv.vval.v_string != NULL
5317 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005318 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005319 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005320 }
5321 else
5322 {
5323 pos.col = list_find_nr(l, 1L, &error);
5324 if (error)
5325 return NULL;
5326 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005329 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005331 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005333 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005334 pos.coladd = list_find_nr(l, 2L, &error);
5335 if (error)
5336 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005337
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005338 return &pos;
5339 }
5340
Bram Moolenaarc5809432021-03-27 21:23:30 +01005341 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5342 return NULL;
5343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005344 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 if (name == NULL)
5346 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005348 {
5349 pos = curwin->w_cursor;
5350 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005351 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005352 return &pos;
5353 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005355 {
5356 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 pos = VIsual;
5358 else
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;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005366 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5368 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005369 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005370 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return pp;
5372 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005373
Bram Moolenaara5525202006-03-02 22:52:09 +00005374 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005375
Bram Moolenaar477933c2007-07-17 14:32:23 +00005376 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005377 {
5378 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005380 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005381 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 // In silent Ex mode topline is zero, but that's not a valid line
5383 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005384 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 return &pos;
5386 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005389 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005391 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005392 return &pos;
5393 }
5394 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005397 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 pos.lnum = curbuf->b_ml.ml_line_count;
5400 pos.col = 0;
5401 }
5402 else
5403 {
5404 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005405 if (charcol)
5406 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5407 else
5408 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 return &pos;
5411 }
5412 return NULL;
5413}
5414
5415/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005416 * Convert list in "arg" into a position and optional file number.
5417 * When "fnump" is NULL there is no file number, only 3 items.
5418 * Note that the column is passed on as-is, the caller may want to decrement
5419 * it to use 1 for the first column.
5420 * Return FAIL when conversion is not possible, doesn't check the position for
5421 * validity.
5422 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424list2fpos(
5425 typval_T *arg,
5426 pos_T *posp,
5427 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005428 colnr_T *curswantp,
5429 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005430{
5431 list_T *l = arg->vval.v_list;
5432 long i = 0;
5433 long n;
5434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5436 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005437 if (arg->v_type != VAR_LIST
5438 || l == NULL
5439 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005440 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005441 return FAIL;
5442
5443 if (fnump != NULL)
5444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005446 if (n < 0)
5447 return FAIL;
5448 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005450 *fnump = n;
5451 }
5452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005454 if (n < 0)
5455 return FAIL;
5456 posp->lnum = n;
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005459 if (n < 0)
5460 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005461 // If character position is specified, then convert to byte position
5462 if (charcol)
5463 {
5464 buf_T *buf;
5465
5466 // Get the text for the specified line in a loaded buffer
5467 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5468 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5469 return FAIL;
5470
Bram Moolenaar91458462021-01-13 20:08:38 +01005471 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005472 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005473 posp->col = n;
5474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005476 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005477 posp->coladd = 0;
5478 else
5479 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005480
Bram Moolenaar493c1782014-05-28 14:34:46 +02005481 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005483
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005484 return OK;
5485}
5486
5487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 * Get the length of an environment variable name.
5489 * Advance "arg" to the first character after the name.
5490 * Return 0 for error.
5491 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005493get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 char_u *p;
5496 int len;
5497
5498 for (p = *arg; vim_isIDc(*p); ++p)
5499 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 return 0;
5502
5503 len = (int)(p - *arg);
5504 *arg = p;
5505 return len;
5506}
5507
5508/*
5509 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005510 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 * Return 0 if something is wrong.
5512 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 char_u *p;
5517 int len;
5518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005521 {
5522 if (*p == ':')
5523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005524 // "s:" is start of "s:var", but "n:" is not and can be used in
5525 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005526 len = (int)(p - *arg);
5527 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5528 || len > 1)
5529 break;
5530 }
5531 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return 0;
5534
5535 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005536 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 return len;
5539}
5540
5541/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005542 * Get the length of the name of a variable or function.
5543 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005545 * Return -1 if curly braces expansion failed.
5546 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * If the name contains 'magic' {}'s, expand them and return the
5548 * expanded name in an allocated string via 'alias' - caller must free.
5549 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005550 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005551get_name_len(
5552 char_u **arg,
5553 char_u **alias,
5554 int evaluate,
5555 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 char_u *p;
5559 char_u *expr_start;
5560 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5565 && (*arg)[2] == (int)KE_SNR)
5566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 *arg += 3;
5569 return get_id_len(arg) + 3;
5570 }
5571 len = eval_fname_script(*arg);
5572 if (len > 0)
5573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 *arg += len;
5576 }
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005581 p = find_name_end(*arg, &expr_start, &expr_end,
5582 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (expr_start != NULL)
5584 {
5585 char_u *temp_string;
5586
5587 if (!evaluate)
5588 {
5589 len += (int)(p - *arg);
5590 *arg = skipwhite(p);
5591 return len;
5592 }
5593
5594 /*
5595 * Include any <SID> etc in the expanded string:
5596 * Thus the -len here.
5597 */
5598 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5599 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 *alias = temp_string;
5602 *arg = skipwhite(p);
5603 return (int)STRLEN(temp_string);
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005607 // Only give an error when there is something, otherwise it will be
5608 // reported at a higher level.
5609 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005610 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 return len;
5613}
5614
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615/*
5616 * Find the end of a variable or function name, taking care of magic braces.
5617 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5618 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005619 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 * Return a pointer to just after the name. Equal to "arg" if there is no
5621 * valid name.
5622 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005623 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005624find_name_end(
5625 char_u *arg,
5626 char_u **expr_start,
5627 char_u **expr_end,
5628 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 int mb_nest = 0;
5631 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005633 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005634 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 *expr_start = NULL;
5639 *expr_end = NULL;
5640 }
5641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005642 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005643 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5644 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005645 return arg;
5646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 for (p = arg; *p != NUL
5648 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005649 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005650 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005651 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005653 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005655 if (*p == '\'')
5656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005657 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005658 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005659 ;
5660 if (*p == NUL)
5661 break;
5662 }
5663 else if (*p == '"')
5664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005666 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005667 if (*p == '\\' && p[1] != NUL)
5668 ++p;
5669 if (*p == NUL)
5670 break;
5671 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005672 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // "s:" is start of "s:var", but "n:" is not and can be used in
5675 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005676 len = (int)(p - arg);
5677 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005678 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005679 break;
5680 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 if (*p == '[')
5685 ++br_nest;
5686 else if (*p == ']')
5687 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005689
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005690 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 if (*p == '{')
5693 {
5694 mb_nest++;
5695 if (expr_start != NULL && *expr_start == NULL)
5696 *expr_start = p;
5697 }
5698 else if (*p == '}')
5699 {
5700 mb_nest--;
5701 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5702 *expr_end = p;
5703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706
5707 return p;
5708}
5709
5710/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005711 * Expands out the 'magic' {}'s in a variable/function name.
5712 * Note that this can call itself recursively, to deal with
5713 * constructs like foo{bar}{baz}{bam}
5714 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5715 * "in_start" ^
5716 * "expr_start" ^
5717 * "expr_end" ^
5718 * "in_end" ^
5719 *
5720 * Returns a new allocated string, which the caller must free.
5721 * Returns NULL for failure.
5722 */
5723 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005724make_expanded_name(
5725 char_u *in_start,
5726 char_u *expr_start,
5727 char_u *expr_end,
5728 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005729{
5730 char_u c1;
5731 char_u *retval = NULL;
5732 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005733
5734 if (expr_end == NULL || in_end == NULL)
5735 return NULL;
5736 *expr_start = NUL;
5737 *expr_end = NUL;
5738 c1 = *in_end;
5739 *in_end = NUL;
5740
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005741 temp_result = eval_to_string(expr_start + 1, FALSE);
5742 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005744 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5745 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746 if (retval != NULL)
5747 {
5748 STRCPY(retval, in_start);
5749 STRCAT(retval, temp_result);
5750 STRCAT(retval, expr_end + 1);
5751 }
5752 }
5753 vim_free(temp_result);
5754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005756 *expr_start = '{';
5757 *expr_end = '}';
5758
5759 if (retval != NULL)
5760 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005761 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005762 if (expr_start != NULL)
5763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005764 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005765 temp_result = make_expanded_name(retval, expr_start,
5766 expr_end, temp_result);
5767 vim_free(retval);
5768 retval = temp_result;
5769 }
5770 }
5771
5772 return retval;
5773}
5774
5775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005777 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005780eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005782 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005783}
5784
5785/*
5786 * Return TRUE if character "c" can be used as the first character in a
5787 * variable or function name (excluding '{' and '}').
5788 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005790eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005791{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005792 return ASCII_ISALPHA(c) || c == '_';
5793}
5794
5795/*
5796 * Return TRUE if character "c" can be used as the first character of a
5797 * dictionary key.
5798 */
5799 int
5800eval_isdictc(int c)
5801{
5802 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803}
5804
5805/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005806 * Handle:
5807 * - expr[expr], expr[expr:expr] subscript
5808 * - ".name" lookup
5809 * - function call with Funcref variable: func(expr)
5810 * - method call: var->method()
5811 *
5812 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005813 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005815handle_subscript(
5816 char_u **arg,
5817 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005818 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005819 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005820{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005821 int evaluate = evalarg != NULL
5822 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005823 int ret = OK;
5824 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005825 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005826 int getnext;
5827 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005828
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005829 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005830 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005831 // When at the end of the line and ".name" or "->{" or "->X" follows in
5832 // the next line then consume the line break.
5833 p = eval_next_non_blank(*arg, evalarg, &getnext);
5834 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005835 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005836 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5837 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5838 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005839 {
5840 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005841 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005842 check_white = FALSE;
5843 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005844
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005845 if (rettv->v_type == VAR_ANY)
5846 {
5847 char_u *exp_name;
5848 int cc;
5849 int idx;
5850 ufunc_T *ufunc;
5851 type_T *type;
5852
5853 // Found script from "import * as {name}", script item name must
5854 // follow.
5855 if (**arg != '.')
5856 {
5857 if (verbose)
5858 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5859 ret = FAIL;
5860 break;
5861 }
5862 ++*arg;
5863 if (IS_WHITE_OR_NUL(**arg))
5864 {
5865 if (verbose)
5866 emsg(_(e_no_white_space_allowed_after_dot));
5867 ret = FAIL;
5868 break;
5869 }
5870
5871 // isolate the name
5872 exp_name = *arg;
5873 while (eval_isnamec(**arg))
5874 ++*arg;
5875 cc = **arg;
5876 **arg = NUL;
5877
5878 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5879 evalarg->eval_cctx, verbose);
5880 **arg = cc;
5881 *arg = skipwhite(*arg);
5882
5883 if (idx < 0 && ufunc == NULL)
5884 {
5885 ret = FAIL;
5886 break;
5887 }
5888 if (idx >= 0)
5889 {
5890 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5891 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5892
5893 copy_tv(sv->sv_tv, rettv);
5894 }
5895 else
5896 {
5897 rettv->v_type = VAR_FUNC;
5898 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5899 }
5900 }
5901
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005902 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5903 || rettv->v_type == VAR_PARTIAL))
5904 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005905 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005906 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5907 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005908
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005909 // Stop the expression evaluation when immediately aborting on
5910 // error, or when an interrupt occurred or an exception was thrown
5911 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005912 if (aborting())
5913 {
5914 if (ret == OK)
5915 clear_tv(rettv);
5916 ret = FAIL;
5917 }
5918 dict_unref(selfdict);
5919 selfdict = NULL;
5920 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005921 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005922 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005923 if (in_vim9script())
5924 *arg = skipwhite(p + 2);
5925 else
5926 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005927 if (ret == OK)
5928 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005929 if (VIM_ISWHITE(**arg))
5930 {
5931 emsg(_(e_nowhitespace));
5932 ret = FAIL;
5933 }
5934 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005935 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005936 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005937 else
5938 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005939 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005940 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005941 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005942 // "." is ".name" lookup when we found a dict or when evaluating and
5943 // scriptversion is at least 2, where string concatenation is "..".
5944 else if (**arg == '['
5945 || (**arg == '.' && (rettv->v_type == VAR_DICT
5946 || (!evaluate
5947 && (*arg)[1] != '.'
5948 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005949 {
5950 dict_unref(selfdict);
5951 if (rettv->v_type == VAR_DICT)
5952 {
5953 selfdict = rettv->vval.v_dict;
5954 if (selfdict != NULL)
5955 ++selfdict->dv_refcount;
5956 }
5957 else
5958 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005959 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005960 {
5961 clear_tv(rettv);
5962 ret = FAIL;
5963 }
5964 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005965 else
5966 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005967 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5970 // Don't do this when "Func" is already a partial that was bound
5971 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005972 if (selfdict != NULL
5973 && (rettv->v_type == VAR_FUNC
5974 || (rettv->v_type == VAR_PARTIAL
5975 && (rettv->vval.v_partial->pt_auto
5976 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005978
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005979 dict_unref(selfdict);
5980 return ret;
5981}
5982
5983/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 * Make a copy of an item.
5985 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005986 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5987 * reference to an already copied list/dict can be used.
5988 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005989 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991item_copy(
5992 typval_T *from,
5993 typval_T *to,
5994 int deep,
5995 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996{
5997 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006002 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006003 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004 }
6005 ++recurse;
6006
6007 switch (from->v_type)
6008 {
6009 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006010 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 case VAR_STRING:
6012 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006013 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006014 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006015 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006016 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006017 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006018 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006019 copy_tv(from, to);
6020 break;
6021 case VAR_LIST:
6022 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006023 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006024 if (from->vval.v_list == NULL)
6025 to->vval.v_list = NULL;
6026 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006028 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006029 to->vval.v_list = from->vval.v_list->lv_copylist;
6030 ++to->vval.v_list->lv_refcount;
6031 }
6032 else
6033 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6034 if (to->vval.v_list == NULL)
6035 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006037 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006039 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006040 case VAR_DICT:
6041 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006042 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 if (from->vval.v_dict == NULL)
6044 to->vval.v_dict = NULL;
6045 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006047 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006048 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6049 ++to->vval.v_dict->dv_refcount;
6050 }
6051 else
6052 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6053 if (to->vval.v_dict == NULL)
6054 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006056 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006057 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006059 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006060 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006061 }
6062 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006063 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006064}
6065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 void
6067echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6068{
6069 char_u *tofree;
6070 char_u numbuf[NUMBUFLEN];
6071 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6072
6073 if (*atstart)
6074 {
6075 *atstart = FALSE;
6076 // Call msg_start() after eval1(), evaluating the expression
6077 // may cause a message to appear.
6078 if (with_space)
6079 {
6080 // Mark the saved text as finishing the line, so that what
6081 // follows is displayed on a new line when scrolling back
6082 // at the more prompt.
6083 msg_sb_eol();
6084 msg_start();
6085 }
6086 }
6087 else if (with_space)
6088 msg_puts_attr(" ", echo_attr);
6089
6090 if (p != NULL)
6091 for ( ; *p != NUL && !got_int; ++p)
6092 {
6093 if (*p == '\n' || *p == '\r' || *p == TAB)
6094 {
6095 if (*p != TAB && *needclr)
6096 {
6097 // remove any text still there from the command
6098 msg_clr_eos();
6099 *needclr = FALSE;
6100 }
6101 msg_putchar_attr(*p, echo_attr);
6102 }
6103 else
6104 {
6105 if (has_mbyte)
6106 {
6107 int i = (*mb_ptr2len)(p);
6108
6109 (void)msg_outtrans_len_attr(p, i, echo_attr);
6110 p += i - 1;
6111 }
6112 else
6113 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6114 }
6115 }
6116 vim_free(tofree);
6117}
6118
Bram Moolenaare9a41262005-01-15 22:18:47 +00006119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 * ":echo expr1 ..." print each argument separated with a space, add a
6121 * newline at the end.
6122 * ":echon expr1 ..." print each argument plain.
6123 */
6124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006129 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 int needclr = TRUE;
6131 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006132 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006133 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006134 evalarg_T evalarg;
6135
Bram Moolenaare707c882020-07-01 13:07:37 +02006136 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138 if (eap->skip)
6139 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006140 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006142 // If eval1() causes an error message the text from the command may
6143 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006144 need_clr_eos = needclr;
6145
Bram Moolenaar68db9962021-05-09 23:19:22 +02006146 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006147 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
6149 /*
6150 * Report the invalid expression unless the expression evaluation
6151 * has been cancelled due to an aborting error, an interrupt, or an
6152 * exception.
6153 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006154 if (!aborting() && did_emsg == did_emsg_before
6155 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006156 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006157 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 break;
6159 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006160 need_clr_eos = FALSE;
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006163 {
6164 if (rettv.v_type == VAR_VOID)
6165 {
6166 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6167 break;
6168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006169 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006170 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006171
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 arg = skipwhite(arg);
6174 }
6175 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006176 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 if (eap->skip)
6179 --emsg_skip;
6180 else
6181 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006182 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (needclr)
6184 msg_clr_eos();
6185 if (eap->cmdidx == CMD_echo)
6186 msg_end();
6187 }
6188}
6189
6190/*
6191 * ":echohl {name}".
6192 */
6193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006194ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006196 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197}
6198
6199/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006200 * Returns the :echo attribute
6201 */
6202 int
6203get_echo_attr(void)
6204{
6205 return echo_attr;
6206}
6207
6208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 * ":execute expr1 ..." execute the result of an expression.
6210 * ":echomsg expr1 ..." Print a message
6211 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006212 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 * Each gets spaces around each argument and a newline at the end for
6214 * echo commands
6215 */
6216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006217ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 int ret = OK;
6222 char_u *p;
6223 garray_T ga;
6224 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 ga_init2(&ga, 1, 80);
6227
6228 if (eap->skip)
6229 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006230 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006232 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006233 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236 if (!eap->skip)
6237 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006238 char_u buf[NUMBUFLEN];
6239
6240 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006241 {
6242 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6243 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006244 semsg(_(e_using_invalid_value_as_string_str),
6245 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006246 p = NULL;
6247 }
6248 else
6249 p = tv_get_string_buf(&rettv, buf);
6250 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006251 else
6252 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006253 if (p == NULL)
6254 {
6255 clear_tv(&rettv);
6256 ret = FAIL;
6257 break;
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 len = (int)STRLEN(p);
6260 if (ga_grow(&ga, len + 2) == FAIL)
6261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006262 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 ret = FAIL;
6264 break;
6265 }
6266 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 ga.ga_len += len;
6270 }
6271
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 arg = skipwhite(arg);
6274 }
6275
6276 if (ret != FAIL && ga.ga_data != NULL)
6277 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006278 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006280 // Mark the already saved text as finishing the line, so that what
6281 // follows is displayed on a new line when scrolling back at the
6282 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006283 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006284 }
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006287 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006288 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006289 out_flush();
6290 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006291 else if (eap->cmdidx == CMD_echoconsole)
6292 {
6293 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6294 ui_write((char_u *)"\r\n", 2, TRUE);
6295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 else if (eap->cmdidx == CMD_echoerr)
6297 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006298 int save_did_emsg = did_emsg;
6299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006301 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (!force_abort)
6303 did_emsg = save_did_emsg;
6304 }
6305 else if (eap->cmdidx == CMD_execute)
6306 do_cmdline((char_u *)ga.ga_data,
6307 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6308 }
6309
6310 ga_clear(&ga);
6311
6312 if (eap->skip)
6313 --emsg_skip;
6314
6315 eap->nextcmd = check_nextcmd(arg);
6316}
6317
6318/*
6319 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6320 * "arg" points to the "&" or '+' when called, to "option" when returning.
6321 * Returns NULL when no option name found. Otherwise pointer to the char
6322 * after the option name.
6323 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *p = *arg;
6328
6329 ++p;
6330 if (*p == 'g' && p[1] == ':')
6331 {
6332 *opt_flags = OPT_GLOBAL;
6333 p += 2;
6334 }
6335 else if (*p == 'l' && p[1] == ':')
6336 {
6337 *opt_flags = OPT_LOCAL;
6338 p += 2;
6339 }
6340 else
6341 *opt_flags = 0;
6342
6343 if (!ASCII_ISALPHA(*p))
6344 return NULL;
6345 *arg = p;
6346
6347 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006348 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 else
6350 while (ASCII_ISALPHA(*p))
6351 ++p;
6352 return p;
6353}
6354
6355/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006356 * Display script name where an item was last set.
6357 * Should only be invoked when 'verbose' is non-zero.
6358 */
6359 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006360last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006361{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006362 char_u *p;
6363
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006364 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006365 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006366 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006367 if (p != NULL)
6368 {
6369 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006370 msg_puts(_("\n\tLast set from "));
6371 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006372 if (script_ctx.sc_lnum > 0)
6373 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006374 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006375 msg_outnum((long)script_ctx.sc_lnum);
6376 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006377 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006378 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006379 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006380 }
6381}
6382
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006383#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
6385/*
6386 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006387 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 * "flags" can be "g" to do a global substitute.
6389 * Returns an allocated string, NULL for error.
6390 */
6391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006392do_string_sub(
6393 char_u *str,
6394 char_u *pat,
6395 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006396 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 int sublen;
6400 regmatch_T regmatch;
6401 int i;
6402 int do_all;
6403 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006404 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 garray_T ga;
6406 char_u *ret;
6407 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006408 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006410 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006412 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 ga_init2(&ga, 1, 200);
6415
6416 do_all = (flags[0] == 'g');
6417
6418 regmatch.rm_ic = p_ic;
6419 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6420 if (regmatch.regprog != NULL)
6421 {
6422 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006423 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006426 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006427 if (regmatch.startp[0] == regmatch.endp[0])
6428 {
6429 if (zero_width == regmatch.startp[0])
6430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006432 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006433 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6434 (size_t)i);
6435 ga.ga_len += i;
6436 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006437 continue;
6438 }
6439 zero_width = regmatch.startp[0];
6440 }
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 /*
6443 * Get some space for a temporary buffer to do the substitution
6444 * into. It will contain:
6445 * - The text up to where the match is.
6446 * - The substituted text.
6447 * - The text after the match.
6448 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006449 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006450 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6452 {
6453 ga_clear(&ga);
6454 break;
6455 }
6456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 i = (int)(regmatch.startp[0] - tail);
6459 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006460 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006461 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 + ga.ga_len + i, TRUE, TRUE, FALSE);
6463 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006464 tail = regmatch.endp[0];
6465 if (*tail == NUL)
6466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (!do_all)
6468 break;
6469 }
6470
6471 if (ga.ga_data != NULL)
6472 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6473
Bram Moolenaar473de612013-06-08 18:19:48 +02006474 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476
6477 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6478 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006479 if (p_cpo == empty_option)
6480 p_cpo = save_cpo;
6481 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006484 // If it's still empty it was changed and restored, need to restore in
6485 // the complicated way.
6486 if (*p_cpo == NUL)
6487 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006488 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491 return ret;
6492}